2.0.1 • Published 27 days ago

storybook-addon-multiselect v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
27 days ago

Storybook Addon Multiselect

Toolbar addon for Storybook that allows to create dropdown(s) to select single or multiple options. Also, there's user defined type of options - that allows user to toggle between select single/multiple option(s).

onChange callback returns an array of selected options with exposing Storybook API. This callback allows to manipulate with both selected options and Storybook API.\ IMPORTANT NOTE: with Storybook 8 you cannot pass any function parameters - Storybook clears it up. So, passing onChange as function won't work with version 8. As a workaround you can make a function to be a string and then plugin will parse it back to function. See example below.API.

To access selected options in your stories, you need to implement your own decorator component that will access globals.multiselect and you can use it in your stories.

Optional Reset to default button that resets all options to default values.

Demo multiselect image

Live demo

https://cyberpunk-ui-library.vercel.app \ Source code

Here, in the top bar you can see two dropdowns: 'Theme Storybook' and 'Theme & Language'. Both of them are implemented with this addon.

'Theme Storybook' is a single select dropdown that allows to change Storybook theme (outer shell) - for this onChange callback used as it allows to reach Storybook API.

'Theme & Language' is a complex dropdown that has single and multiple select groups, as well as reset button.

Quick start

Here's a quick and minimal example of how to use this addon:

npm i -D storybook-addon-multiselect
// .storybook/main.ts

const config: StorybookConfig = {
  // ... (other config)
  addons: [
    // ... (other addons)
    'storybook-addon-multiselect'
  ]
  // ... (other config)
}
// .storybook/preview.ts(x)

// ... (other imports)
import { Addon } from 'storybook-addon-multiselect'
import ExampleDecorator from '../src/ExampleDecorator'

const multiselect: Addon = {
  example: {
    icon: <>💾</>,
    elements: [
      {
        type: 'singleSelect',
        queryKey: 'example',
        options: [
          { title: 'Option 1', value: 'option1' },
          { title: 'Option 2', value: 'option2' },
          { title: 'Option 3', value: 'option3' }
        ]
      }
    ]
  }
}

const preview: Preview = {
  parameters: {
    // ... (other parameters)
    multiselect
  },
  decorators: [
    // ... (other decorators)
    ExampleDecorator
  ]
}
// src/ExampleDecorator.tsx

import type { Decorator } from '@storybook/react'

const ExampleDecorator: Decorator = (Story, context) => {
  const { globals } = context
  const multiselect: { [key: string]: string | string[] } = globals.multiselect
  const example = multiselect.example as 'option1' | 'option2' | 'option3'
  console.log('example value', example)

  return <Story />
}

export default ExampleDecorator

## `onChange` callback

`onChange` callback returns an array of selected options with exposing Storybook API. This callback allows to manipulate with both selected options and Storybook API. If you return nothing, then selected options won't change. If you return an array, then selected options will be replaced with this array.

As mentioned above, Storybook 8 clears up any function parameters, so you can't pass `onChange` as function. As a workaround you can make a function to be a string and then plugin will parse it back to function.

```ts
const multiselect: Addon = {
  example: {
    icon: <>💾</>,
    elements: [...],
    onChange: `(values, api) => {
      const { emit } = api
      emit('customEmit', values)

      if (values.includes('parzival')) {
        return [...values, 'art3mis']
      }
      return values
    }`,
  }
}

If yours version of Storybook below 8, you can pass onChange as function:

const multiselect: Addon = {
  example: {
    icon: <>💾</>,
    elements: [...],
    onChange: (values, api) => {
      api.emit('customEmit', values)

     if (values.includes('parzival')) {
        return [...values, 'art3mis']
      }
      return values
    },
  }
}

onfig: StorybookConfig = { // ... addons: // ... 'storybook-addon-multiselect' // .... }

Set up options in `.storybook/preview.tsx`:

```ts
// Addon is heavily typed and have jsdoc comments, so you can see all the clues in your IDE
import { Addon } from 'storybook-addon-multiselect'

// implement your own decorator component that will access `globals.multiselect` and you can use it in your stories
import YourDecoratorComponent from '../src/decorators/YourDecoratorComponent'

const multiselect: Addon = {
  // how many keys is how many dropdowns will be rendered
  theme: {
    // (optional)
    name: 'Theme',
    // put your svg or emoji (optional)
    icon: <svg />,
    // (optional)
    description: 'Change theme of Storybook (outer shell)',
    elements: [
      // reset button (optional)
      { type: 'reset' },
      // single select
      {
        type: 'singleSelect',
        // key that will be used in query string (required)
        queryKey: 'priority',
        // title of dropdown (required)
        title: 'Priority',
        // default value (optional)
        defaultValue: 'theme',
        // options (required)
        options: [
          {
            // title of option (required)
            title: 'Theme',
            // value of option (required)
            value: 'theme',
            // left side of option (optional)
            left: <svg />
          },
          {
            title: 'Language',
            value: 'lang',
            left: <svg />
          }
        ]
      }
    ]
  },
  themeAndLanguage: {
    // ...
  }
}

const preview: Preview = {
  // ...
  parameters: {
    // ...
    multiselect
  },
  decorators: [
    // ...
    YourDecoratorComponent
  ]
}

Things to do

  • Add boolean flag to close toolbar after selection
  • Measure performance and optimize
  • Finish readme

Known issues

  • Performance issues. No memo used intentionally to avoid premature optimization. However, profiling and optimization is planned.
1.0.9

27 days ago

1.0.8

27 days ago

1.0.7

27 days ago

1.0.6

27 days ago

2.0.1

27 days ago

1.0.5

3 months ago

1.0.4

3 months ago

1.0.2

4 months ago

1.0.3

4 months ago

1.0.1

4 months ago

0.0.38

4 months ago

0.0.37

6 months ago

0.0.35

6 months ago

0.0.36

6 months ago

0.0.34

8 months ago

0.0.33

8 months ago

0.0.32

8 months ago

0.0.31

8 months ago

0.0.30

8 months ago

0.0.29

9 months ago

0.0.28

9 months ago

0.0.27

9 months ago

0.0.26

9 months ago

0.0.25

9 months ago

0.0.24

9 months ago

0.0.23

9 months ago

0.0.22

9 months ago

0.0.21

9 months ago

0.0.20

9 months ago

0.0.19

9 months ago

0.0.18

9 months ago

0.0.17

9 months ago

0.0.16

9 months ago

0.0.15

10 months ago

0.0.14

10 months ago

0.0.13

10 months ago

0.0.12

10 months ago

0.0.11

10 months ago

0.0.10

10 months ago

0.0.9

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.0

10 months ago