How do I get dropdown in controls addon for Storybook?
Asked Answered
K

3

19

I want to get this: enter image description here

My stories.tsx code looks like this:

export default {
  title: "Components/Switch",
  argTypes: {
    color: { control: "select", options: ["primary", "secondary"] },
  },
};

However, the page just fails to render when I do that.

To reproduce:

Clone this repository: https://github.com/jauggy/storybook-args-error

npm i

npm run storybook

Select the Switch component on the left menu.

Khosrow answered 28/8, 2020 at 21:34 Comment(0)
T
40

you should send an object to the control property. Like this:

export default {
  title: "Components/Switch",
  argTypes: {
    color: { control: { type: "select", options: ["primary", "secondary"] } },
  },
};

Update after Storybook v7 control.options will be deprecated for more info go to: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-controloptions

Tamratamsky answered 31/8, 2020 at 20:41 Comment(2)
Storybook v7: you would move the options array as a child of color and a sibling of control. This also works in Storybook v6.2Messenger
This does not seem to work v6.5Filomena
U
6

You could also provide there an enum:

const SwitchTypeEnum {
    PRIMARY = "primary",
    SECONDARY = "secondary",
}

export default {
  title: "Components/Switch",
  argTypes: {
    table: {
        summary: "SwitchTypeEnum",
        defaultValue: { summary: "SwitchTypeEnum.PRIMARY" },
    },
    color: { control: { type: "select", options: SwitchTypeEnum },
  },
};
Unpeopled answered 20/10, 2020 at 12:35 Comment(1)
do you know if we could connect options directly to .d.ts (typescript ), ex: if i have a interface of { varient?: "primary" | "secondary" | "dark" | "white" }, then making a enums again is i feel lazyEleanoraeleanore
C
2

In Storybook 7, like this

    propertyB: {
      options: ['Another Item One', 'Another Item Two', 'Another Item Three'],
    },

Reference: https://storybook.js.org/docs/react/essentials/controls#dealing-with-complex-values

Cubby answered 17/4, 2023 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.