Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get dropdown in controls addon for Storybook?

Tags:

storybook

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.

like image 761
Joshua Augustinus Avatar asked Aug 28 '20 21:08

Joshua Augustinus


People also ask

How do you add controls on storybook?

To use the Controls addon, you need to write your stories using args. Storybook will automatically generate UI controls based on your args and what it can infer about your component. Still, you can configure the controls further using argTypes, see below.

What are ArgTypes in storybook?

ArgTypes are a first-class feature in Storybook for specifying the behaviour of Args. By specifying the type of an arg, you constrain the values that it can take and provide information about args that are not explicitly set (i.e., not required).

What are addon knobs?

Storybook Addon Knobs allow you to edit props dynamically using the Storybook UI. You can also use Knobs as a dynamic variable inside stories in Storybook. Framework Support. This is what Knobs looks like: Checkout the above Live Storybook or watch this video.

What is the storybook controls addon?

Storybook Controls Addon Storybook Controls gives you a graphical UI to interact with a component's arguments dynamically, without needing to code. It creates an addon panel next to your component examples ("stories"), so you can edit them live.

What are control values in Storybook Stories?

Storybook stories (component examples) are functions that return a rendered component. Control values are passed to your Story functions as arguments, and you can also declare the initial values succinctly:

What is the difference between controls and stories?

Storybook stories (component examples) are functions that return a rendered component. Control values are passed to your Story functions as arguments, and you can also declare the initial values succinctly: Controls works with Docs, Storybook’s automated documentation generator for UI components, including both DocsPage and MDX.

How do I create a controls storybook in Bootstrap?

Or bootstrap Storybook into an existing app: To create your first Controls story, create a function that takes an Args object as its first input, and then annotate the function with the data you want to receive:


Video Answer


2 Answers

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

like image 129
Federico Rodriguez La Rosa Avatar answered Oct 23 '22 20:10

Federico Rodriguez La Rosa


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 },
  },
};
like image 29
samoilov Avatar answered Oct 23 '22 18:10

samoilov