From 98418ea55f451c6d4b22151a7f5672c9e4751ace Mon Sep 17 00:00:00 2001 From: Tobias Skarhed <1438972+tskarhed@users.noreply.github.com> Date: Tue, 3 Mar 2020 17:06:57 +0100 Subject: [PATCH] Old AsyncSelect: Add story (#22536) * Change to CSF * Remove comments * Fix AsyncSelect error * Remove changes to the component * Restructure * Remove comments * Add searching * Remove witespace Co-Authored-By: Dominik Prokop Co-authored-by: Dominik Prokop --- .../src/components/Select/Select.story.tsx | 69 ++++++++++++++----- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/packages/grafana-ui/src/components/Select/Select.story.tsx b/packages/grafana-ui/src/components/Select/Select.story.tsx index 293e3e50365..d4a8acfb45a 100644 --- a/packages/grafana-ui/src/components/Select/Select.story.tsx +++ b/packages/grafana-ui/src/components/Select/Select.story.tsx @@ -1,23 +1,31 @@ -import React from 'react'; -import { storiesOf } from '@storybook/react'; +import React, { useState, useCallback } from 'react'; import { action } from '@storybook/addon-actions'; import { withKnobs, object } from '@storybook/addon-knobs'; import { withCenteredStory } from '../../utils/storybook/withCenteredStory'; import { UseState } from '../../utils/storybook/UseState'; import { SelectableValue } from '@grafana/data'; -import { Select } from './Select'; +import { Select, AsyncSelect } from './Select'; -const SelectStories = storiesOf('General/Select/Select', module); +export default { + title: 'General/Select/Select', + component: Select, + decorators: [withCenteredStory, withKnobs], +}; -SelectStories.addDecorator(withCenteredStory).addDecorator(withKnobs); +const intialState: SelectableValue = { label: 'A label', value: 'A value' }; -SelectStories.add('default', () => { - const intialState: SelectableValue = { label: 'A label', value: 'A value' }; +const options = object>>('Options:', [ + intialState, + { label: 'Another label', value: 'Another value 1' }, + { label: 'Another label', value: 'Another value 2' }, + { label: 'Another label', value: 'Another value 3' }, + { label: 'Another label', value: 'Another value 4' }, + { label: 'Another label', value: 'Another value 5' }, + { label: 'Another label', value: 'Another value ' }, +]); + +export const basic = () => { const value = object>('Selected Value:', intialState); - const options = object>>('Options:', [ - intialState, - { label: 'Another label', value: 'Another value' }, - ]); return ( @@ -35,16 +43,11 @@ SelectStories.add('default', () => { }} ); -}); +}; -SelectStories.add('With allowCustomValue', () => { - const intialState: SelectableValue = { label: 'A label', value: 'A value' }; +export const withAllowCustomValue = () => { // @ts-ignore const value = object>('Selected Value:', null); - const options = object>>('Options:', [ - intialState, - { label: 'Another label', value: 'Another value' }, - ]); return ( @@ -64,4 +67,32 @@ SelectStories.add('With allowCustomValue', () => { }} ); -}); +}; + +export const asyncSelect = () => { + const [isLoading, setIsLoading] = useState(true); + const [value, setValue] = useState(); + const loadAsyncOptions = useCallback( + inputValue => { + return new Promise>>(resolve => { + setTimeout(() => { + setIsLoading(false); + resolve(options.filter(option => option.label && option.label.includes(inputValue))); + }, 1000); + }); + }, + [value] + ); + return ( + { + action('onChange')(value); + setValue(value); + }} + /> + ); +};