import React, { useState } from 'react'; import { withCenteredStory, withHorizontallyCenteredStory } from '../../utils/storybook/withCenteredStory'; import { SelectableValue } from '@grafana/data'; import { Icon, Button, ButtonSelect, Select, AsyncSelect, MultiSelect, AsyncMultiSelect } from '@grafana/ui'; import { getAvailableIcons, IconName } from '../../types'; import { select, boolean, number } from '@storybook/addon-knobs'; import { getIconKnob } from '../../utils/storybook/knobs'; import kebabCase from 'lodash/kebabCase'; import { generateOptions } from './mockOptions'; import mdx from './Select.mdx'; export default { title: 'Forms/Select', component: Select, decorators: [withCenteredStory, withHorizontallyCenteredStory], subcomponents: { AsyncSelect, MultiSelect, AsyncMultiSelect }, parameters: { docs: { page: mdx, }, }, }; const BEHAVIOUR_GROUP = 'Behaviour props'; const loadAsyncOptions = () => { return new Promise>>(resolve => { setTimeout(() => { resolve(generateOptions()); }, 2000); }); }; const getKnobs = () => { const disabled = boolean('Disabled', false, BEHAVIOUR_GROUP); const invalid = boolean('Invalid', false, BEHAVIOUR_GROUP); const loading = boolean('Loading', false, BEHAVIOUR_GROUP); const prefixSuffixOpts = { None: null, Text: '$', ...getAvailableIcons().reduce>((prev, c) => { return { ...prev, [`Icon: ${c}`]: `icon-${c}`, }; }, {}), }; const VISUAL_GROUP = 'Visual options'; // --- const prefix = select('Prefix', prefixSuffixOpts, null, VISUAL_GROUP); const width = number('Width', 0, undefined, VISUAL_GROUP); let prefixEl: any = prefix; if (prefix && prefix.match(/icon-/g)) { prefixEl = ; } return { width, disabled, invalid, loading, prefixEl, }; }; const getMultiSelectKnobs = () => { const isClearable = boolean('Clearable', false, BEHAVIOUR_GROUP); const closeMenuOnSelect = boolean('Close on Select', false, BEHAVIOUR_GROUP); const maxVisibleValues = number('Max. visible values', 5, undefined, BEHAVIOUR_GROUP); return { isClearable, closeMenuOnSelect, maxVisibleValues, }; }; const getDynamicProps = () => { const knobs = getKnobs(); return { width: knobs.width, disabled: knobs.disabled, isLoading: knobs.loading, invalid: knobs.invalid, prefix: knobs.prefixEl, }; }; export const Basic = () => { const [value, setValue] = useState>(); return ( <> { setValue(v.value); }} {...getDynamicProps()} /> ); }; /** * Uses plain values instead of SelectableValue */ export const SelectWithOptionDescriptions = () => { // TODO this is not working with new Select const [value, setValue] = useState(); const options = [ { label: 'Basic option', value: 0 }, { label: 'Option with description', value: 1, description: 'this is a description' }, { label: 'Option with description and image', value: 2, description: 'This is a very elaborate description, describing all the wonders in the world.', imgUrl: 'https://placekitten.com/40/40', }, ]; return ( <> { setValue(v); }} renderControl={React.forwardRef(({ isOpen, value, ...otherProps }, ref) => { return ( ); })} {...getDynamicProps()} /> ); }; export const AutoMenuPlacement = () => { const [value, setValue] = useState>(); return ( <>
{ setValue(v); }} allowCustomValue onCreateOption={v => { const customValue: SelectableValue = { value: kebabCase(v), label: v }; setCustomOptions([...customOptions, customValue]); setValue(customValue); }} {...getDynamicProps()} /> ); };