1
1
import { useState } from ' react' ;
2
2
import { Meta , Story , Canvas , ArgsTable } from ' @storybook/addon-docs' ;
3
3
import { Information } from ' @strapi/icons' ;
4
- import { ToggleInput , Tooltip } from ' @strapi/design-system' ;
4
+ import { ToggleInput , Tooltip , Flex , Button } from ' @strapi/design-system' ;
5
5
6
6
<Meta title = " Design System/Components/ToggleInput" component = { ToggleInput } />
7
7
8
8
# ToggleInput
9
9
10
- This is the doc of the ` ToggleInput ` component
11
-
12
10
[ View source] ( https://github.com/strapi/design-system/tree/main/packages/strapi-design-system/src/ToggleInput )
13
11
14
- ## Imports
12
+ ## Usage
15
13
16
14
``` js
17
15
import { ToggleInput } from ' @strapi/design-system' ;
18
16
```
19
17
20
- ## Usage
18
+ ### Basic
19
+
20
+ By default, the checked state is ` false ` and uncontrolled.
21
21
22
22
<Canvas >
23
23
<Story name = " base" >
24
24
{ () => {
25
- const [checked, setChecked] = useState (true );
25
+ const [error, toggleError] = useState ();
26
+ const [disabled, toggleDisabled] = useState ();
26
27
return (
27
- <ToggleInput
28
- hint = " Enable provider"
29
- label = " Enabled"
30
- name = " enable-provider"
31
- labelAction = {
32
- <Tooltip description = " Content of the tooltip" >
33
- <button
34
- aria-label = " Information about the email"
35
- style = { { border: ' none' , padding: 0 , background: ' transparent' }}
36
- >
37
- <Information aria-hidden />
38
- </button >
39
- </Tooltip >
40
- }
41
- onLabel = " True"
42
- offLabel = " False"
43
- checked = { checked }
44
- onChange = { (e ) => setChecked (e .target .checked )}
45
- />
28
+ <Flex direction = " column" alignItems = " stretch" gap = { 11 } >
29
+ <ToggleInput
30
+ label = " Enabled"
31
+ onLabel = " True"
32
+ offLabel = " False"
33
+ hint = " Enable your SSO provider for users"
34
+ disabled = { disabled }
35
+ error = { error }
36
+ />
37
+ <Flex gap = { 2 } justifyContent = " center" >
38
+ <Button
39
+ variant = " danger-light"
40
+ onClick = { () => toggleError ((s ) => (s ? undefined : " That's not a boolean option!" ))}
41
+ >
42
+ { ` ${error ? ' Hide' : ' Show' } the error state ` }
43
+ </Button >
44
+ <Button variant = " tertiary" onClick = { () => toggleDisabled ((s ) => ! s )} >
45
+ { ` ${disabled ? ' Hide' : ' Show' } the disabled state ` }
46
+ </Button >
47
+ </Flex >
48
+ </Flex >
46
49
);
47
50
}}
48
51
</Story >
49
52
</Canvas >
50
53
51
- ### Error state
52
-
53
- <Canvas >
54
- <Story name = " error" >
55
- { () => {
56
- const [checked, setChecked] = useState (false );
57
- return (
58
- <ToggleInput
59
- hint = " Enable provider"
60
- error = " this field is required"
61
- label = " Label"
62
- name = " enable-provider"
63
- onLabel = " True"
64
- offLabel = " False"
65
- checked = { checked }
66
- onChange = { (e ) => setChecked (e .target .checked )}
67
- />
68
- );
69
- }}
70
- </Story >
71
- </Canvas >
54
+ ### Controlled
72
55
73
- ### Required state
56
+ By passing an ` onChange ` handler and a ` checked ` value, the component becomes controlled.
57
+ Instead of returning the value, the ` onChange ` handler recieves the ` ChangeEvent<HTMLInputElement> ` .
74
58
75
59
<Canvas >
76
- <Story name = " required " >
60
+ <Story name = " controlled " >
77
61
{ () => {
78
62
const [checked, setChecked] = useState (false );
79
63
return (
80
64
<ToggleInput
81
- hint = " Enable provider"
82
- label = " Label"
83
- name = " enable-provider"
65
+ label = " Enabled"
84
66
onLabel = " True"
85
67
offLabel = " False"
86
68
checked = { checked }
87
69
onChange = { (e ) => setChecked (e .target .checked )}
88
- required
89
70
/>
90
71
);
91
72
}}
@@ -94,84 +75,31 @@ import { ToggleInput } from '@strapi/design-system';
94
75
95
76
### Size S
96
77
97
- <Canvas >
98
- <Story name = " size S" >
99
- { () => {
100
- const [checked, setChecked] = useState (false );
101
- return (
102
- <ToggleInput
103
- size = " S"
104
- hint = " Enable provider"
105
- label = " Label"
106
- name = " enable-provider"
107
- onLabel = " True"
108
- offLabel = " False"
109
- checked = { checked }
110
- onChange = { (e ) => setChecked (e .target .checked )}
111
- />
112
- );
113
- }}
114
- </Story >
115
- </Canvas >
116
-
117
- ### Null value
78
+ Similar to other inputs, we support a ` small ` size.
118
79
119
80
<Canvas >
120
- <Story name = " null-value " >
81
+ <Story name = " size S " >
121
82
{ () => {
122
- const [checked, setChecked] = useState (null );
123
- return (
124
- <ToggleInput
125
- hint = " Enable provider"
126
- label = " Label"
127
- name = " enable-provider"
128
- onLabel = " True"
129
- offLabel = " False"
130
- checked = { checked }
131
- onChange = { (e ) => setChecked (e .target .checked )}
132
- clearLabel = " clear"
133
- onClear = { () => setChecked (null )}
134
- />
135
- );
83
+ return <ToggleInput size = " S" label = " Enabled" onLabel = " True" offLabel = " False" />;
136
84
}}
137
85
</Story >
138
86
</Canvas >
139
87
140
- ### Clear value
88
+ ### Using ` null ` values
141
89
142
- <Canvas >
143
- <Story name = " clear-value" >
144
- { () => {
145
- const [checked, setChecked] = useState (true );
146
- return (
147
- <ToggleInput
148
- hint = " Enable provider"
149
- label = " Label"
150
- name = " enable-provider"
151
- onLabel = " True"
152
- offLabel = " False"
153
- checked = { checked }
154
- onChange = { (e ) => setChecked (e .target .checked )}
155
- clearLabel = " clear"
156
- onClear = { () => setChecked (null )}
157
- />
158
- );
159
- }}
160
- </Story >
161
- </Canvas >
90
+ Where desirable you can supply a ` null ` value to the component. This may be helpful in
91
+ circumstances such whether or not a restaurant is open – ` true ` / ` false ` / not yet specified (` null ` ).
162
92
163
- ### Disabled
93
+ It's additionally possible to "clear" the value by passing an ` onClear ` handler and a ` clearLabel ` .
94
+ This label _ will not show_ if the value is ` null ` .
164
95
165
96
<Canvas >
166
- <Story name = " disabled " >
97
+ <Story name = " nullish " >
167
98
{ () => {
168
- const [checked, setChecked] = useState (true );
99
+ const [checked, setChecked] = useState (null );
169
100
return (
170
101
<ToggleInput
171
- disabled
172
- hint = " Enable provider"
173
- label = " Label"
174
- name = " enable-provider"
102
+ label = " Enabled"
175
103
onLabel = " True"
176
104
offLabel = " False"
177
105
checked = { checked }
0 commit comments