Can I create a Knob in React Storybook to modify an array with 4 values where each value has a select...











up vote
0
down vote

favorite












I'm using React Storybook (https://github.com/storybooks/storybook/tree/next/app/react) to write stories for my ui components, and I use the knobs addon (https://github.com/storybooks/storybook/tree/next/addons/knobs) to create realtime editable props...



My application is written using flow-types so my props are types, I have defined this type to work with my theme:





export type BoxModel =
| [ThemeSpacing | 0]
| [ThemeSpacing | 0, ThemeSpacing | 0]
| [ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0]
| [ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0];




so the idea is I can define padding or margin on a component using an array of my theme values



this is my theme:



spacing: {
xxSmall: '2px',
xSmall: '4px',
small: '8px',
medium: '12px',
larger: '16px',
large: '24px',
xLarge: '32px',
xxLarge: '48px',
},


props of my component (several of them)



export type LinkStyleProps = {
theme?: Theme,
padding?: BoxModel,
margin?: BoxModel,
};


So I can basically define margin & padding on my component using an array like ['small', 'medium', 'xsmall'] etc... a lot of like css shorthand



Is there a way to create a Knob that can have this array & have each field selectable via a select?










share|improve this question









New contributor




nickpro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I'm using React Storybook (https://github.com/storybooks/storybook/tree/next/app/react) to write stories for my ui components, and I use the knobs addon (https://github.com/storybooks/storybook/tree/next/addons/knobs) to create realtime editable props...



    My application is written using flow-types so my props are types, I have defined this type to work with my theme:





    export type BoxModel =
    | [ThemeSpacing | 0]
    | [ThemeSpacing | 0, ThemeSpacing | 0]
    | [ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0]
    | [ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0];




    so the idea is I can define padding or margin on a component using an array of my theme values



    this is my theme:



    spacing: {
    xxSmall: '2px',
    xSmall: '4px',
    small: '8px',
    medium: '12px',
    larger: '16px',
    large: '24px',
    xLarge: '32px',
    xxLarge: '48px',
    },


    props of my component (several of them)



    export type LinkStyleProps = {
    theme?: Theme,
    padding?: BoxModel,
    margin?: BoxModel,
    };


    So I can basically define margin & padding on my component using an array like ['small', 'medium', 'xsmall'] etc... a lot of like css shorthand



    Is there a way to create a Knob that can have this array & have each field selectable via a select?










    share|improve this question









    New contributor




    nickpro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm using React Storybook (https://github.com/storybooks/storybook/tree/next/app/react) to write stories for my ui components, and I use the knobs addon (https://github.com/storybooks/storybook/tree/next/addons/knobs) to create realtime editable props...



      My application is written using flow-types so my props are types, I have defined this type to work with my theme:





      export type BoxModel =
      | [ThemeSpacing | 0]
      | [ThemeSpacing | 0, ThemeSpacing | 0]
      | [ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0]
      | [ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0];




      so the idea is I can define padding or margin on a component using an array of my theme values



      this is my theme:



      spacing: {
      xxSmall: '2px',
      xSmall: '4px',
      small: '8px',
      medium: '12px',
      larger: '16px',
      large: '24px',
      xLarge: '32px',
      xxLarge: '48px',
      },


      props of my component (several of them)



      export type LinkStyleProps = {
      theme?: Theme,
      padding?: BoxModel,
      margin?: BoxModel,
      };


      So I can basically define margin & padding on my component using an array like ['small', 'medium', 'xsmall'] etc... a lot of like css shorthand



      Is there a way to create a Knob that can have this array & have each field selectable via a select?










      share|improve this question









      New contributor




      nickpro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm using React Storybook (https://github.com/storybooks/storybook/tree/next/app/react) to write stories for my ui components, and I use the knobs addon (https://github.com/storybooks/storybook/tree/next/addons/knobs) to create realtime editable props...



      My application is written using flow-types so my props are types, I have defined this type to work with my theme:





      export type BoxModel =
      | [ThemeSpacing | 0]
      | [ThemeSpacing | 0, ThemeSpacing | 0]
      | [ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0]
      | [ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0, ThemeSpacing | 0];




      so the idea is I can define padding or margin on a component using an array of my theme values



      this is my theme:



      spacing: {
      xxSmall: '2px',
      xSmall: '4px',
      small: '8px',
      medium: '12px',
      larger: '16px',
      large: '24px',
      xLarge: '32px',
      xxLarge: '48px',
      },


      props of my component (several of them)



      export type LinkStyleProps = {
      theme?: Theme,
      padding?: BoxModel,
      margin?: BoxModel,
      };


      So I can basically define margin & padding on my component using an array like ['small', 'medium', 'xsmall'] etc... a lot of like css shorthand



      Is there a way to create a Knob that can have this array & have each field selectable via a select?







      javascript reactjs flowtype storybook






      share|improve this question









      New contributor




      nickpro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      nickpro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 days ago









      skyboyer

      2,69511027




      2,69511027






      New contributor




      nickpro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 10 at 12:07









      nickpro

      33




      33




      New contributor




      nickpro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      nickpro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      nickpro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          It is definitely possible to do that with Storybook knobs. You can use something like the example below. You can do additional logic to build the array with fewer than 4 elements if you want as well, you would just need an option in the select to indicate that and dynamically build the array.



          storiesOf("YourComponent", module)
          .add("with knobs", () => {
          const options = {
          none: '0',
          xxSmall: 'xxSmall',
          xSmall: 'xSmall',
          small: 'small',
          // etc.
          };
          const top = select('Top', options, '0');
          const right = select('Right', options, '0');
          const bottom = select('Bottom', options, '0');
          const left = select('Left', options, '0');
          return <YourComponent yourProp={[top, right, bottom, left]} />;
          });





          share|improve this answer





















          • Thanks! This is awesome and pointed me exactly in the right direction. I had no idea knobs were this flexible, this opens up a whole new way to do component stories for me with these more complex props! Cheers!
            – nickpro
            2 days ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          nickpro is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238778%2fcan-i-create-a-knob-in-react-storybook-to-modify-an-array-with-4-values-where-ea%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          It is definitely possible to do that with Storybook knobs. You can use something like the example below. You can do additional logic to build the array with fewer than 4 elements if you want as well, you would just need an option in the select to indicate that and dynamically build the array.



          storiesOf("YourComponent", module)
          .add("with knobs", () => {
          const options = {
          none: '0',
          xxSmall: 'xxSmall',
          xSmall: 'xSmall',
          small: 'small',
          // etc.
          };
          const top = select('Top', options, '0');
          const right = select('Right', options, '0');
          const bottom = select('Bottom', options, '0');
          const left = select('Left', options, '0');
          return <YourComponent yourProp={[top, right, bottom, left]} />;
          });





          share|improve this answer





















          • Thanks! This is awesome and pointed me exactly in the right direction. I had no idea knobs were this flexible, this opens up a whole new way to do component stories for me with these more complex props! Cheers!
            – nickpro
            2 days ago















          up vote
          0
          down vote



          accepted










          It is definitely possible to do that with Storybook knobs. You can use something like the example below. You can do additional logic to build the array with fewer than 4 elements if you want as well, you would just need an option in the select to indicate that and dynamically build the array.



          storiesOf("YourComponent", module)
          .add("with knobs", () => {
          const options = {
          none: '0',
          xxSmall: 'xxSmall',
          xSmall: 'xSmall',
          small: 'small',
          // etc.
          };
          const top = select('Top', options, '0');
          const right = select('Right', options, '0');
          const bottom = select('Bottom', options, '0');
          const left = select('Left', options, '0');
          return <YourComponent yourProp={[top, right, bottom, left]} />;
          });





          share|improve this answer





















          • Thanks! This is awesome and pointed me exactly in the right direction. I had no idea knobs were this flexible, this opens up a whole new way to do component stories for me with these more complex props! Cheers!
            – nickpro
            2 days ago













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          It is definitely possible to do that with Storybook knobs. You can use something like the example below. You can do additional logic to build the array with fewer than 4 elements if you want as well, you would just need an option in the select to indicate that and dynamically build the array.



          storiesOf("YourComponent", module)
          .add("with knobs", () => {
          const options = {
          none: '0',
          xxSmall: 'xxSmall',
          xSmall: 'xSmall',
          small: 'small',
          // etc.
          };
          const top = select('Top', options, '0');
          const right = select('Right', options, '0');
          const bottom = select('Bottom', options, '0');
          const left = select('Left', options, '0');
          return <YourComponent yourProp={[top, right, bottom, left]} />;
          });





          share|improve this answer












          It is definitely possible to do that with Storybook knobs. You can use something like the example below. You can do additional logic to build the array with fewer than 4 elements if you want as well, you would just need an option in the select to indicate that and dynamically build the array.



          storiesOf("YourComponent", module)
          .add("with knobs", () => {
          const options = {
          none: '0',
          xxSmall: 'xxSmall',
          xSmall: 'xSmall',
          small: 'small',
          // etc.
          };
          const top = select('Top', options, '0');
          const right = select('Right', options, '0');
          const bottom = select('Bottom', options, '0');
          const left = select('Left', options, '0');
          return <YourComponent yourProp={[top, right, bottom, left]} />;
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 16:15









          Tyler

          7267




          7267












          • Thanks! This is awesome and pointed me exactly in the right direction. I had no idea knobs were this flexible, this opens up a whole new way to do component stories for me with these more complex props! Cheers!
            – nickpro
            2 days ago


















          • Thanks! This is awesome and pointed me exactly in the right direction. I had no idea knobs were this flexible, this opens up a whole new way to do component stories for me with these more complex props! Cheers!
            – nickpro
            2 days ago
















          Thanks! This is awesome and pointed me exactly in the right direction. I had no idea knobs were this flexible, this opens up a whole new way to do component stories for me with these more complex props! Cheers!
          – nickpro
          2 days ago




          Thanks! This is awesome and pointed me exactly in the right direction. I had no idea knobs were this flexible, this opens up a whole new way to do component stories for me with these more complex props! Cheers!
          – nickpro
          2 days ago










          nickpro is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          nickpro is a new contributor. Be nice, and check out our Code of Conduct.













          nickpro is a new contributor. Be nice, and check out our Code of Conduct.












          nickpro is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238778%2fcan-i-create-a-knob-in-react-storybook-to-modify-an-array-with-4-values-where-ea%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ