Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storybook default selection control value

I'm using Storybook with React, and trying to add storybook select control with default value, but with no success. There's nothing about that in documentation. enter image description here

enter image description here

I need one of the options above to be selected by default.

    argTypes: {
        iconSrc: {
            options: Object.keys(iconTypes),
            mapping: iconTypes,
            control: {
                sort: 'requiredFirst',
                type: 'select',
                labels: {
                    BlueStep: 'Blue Step',
                    Step: 'Step',
                    Globe: 'Globe',
                },
            },
        },
    }

like image 239
Eylon Shmilovich Avatar asked Mar 09 '26 08:03

Eylon Shmilovich


2 Answers

You have to add the table key

your storybook will look like this:

 argTypes: {
        iconSrc: {
            options: Object.keys(iconTypes),
            mapping: iconTypes,
            control: {
                sort: 'requiredFirst',
                type: 'select',
                labels: {
                    BlueStep: 'Blue Step',
                    Step: 'Step',
                    Globe: 'Globe',
                },
            },
            table: {
              type: { summary: 'select' },
              defaultValue: { summary: 'Step' },
            },
        },
    }
like image 117
Martin Gainza Avatar answered Mar 12 '26 00:03

Martin Gainza


The most upvoted answer at time of writing refers to the table key, which is used for specifying how an arg is documented.

However the question is asking how to set a default that will be the initially selected option on the <select/> element rendered when using the select control. In the rendered HTML this would be done by setting the selected boolean on the <option/> intended as default.

It appears that Storybook does not expose a way to accomplish this, and that this is by design (for better or worse). Instead an optional control value is initialized to undefined. defaultValue has been deprecated.

like image 43
jf7 Avatar answered Mar 11 '26 22:03

jf7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!