/** ******************************************************************************************************************* 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, { useState, useMemo } from 'react'; import { actions } from '@storybook/addon-actions'; import Box from '../../layouts/Box'; import Select, { SelectOption } from '.'; import { useCallback } from 'react'; export default { component: Select, title: 'Components/Select', }; const options: SelectOption[] = [ { label: 'Option 1', value: '1', }, { label: 'Option 2', value: '2', }, { label: 'Option 3', value: '3', }, { label: 'Option 4', value: '4', disabled: true, }, ]; const optionsWithIcons = [ { label: 'Option 1', value: '1', iconName: 'copy' as const, }, { label: 'Option 2', value: '2', iconName: 'add_plus' as const, }, { label: 'Option 3', value: '3', iconName: 'refresh' as const, }, { label: 'Option 4', value: '4', disabled: true, iconName: 'settings' as const, }, ]; const optionsWithGroup = [ ...options, { label: 'Group One', options: [ { label: 'Option 5', value: '5', }, { label: 'Option 6', value: '6', disabled: true, }, ], }, { label: 'Group Two', disabled: true, options: [ { label: 'Option 7', value: '7', }, { label: 'Option 8', value: '8', }, ], }, ]; export const Default = () => { const [selectedOption, setSelectedOption] = useState({ value: '' }); const onChange = (event: React.ChangeEvent<{ value: any }>) => { setSelectedOption({ value: String(event.target.value) }); }; return ( ); }; export const WithIcon = () => { const [selectedOption, setSelectedOption] = useState({ value: '' }); const onChange = (event: React.ChangeEvent<{ value: any }>) => { setSelectedOption({ value: String(event.target.value) }); }; return ( ); }; export const WithOptionGroups = () => { const [selectedOption, setSelectedOption] = useState({ value: '' }); const onChange = (event: React.ChangeEvent<{ value: any }>) => { setSelectedOption({ value: String(event.target.value) }); }; return ( ); }; export const PlaceholderWithExtraAriaTags = () => { const [selectedOption, setSelectedOption] = useState({ value: '' }); const onChange = (event: React.ChangeEvent<{ value: any }>) => { setSelectedOption({ value: String(event.target.value) }); }; return ( Description of the select ); }; export const EmptyOption = () => ( ); export const Disabled = () => ( ); export const Invalid = () => ( ); export const LoadingStatus = () => { return ( ); }; export const ErrorStatus = () => { const actionNames = actions('onRecoveryClick'); return ( ); }; export const FetchOptionsFromAPI = () => { const [loading, setLoading] = useState(false); const [_options, setOptions] = useState([]); const onFocus = () => { setLoading(true); setTimeout(() => { setLoading(false); setOptions(options); }, 1000); }; const onBlur = () => { setLoading(false); }; return ( ); }; const customOptions = [ { id: 1, name: 'Name 1', description: 'Description 1', value: '1', }, { id: 2, name: 'Name 2', description: 'Description 2', value: '2', }, { id: 3, name: 'Name 3', description: 'Description 3', value: '3', }, { id: 4, name: 'Name 4', description: 'Description 4', value: '4', }, ]; export const RenderCustomLabel = () => { const [selectedOption, setSelectedOption] = useState({ value: '1' }); const onChange = (event: React.ChangeEvent<{ value: any }>) => { setSelectedOption({ value: String(event.target.value) }); }; const options = 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} ); }, []); return ( ); };