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?
javascript reactjs flowtype storybook
New contributor
add a comment |
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?
javascript reactjs flowtype storybook
New contributor
add a comment |
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?
javascript reactjs flowtype storybook
New contributor
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
javascript reactjs flowtype storybook
New contributor
New contributor
edited 2 days ago
skyboyer
2,69511027
2,69511027
New contributor
asked Nov 10 at 12:07
nickpro
33
33
New contributor
New contributor
add a comment |
add a comment |
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]} />;
});
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
add a comment |
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]} />;
});
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
add a comment |
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]} />;
});
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
add a comment |
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]} />;
});
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]} />;
});
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
add a comment |
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
add a comment |
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.
nickpro is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password