1
1
import { useState , useEffect } from ' react' ;
2
2
import { Meta , Story , Canvas , ArgsTable } from ' @storybook/addon-docs' ;
3
- import { Combobox , CreatableCombobox , ComboboxOption } from ' @strapi/design-system' ;
3
+ import {
4
+ Combobox ,
5
+ CreatableCombobox ,
6
+ ComboboxOption ,
7
+ Flex ,
8
+ Button ,
9
+ SingleSelect ,
10
+ SingleSelectOption ,
11
+ } from ' @strapi/design-system' ;
4
12
5
13
<Meta title = " Design System/Components/Combobox" component = { Combobox } />
6
14
@@ -21,13 +29,51 @@ import { Combobox, ComboboxOption, CreatableCombobox } from '@strapi/design-syst
21
29
22
30
### Basic Usage
23
31
24
- By default, the combobox uses both inline and list autocomplete, that is where as the user types suggestions
32
+ By default, the combobox is uncontrolled & uses both inline and list autocomplete, that is where as the user types suggestions
25
33
(based on the order of the options) are shown in the input field whilst the list gradually gets filtered.
26
34
27
35
<Canvas >
28
36
<Story name = " basic" >
29
37
{ () => {
30
- const [value, setValue] = useState (' ' );
38
+ const [error, toggleError] = useState ();
39
+ const [disabled, toggleDisabled] = useState ();
40
+ return (
41
+ <Flex direction = " column" alignItems = " stretch" gap = { 11 } >
42
+ <Combobox placeholder = " My favourite fruit is..." label = " Fruits" error = { error } disabled = { disabled } >
43
+ <ComboboxOption value = " apple" >Apple</ComboboxOption >
44
+ <ComboboxOption value = " avocado" >Avocado</ComboboxOption >
45
+ <ComboboxOption value = " banana" >Banana</ComboboxOption >
46
+ <ComboboxOption value = " kiwi" >Kiwi</ComboboxOption >
47
+ <ComboboxOption value = " mango" >Mango</ComboboxOption >
48
+ <ComboboxOption value = " orange" >Orange</ComboboxOption >
49
+ <ComboboxOption value = " strawberry" >Strawberry</ComboboxOption >
50
+ </Combobox >
51
+ <Flex gap = { 2 } justifyContent = " center" >
52
+ <Button
53
+ variant = " danger-light"
54
+ onClick = { () => toggleError ((s ) => (s ? undefined : ' Oh no, the fruits have gone mouldy!' ))}
55
+ >
56
+ { ` ${error ? ' Hide' : ' Show' } the error state ` }
57
+ </Button >
58
+ <Button variant = " tertiary" onClick = { () => toggleDisabled ((s ) => ! s )} >
59
+ { ` ${disabled ? ' Hide' : ' Show' } the disabled state ` }
60
+ </Button >
61
+ </Flex >
62
+ </Flex >
63
+ );
64
+ }}
65
+ </Story >
66
+ </Canvas >
67
+
68
+ ### Controlled
69
+
70
+ The combobox can be a controlled component by passing a ` value ` prop and an ` onChange ` callback, this also
71
+ enables you to have the ability to "clear" the input.
72
+
73
+ <Canvas >
74
+ <Story name = " controlled" >
75
+ { () => {
76
+ const [value, setValue] = useState ();
31
77
return (
32
78
<Combobox
33
79
placeholder = " My favourite fruit is..."
@@ -154,6 +200,46 @@ still pass an `onCreateOption` callback to handle the creation of the new option
154
200
</Story >
155
201
</Canvas >
156
202
203
+ ### Autocomplete settings
204
+
205
+ By default, the combobox uses both inline and list autocomplete, however you can change this behaviour
206
+ to only be ` list ` or ` none ` . If you set the autocomplete mode to ` none ` , the first matching option will
207
+ be visually focussed
208
+
209
+ <Canvas >
210
+ <Story name = " autocomplete" >
211
+ { () => {
212
+ const [value, setValue] = useState (' ' );
213
+ const [autocompleteMode, setAutocompleteMode] = useState (' both' );
214
+ return (
215
+ <Flex direction = " column" alignItems = " stretch" gap = { 11 } >
216
+ <Combobox
217
+ placeholder = " My favourite fruit is..."
218
+ label = " Fruits"
219
+ value = { value }
220
+ onChange = { setValue }
221
+ autocomplete = { autocompleteMode }
222
+ onClear = { () => setValue (' ' )}
223
+ >
224
+ <ComboboxOption value = " apple" >Apple</ComboboxOption >
225
+ <ComboboxOption value = " avocado" >Avocado</ComboboxOption >
226
+ <ComboboxOption value = " banana" >Banana</ComboboxOption >
227
+ <ComboboxOption value = " kiwi" >Kiwi</ComboboxOption >
228
+ <ComboboxOption value = " mango" >Mango</ComboboxOption >
229
+ <ComboboxOption value = " orange" >Orange</ComboboxOption >
230
+ <ComboboxOption value = " strawberry" >Strawberry</ComboboxOption >
231
+ </Combobox >
232
+ <SingleSelect label = " Autocomplete Mode" value = { autocompleteMode } onValueChange = { setAutocompleteMode } >
233
+ <SingleSelectOption value = " both" >both</SingleSelectOption >
234
+ <SingleSelectOption value = " list" >list</SingleSelectOption >
235
+ <SingleSelectOption value = " none" >none</SingleSelectOption >
236
+ </SingleSelect >
237
+ </Flex >
238
+ );
239
+ }}
240
+ </Story >
241
+ </Canvas >
242
+
157
243
## Props
158
244
159
245
### Combobox
0 commit comments