import { auto } from '@popperjs/core'; import { action } from '@storybook/addon-actions'; import { Meta, Story } from '@storybook/react'; import React, { useState } from 'react'; import { SelectableValue, toIconName } from '@grafana/data'; import { Icon, Select, AsyncSelect, MultiSelect, AsyncMultiSelect } from '@grafana/ui'; import { getAvailableIcons } from '../../types'; import { withCenteredStory, withHorizontallyCenteredStory } from '../../utils/storybook/withCenteredStory'; import mdx from './Select.mdx'; import { generateOptions, generateThousandsOfOptions } from './mockOptions'; import { SelectCommonProps } from './types'; const meta: Meta = { title: 'Forms/Select', component: Select, decorators: [withCenteredStory, withHorizontallyCenteredStory], // SB7 has broken subcomponent types due to dropping support for the feature // https://github.com/storybookjs/storybook/issues/20782 // @ts-ignore subcomponents: { AsyncSelect, MultiSelect, AsyncMultiSelect }, parameters: { docs: { page: mdx, }, controls: { exclude: [ 'getOptionValue', 'getOptionLabel', 'formatCreateLabel', 'filterOption', 'className', 'components', 'defaultValue', 'id', 'inputId', 'onBlur', 'onChange', 'onCloseMenu', 'onCreateOption', 'onInputChange', 'onKeyDown', 'onOpenMenu', 'prefix', 'renderControl', 'options', 'isOptionDisabled', 'maxVisibleValues', 'aria-label', 'noOptionsMessage', 'menuPosition', 'isValidNewOption', 'value', ], }, }, args: { width: 0, disabled: false, isLoading: false, invalid: false, icon: 'arrow-down', }, argTypes: { width: { control: { type: 'range', min: 1, max: 100 } }, icon: { control: { type: 'select', options: getAvailableIcons(), }, }, }, }; const loadAsyncOptions = () => { return new Promise>>((resolve) => { setTimeout(() => { resolve(generateOptions()); }, 2000); }); }; const getPrefix = (prefix: string) => { const prefixEl = ; return prefixEl; }; interface StoryProps extends Partial> { icon: string; } export const Basic: Story = (args) => { const [value, setValue] = useState>(); return ( <> { setValue(v); action('onChange')(v); }} {...args} /> ); }; /** * Uses plain values instead of SelectableValue */ export const BasicSelectPlainValue: Story = (args) => { const [value, setValue] = useState(); return ( <> { setValue(v.value); action('onChange')(v); }} prefix={getPrefix(args.icon)} {...args} /> ); }; /** * Uses plain values instead of SelectableValue */ export const MultiPlainValue: Story = (args) => { const [value, setValue] = useState(); return ( <> { setValue(v.map((v) => v.value!)); }} prefix={getPrefix(args.icon)} {...args} /> ); }; export const MultiSelectWithOptionGroups: Story = (args) => { const [value, setValue] = useState(); return ( <> { setValue(v.map((v) => v.value!)); action('onChange')(v); }} prefix={getPrefix(args.icon)} {...args} /> ); }; export const MultiSelectBasic: Story = (args) => { const [value, setValue] = useState>>([]); return ( <> { setValue(v); action('onChange')(v); }} prefix={getPrefix(args.icon)} {...args} /> ); }; MultiSelectBasic.args = { isClearable: false, closeMenuOnSelect: false, maxVisibleValues: 5, }; export const MultiSelectAsync: Story = (args) => { const [value, setValue] = useState>>(); return ( { setValue(v); action('onChange')(v); }} prefix={getPrefix(args.icon)} {...args} /> ); }; MultiSelectAsync.args = { allowCustomValue: false, }; export const BasicSelectAsync: Story = (args) => { const [value, setValue] = useState>(); return ( { setValue(v); action('onChange')(v); }} prefix={getPrefix(args.icon)} {...args} /> ); }; export const AutoMenuPlacement: Story = (args) => { const [value, setValue] = useState>(); return ( <>
{ setValue(v); action('onChange')(v); }} prefix={getPrefix(args.icon)} {...args} width="auto" />
); }; export const CustomValueCreation: Story = (args) => { const [value, setValue] = useState>(); const [customOptions, setCustomOptions] = useState>>([]); const options = generateOptions(); return ( <>