1
1
import { action } from '@storybook/addon-actions' ;
2
2
import { Story } from '@storybook/react' ;
3
3
import { Box , useTheme } from '@mui/material' ;
4
- import React from 'react' ;
4
+ import React , { useCallback } from 'react' ;
5
5
import Autocomplete , { ValueType , Props as AutocompleteProps } from '../components/Autocomplete/Autocomplete' ;
6
6
7
7
export default {
@@ -32,10 +32,71 @@ const Template: Story<AutocompleteProps> = (args) => {
32
32
) ;
33
33
} ;
34
34
35
+ const AsyncTemplate : Story < AutocompleteProps > = ( args ) => {
36
+ const theme = useTheme ( ) ;
37
+
38
+ const [ open , setOpen ] = React . useState < boolean > ( false ) ;
39
+ const [ loading , setLoading ] = React . useState < boolean > ( false ) ;
40
+ const [ selected , setSelected ] = React . useState < ValueType > ( ) ;
41
+ const [ options , setOptions ] = React . useState < ValueType [ ] > ( [ ] ) ;
42
+
43
+ const sleep = ( duration : number ) : Promise < void > => {
44
+ return new Promise < void > ( ( resolve ) => {
45
+ setTimeout ( ( ) => {
46
+ resolve ( ) ;
47
+ } , duration ) ;
48
+ } ) ;
49
+ } ;
50
+
51
+ const load = useCallback ( async ( ) => {
52
+ setLoading ( true ) ;
53
+ await sleep ( 1e3 ) ;
54
+ setLoading ( false ) ;
55
+ setOptions ( [ ...args . options ] ) ;
56
+ } , [ setLoading , setOptions ] ) ;
57
+
58
+ const handleOpen = useCallback ( async ( ) => {
59
+ setOpen ( true ) ;
60
+ await load ( ) ;
61
+ } , [ setOpen , load ] ) ;
62
+
63
+ const handleClose = ( ) => {
64
+ setOpen ( false ) ;
65
+ setOptions ( [ ] ) ;
66
+ } ;
67
+
68
+ const handleChange = ( value : ValueType ) => {
69
+ action ( 'onChange' ) ( value ) ;
70
+ setSelected ( value ) ;
71
+ } ;
72
+
73
+ return (
74
+ < Box sx = { { marginTop : '30px' } } >
75
+ < Autocomplete
76
+ { ...args }
77
+ open = { open }
78
+ onOpen = { ( ) => void handleOpen ( ) }
79
+ onClose = { handleClose }
80
+ selected = { selected }
81
+ loading = { loading }
82
+ options = { options }
83
+ onChange = { handleChange }
84
+ sx = { {
85
+ backgroundColor : theme . palette . TwClrBaseWhite ,
86
+ width : '300px' ,
87
+ } }
88
+ />
89
+ </ Box >
90
+ ) ;
91
+ } ;
92
+
35
93
export const Default = Template . bind ( { } ) ;
36
94
37
95
export const Complex = Template . bind ( { } ) ;
38
96
97
+ export const AsyncDefault = AsyncTemplate . bind ( { } ) ;
98
+
99
+
39
100
Default . args = {
40
101
id : '1' ,
41
102
label : 'Test' ,
@@ -75,3 +136,11 @@ Complex.args = {
75
136
isEqual : ( option : any , value : any ) => option . value === value . value ,
76
137
tooltipTitle : 'Hello world!' ,
77
138
} ;
139
+
140
+ AsyncDefault . args = {
141
+ id : '3' ,
142
+ label : 'Test' ,
143
+ options : [ 'Test 1' , 'Test 2' , 'Hello' ] ,
144
+ freeSolo : true ,
145
+ errorText : '' ,
146
+ } ;
0 commit comments