-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
select knob for optional prop #805
Comments
Comment by bluetidepro I don't know if there is a more graceful way to do this @alex-bezek, but the way I have done this in the past is by doing:
And then that way by default it passes Hope that helps some. |
Comment by Djspaceg An alternative that I do occasionally is this:
Knobs interprets this Your propTypes can then look like this if you want to make Storybook Knobs happy:
It would be great if knobs could interpret having a literal |
I've tried to find a way to make
Button.propTypes = {
fooString: PropTypes.oneOf([
'one',
'two',
'three',
]),
fooNumber: PropTypes.oneOf([
0,
30,
60,
90,
]),
}
const fooStringValues = {
'null': 'Select a fooString',
one: 'one',
two: 'two',
three: 'three',
} or like this: const fooStringValues = [null, 'one', 'two', 'three'] <Button fooString={select('fooString', fooStringValues) } /> works perfectly when storybook is loaded, but selecting a value and then the
a. Let the user take care of this.In this case we could add some examples to the documentation.
Button.propTypes = {
fooString: PropTypes.oneOf([
'',
'one',
'two',
'three',
])
} Drawbacks: this doesn't sound optimal at all, unless the '' is actually handled by the component and not just ignored.
<Button fooString={select('fooString', fooStringValues) || undefined } /> Drawbacks: this requires changing code in all of one's stories. Note: this workaround can also be used to correctly pass "number" props, that otherwise are passed as strings and trigger const selectNumber = (key, values, defaultValue) => {
const ret = select(key, values, defaultValue)
return (ret !== '0' && !ret) ? undefined : parseInt(ret, 10)
}
...
<Button fooNumber={selectNumber('fooNumber', fooNumberValues)} /> b. Modify
|
Related: #799 I'm open to option b actually, the easier we can make the usage of knobs, the better. Are you open to making a PR on this @matteocng ? |
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 60 days. Thanks! |
Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook! |
Hi there, Just use next code snippet in your stories <Component
myProp={select('myProp', [null, 'value1', 'value2']) || undefined}
/> It will pass undefined to Component without any prop types warnings or errors. Component.propTypes = {
myProp: PropTypes.oneOf(['value1', 'value2']),
}; |
Issue by alex-bezek
Thursday Feb 02, 2017 at 02:20 GMT
Originally opened as storybook-eol/storybook-addon-knobs#85
I am trying to implement a story book for a react component with a number of optional props that
PropTypes.oneOf(['option1', 'option2', 'option3'])
I would like to be able to configure this knob so by default it doesn't pass in the prop. Is this possible and I'm missing an explanation?
Thanks!
The text was updated successfully, but these errors were encountered: