/** ******************************************************************************************************************* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * ******************************************************************************************************************** */ import React, { useMemo, useCallback } from 'react'; import { action } from '@storybook/addon-actions'; import DnsOutlined from '@material-ui/icons/DnsOutlined'; import Computer from '@material-ui/icons/Computer'; import Box from '../../layouts/Box'; import Autosuggest, { SelectOption, StatusType, FilterOptionsState } from '.'; import FormField from '../FormField'; import { awsServices, groupedAwsServices } from './data/data'; export default { component: Autosuggest, title: 'Components/Autosuggest', }; function sleep(delay = 0) { return new Promise((resolve) => { setTimeout(resolve, delay); }); } export const Default = () => ( ); export const WithValue = () => ( ); export const Placeholder = () => ( ); export const Groups = () => ( ); export const AsyncLoading = () => { const [shouldLoad, setLoadingStatus] = React.useState(false); const [status, setStatus] = React.useState('finished'); const [options, setOptions] = React.useState([]); React.useEffect(() => { const loading = shouldLoad && options.length === 0; if (loading) { (async () => { setStatus('loading'); await sleep(1e3); setStatus('finished'); setOptions(awsServices); setLoadingStatus(false); })(); } }, [shouldLoad, options.length]); return ( { setOptions([]); setLoadingStatus(true); action('onFocus')(e); }} onBlur={action('onBlur')} statusType={status} options={options} empty="No matching service found" onChange={action('onChange')} /> ); }; export const AsyncWithError = () => { const [shouldLoad, setLoadingStatus] = React.useState(false); const [status, setStatus] = React.useState('finished'); React.useEffect(() => { if (shouldLoad) { (async () => { setStatus('loading'); await sleep(1e3); // For demo purposes. setStatus('error'); setLoadingStatus(false); })(); } }, [shouldLoad]); return ( { setLoadingStatus(true); }} onRecoveryClick={(e) => { setLoadingStatus(true); }} statusType={status} recoveryText="Retry" empty="No matching service found" /> ); }; export const WithoutIcon = () => ( ); export const WithCustomIcon = () => ( ); const customOptions = [ { id: 1, name: 'Name 1', description: 'Description 1', }, { id: 2, name: 'Name 2', description: 'Description 2', }, { id: 3, name: 'Name 3', description: 'Description 3', }, { id: 4, name: 'Name 4', description: 'Description 4', }, ]; interface CustomOptionType extends SelectOption { id: number; name: string; description: string; } export const RenderCustomLabel = () => { const options: CustomOptionType[] = useMemo(() => { return customOptions.map((o) => ({ ...o, label: `${o.id}-${o.name}`, value: o.id.toString(), })); }, []); const renderOption = useCallback((o) => { return ( {o.id}-{o.name} {o.description} ); }, []); const filterOptions = useCallback((options: SelectOption[], state: FilterOptionsState) => { if (state?.inputValue) { return (options as CustomOptionType[]).filter( (o) => o.label.toLowerCase().indexOf(state.inputValue.toLowerCase()) >= 0 || o.name.toLowerCase().indexOf(state.inputValue.toLowerCase()) >= 0 || o.description.toLowerCase().indexOf(state.inputValue.toLowerCase()) >= 0 ); } return options; }, []); return ( ); }; export const WithFreeSolo = () => ( );