-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathformik.stories.tsx
323 lines (311 loc) · 11.9 KB
/
formik.stories.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { isBefore, isValid, parseISO } from 'date-fns'
import React, { useState } from 'react'
import { Meta, StoryFn } from '@storybook/react'
import { Formik, Field as FormikField, FieldProps } from 'formik'
import { TextInput } from '../../components/TextInput'
import { TextArea } from '../../components/TextArea'
import { Radio } from '../../components/Radio'
import { Checkbox } from '../../components/Checkbox'
import { Autocomplete, AutocompleteOption } from '../../components/Autocomplete'
import { Button } from '../../components/Button'
import { NumberInput } from '../../components/NumberInput'
import { DatePicker, DatePickerOnChangeData } from '../../components/DatePicker'
import { Select, SelectOption } from '../../components/Select'
import { Switch, SwitchOption } from '../../components/Switch'
import { RangeSlider } from '../../components/RangeSlider'
import { sleep } from '../../helpers'
import { Field } from '../../components/Field'
import { Fieldset } from '../../components/Fieldset'
import { SectionDivider } from '../../components/SectionDivider'
import { EMPTY_FORM_VALUES, PREPOPULATED_FORM_VALUES } from '../constants'
import { FormValues } from '../types'
const MINIMUM_DATE = parseISO('2022-01-01')
const Example = ({ initialValues }: { initialValues: FormValues }) => {
const [formValues, setFormValues] = useState<FormValues>()
return (
<main>
<Formik<FormValues>
initialValues={initialValues}
validate={({ email, exampleDatePicker }) => {
const errors: Record<string, unknown> = {}
if (!email) {
errors.email = 'Required'
} else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(email)) {
errors.email = 'Invalid email address'
}
if (exampleDatePicker && !isValid(exampleDatePicker)) {
errors.exampleDatePicker = 'Enter a valid date'
}
if (exampleDatePicker && isBefore(exampleDatePicker, MINIMUM_DATE)) {
errors.exampleDatePicker = 'Enter a date on or after 1 January 2022'
}
return errors
}}
onSubmit={async (values: FormValues, { setSubmitting }) => {
await sleep(400)
setFormValues(values)
setSubmitting(false)
}}
>
{({ values, handleSubmit, isSubmitting, setFieldValue }) => (
<form onSubmit={handleSubmit}>
<SectionDivider title="Example Form" />
<FormikField name="email">
{({ field, meta }: FieldProps) => {
return (
<Field
hintText="Example hint text."
errors={[{ error: meta.touched && meta.error }]}
>
<TextInput
label="Email"
{...field}
data-testid="form-example-TextInput-email"
/>
</Field>
)
}}
</FormikField>
<FormikField name="password">
{({ field, meta }: FieldProps) => {
return (
<Field
hintText="Example hint text."
errors={[{ error: meta.touched && meta.error }]}
>
<TextInput
type="password"
label="Password"
{...field}
data-testid="form-example-TextInput-password"
/>
</Field>
)
}}
</FormikField>
<FormikField name="description">
{({ field, meta }: FieldProps) => {
return (
<Field
hintText="Example hint text."
errors={[{ error: meta.touched && meta.error }]}
>
<TextArea
label="Description"
{...field}
data-testid="form-example-TextArea-description"
/>
</Field>
)
}}
</FormikField>
<Fieldset legend="Example checkbox selection">
<FormikField
name="exampleCheckbox"
value="Option 1"
type="checkbox"
>
{({ field }: FieldProps) => (
<Checkbox label="Option 1" {...field} />
)}
</FormikField>
<FormikField
name="exampleCheckbox"
value="Option 2"
type="checkbox"
>
{({ field }: FieldProps) => (
<Checkbox label="Option 2" {...field} />
)}
</FormikField>
<FormikField
name="exampleCheckbox"
value="Option 3"
type="checkbox"
>
{({ field }: FieldProps) => (
<Checkbox label="Option 3" {...field} />
)}
</FormikField>
</Fieldset>
<Fieldset legend="Example radio selection">
<FormikField name="exampleRadio" value="Option 1" type="radio">
{({ field }: FieldProps) => (
<Radio label="Option 1" {...field} />
)}
</FormikField>
<FormikField name="exampleRadio" value="Option 2" type="radio">
{({ field }: FieldProps) => (
<Radio label="Option 2" {...field} />
)}
</FormikField>
</Fieldset>
<FormikField name="exampleSwitch">
{({ field: { name, value }, meta }: FieldProps) => {
return (
<Field
hintText="Example hint text."
errors={[{ error: meta.touched && meta.error }]}
>
<Switch
label="Example switch selection"
name={name}
value={value}
data-testid="form-example-Switch"
onChange={(event: React.FormEvent<HTMLInputElement>) => {
setFieldValue(
'exampleSwitch',
event.currentTarget.value
)
}}
>
<SwitchOption label="One" value="1" />
<SwitchOption label="Two" value="2" />
<SwitchOption label="Three" value="3" />
</Switch>
</Field>
)
}}
</FormikField>
<FormikField name="exampleNumberInput">
{({ field: { name, value }, meta }: FieldProps) => {
return (
<Field
hintText="Example hint text."
errors={[{ error: meta.touched && meta.error }]}
>
<NumberInput
label="Example number input"
name={name}
value={value}
onChange={(
_:
| React.ChangeEvent<HTMLInputElement>
| React.MouseEvent<HTMLButtonElement>
| React.KeyboardEvent<HTMLInputElement>,
newValue: number | null
) => {
setFieldValue('exampleNumberInput', newValue)
}}
/>
</Field>
)
}}
</FormikField>
<FormikField name="exampleDatePicker">
{({ field: { name, value }, meta }: FieldProps) => {
return (
<Field
hintText="Example hint text."
errors={[{ error: meta.touched && meta.error }]}
>
<DatePicker
name={name}
disabledDays={{ before: MINIMUM_DATE }}
startDate={value}
onChange={({ startDate }: DatePickerOnChangeData) => {
setFieldValue('exampleDatePicker', startDate)
}}
/>
</Field>
)
}}
</FormikField>
<FormikField name="exampleSelect">
{({ field: { value }, meta }: FieldProps) => {
return (
<Field
hintText="Example hint text."
errors={[{ error: meta.touched && meta.error }]}
>
<Select
value={value}
label="Example select"
onChange={(newValue: string | null) => {
setFieldValue('exampleSelect', newValue)
}}
>
<SelectOption value="one">One</SelectOption>
<SelectOption value="two">Two</SelectOption>
<SelectOption value="three">Three</SelectOption>
<SelectOption value="four">Four</SelectOption>
</Select>
</Field>
)
}}
</FormikField>
<FormikField name="exampleAutocomplete">
{({ field: { value }, meta }: FieldProps) => {
return (
<Field
hintText="Example hint text."
errors={[{ error: meta.touched && meta.error }]}
>
<Autocomplete
value={value}
label="Example autocomplete"
onChange={(newValue: string | null) => {
setFieldValue('exampleAutocomplete', newValue)
}}
>
<AutocompleteOption value="one">One</AutocompleteOption>
<AutocompleteOption value="two">Two</AutocompleteOption>
<AutocompleteOption value="three">
Three
</AutocompleteOption>
<AutocompleteOption value="four">Four</AutocompleteOption>
</Autocomplete>
</Field>
)
}}
</FormikField>
<FormikField name="exampleRangeSlider">
{({ meta }: FieldProps) => {
return (
<Field
hintText="Example hint text."
errors={[{ error: meta.touched && meta.error }]}
>
<RangeSlider
domain={[0, 40]}
mode={1}
values={values.exampleRangeSlider}
tracksLeft
step={2}
onChange={(newValues: ReadonlyArray<number>) => {
setFieldValue('exampleRangeSlider', newValues)
}}
/>
</Field>
)
}}
</FormikField>
<Button
type="submit"
isDisabled={isSubmitting}
data-testid="form-example-submit"
>
Submit
</Button>
</form>
)}
</Formik>
<pre data-testid="form-example-values">
{JSON.stringify(formValues, null, 2)}
</pre>
</main>
)
}
export default {
title: 'Forms/Usage/Formik',
component: Example,
} as Meta<typeof Example>
const Template: StoryFn<typeof Example> = (args) => <Example {...args} />
export const Default = Template.bind({})
Default.args = {
initialValues: EMPTY_FORM_VALUES,
}
export const Prepopulated = Template.bind({})
Prepopulated.args = {
initialValues: PREPOPULATED_FORM_VALUES,
}