storybook: addon-knobs select should be able to have undefined options.
If you are reporting a bug or requesting support, start here:
Bug or support request summary
const pressed = select(
'Pressed',
{ Undefined: undefined, False: false, True: true },
undefined
);
I’d expect there to be a value called Undefined which is selectable, but it is removed when rendered in the knobs panel.
Please specify which version of Storybook and optionally any affected addons that you’re running
- @storybook/react latest
- @storybook/addon-knobs latest (alpha)
Affected platforms
- If UI related, please indicate browser, OS, and version
- If dependency related, please include relevant version numbers
- If developer tooling related, please include the platform information
Screenshots / Screencast / Code Snippets (Optional)
const pressed = select(
'Pressed',
{ Undefined: undefined, False: false, True: true },
undefined
);
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 18
- Comments: 22 (18 by maintainers)
Supporting
undefined
options is very important when displaying React components which have been written in TypeScript.In TypeScript it is common sense to use the question mark to declare optional parameters and properties. So an interface for a component’s properties can look like this:
Which is equivalent to:
But different from:
Unfortunately, it is not possible to use
undefined
values with select, so states with optional props cannot be shown with@storybook/addon-knobs
. 😢Hey, Just to add a little extra info to @bennyn comments, it’s not just TypeScript but ES6 (EcmaScript 2015) that uses optional parameters which only accepts undefined (not null).
In TypeScript it would be something like this:
but you can do the same in ES6 like this:
if
environment = null
it will benull
instead the method and not the default value'win'
@shilman still an issue with args.
should have an option here to not have a value.
@ndelangen has a new version been shipped with this functionality yet? If not, is it possible to get a link somewhere this work is being tracked?
@lifeiscontent telejson states in the repo description that it supports
undefined
while in fact it doesn’t. So it’s not some specific requirement of knobs@christophehurpeau note that it’s telejson library that needs a PR:
https://github.com/storybookjs/storybook/issues/4487#issuecomment-485805825 https://github.com/storybookjs/telejson/issues/7
Looks like the root of the issue is that
telejson
library that we use to transmit data between preview and manager frames usesJSON.parse(data, reviver())
which ignoresundefined
values:@ndelangen you probably need to iterate on object fields manually instead of using reviver parameter of
JSON.parse
.@lifeiscontent Are you still working on this? If not, I’ll start looking into it 🙂