Skip to content
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

Suggestion: addon-knobs select() with TypeScript enum #7420

Closed
ChristianOezelt opened this issue Jul 15, 2019 · 3 comments
Closed

Suggestion: addon-knobs select() with TypeScript enum #7420

ChristianOezelt opened this issue Jul 15, 2019 · 3 comments

Comments

@ChristianOezelt
Copy link

Is your feature request related to a problem? Please describe.
I'd like if (none const) enums are working out of the box with select(...) knobs, as this is "just" a typing issue.

Here is an example of what I'd like to do:

index.tsx

export enum ButtonVariant {
    primary = 'primary',
    secondary = 'secondary'
}

export const Button: React.FC<{ variant: ButtonVariant }> = ({ variant }) => /* ... */;

index.story.tsx

storiesOf('Button', module)
    .add('variants', () => <Button variant={ select('variant', ButtonVariant, ButtonVariant.primary) }/>)

Describe the solution you'd like
The fix I currently use is a custom module declaration in my project.
I suppose the additional type could be added as an overload of the select() function

/**
 * the import is here to make the file a module
 * else it would replace the typedefs, instead of extending them
 * which leads to missing types for everything, but select with enums
 */
import '@storybook/addon-knobs';

declare module '@storybook/addon-knobs' {
    /**
     * @typeparam EnumT any none const enum, e.g ButtonVariant
     * @typeparam ResultT one (and any) of the enum values, e.g ButtonVariant.Primary
     *
     * @example
     * enum ButtonVariant {
     *   Primary = 'primary',
     *   Secondary = 'secondary'
     * }
     * const selectKnob: ButtonVariant = select('variant', ButtonVariant, ButtonVariant.Primary);
     */
    export function select<EnumT extends {}, ResultT = EnumT[keyof EnumT]>(
        name: string,
        options: EnumT,
        value: ResultT,
        groupId?: string
    ): ResultT;
}

Describe alternatives you've considered

  • writing a function that creates a select knob out of an enum, its default value and name

Are you able to assist bring the feature to reality?
I can create a PullRequest, suppose I'd find time on the weekend.
If this is a wanted feature.

Additional context
the TS Error I get currently:

TS2345: Argument of type 'typeof ButtonVariant' is not assignable to parameter of type 'readonly ButtonVariant.primary[]'.   Type 'typeof ButtonVariant' is missing the following properties from type 'readonly ButtonVariant.primary[]': length, concat, join, slice, and 16 more
@shilman
Copy link
Member

shilman commented Jul 17, 2019

Is this addressed by #7411 ??

@emilio-martinez
Copy link
Contributor

emilio-martinez commented Jul 17, 2019

Addressed in #7411.

@shilman
Copy link
Member

shilman commented Jul 18, 2019

Son of a gun!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.2.0-beta.1 containing PR #7411 that references this issue. Upgrade today to try it out!

You can find this prerelease on the @next NPM tag.

Closing this issue. Please re-open if you think there's still more to do.

@shilman shilman closed this as completed Jul 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants