-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathForm.stories.tsx
360 lines (330 loc) · 8.55 KB
/
Form.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import React, { useState } from 'react';
import styled from 'styled-components';
import { Story, Meta } from '@storybook/react';
import { mdiAccountCircleOutline, mdiRefresh } from '@mdi/js';
import { name, address, internet, company, phone, commerce, lorem } from 'faker';
import TextInput from '../TextInput';
import Button from '../Button';
import Card from '../Card';
import Divider from '../Divider';
import Dropdown from '../Dropdown';
import Modal from '../Modal';
import Text from '../Text';
import Label from '../Label';
import colors from '../../enums/colors';
import variants from '../../enums/variants';
import Toggle from '../Toggle/Toggle';
// All 50 + DC
const stateAbbreviations = [
'AL',
'AK',
'AZ',
'AR',
'CA',
'CO',
'CT',
'DE',
'DC',
'FL',
'GA',
'HI',
'ID',
'IL',
'IN',
'IA',
'KS',
'KY',
'LA',
'ME',
'MD',
'MA',
'MI',
'MN',
'MS',
'MO',
'MT',
'NE',
'NV',
'NH',
'NJ',
'NM',
'NY',
'NC',
'ND',
'OH',
'OK',
'OR',
'PA',
'RI',
'SC',
'SD',
'TN',
'TX',
'UT',
'VT',
'VA',
'WA',
'WV',
'WI',
'WY',
];
interface state {
firstName: string;
lastName: string;
title: string;
city: string;
state: string;
phone: string;
email: string;
company: string;
department: string;
notifications: boolean;
bio: string;
age: number;
}
const defaultState: state = {
firstName: name.firstName(),
lastName: name.lastName(),
title: name.title(),
city: address.city(),
state: address.stateAbbr(),
phone: phone.phoneNumber(),
age: Math.ceil(Math.random() * 50 + 18),
email: internet.email(),
company: company.companyName(),
department: commerce.department(),
notifications: false,
bio: lorem.paragraph(5),
};
// Adjusting the style of the footer to help position the buttons added
const StyledFooter = styled(Card.Footer)`
display: flex;
justify-content: space-between;
`;
const StyledBody = styled(Card.Body)`
display: grid;
grid-template-columns: 1fr 1fr;
column-gap: 1rem;
row-gap: 1.5rem;
`;
const ResetButtonContainer = styled(Button.Container)`
margin-right: 1.5rem;
`;
export const ControlledForm: Story = () => {
const [state, setState] = useState(defaultState);
const [isSaving, setIsSaving] = useState(false);
const [isResetting, setIsResetting] = useState(false);
const [savedState, setSavedState] = useState(defaultState);
const [isModalOpen, setIsModalOpen] = useState(false);
// By creating a callback function like this, we will create a new callback for each
// handler on every render, which is not the ideal scenario for maximum performance.
// To prevent this, use the useCallback helper. We're doing this to shorten the length
// of the example's source code.
const createTextInputCallback = (property: string): ((event: any) => void) => {
return event => {
setState({ ...state, [property]: event.target.value });
};
};
const onSave = () => {
const newSavedState = { ...state };
setIsSaving(true);
// Use a setTimeout to simulate a network call
setTimeout(() => {
setSavedState(newSavedState);
setIsSaving(false);
}, Math.random() * 1000);
};
const onReset = () => {
setIsResetting(true);
setIsModalOpen(false);
// Simulate network call
setTimeout(() => {
setIsResetting(false);
setState({ ...savedState });
}, Math.random() * 1000 + 500);
};
const closeModal = () => {
setIsModalOpen(false);
};
const openModal = () => {
setIsModalOpen(true);
};
const saveButton = (
<Button
key="saveButton"
onClick={onSave}
color={colors.primaryDark}
isProcessing={isSaving}
type={Button.ButtonTypes.submit}
>
{isSaving ? 'Saving' : 'Save'}
</Button>
);
const cancelButton = (
<Button
key="cancelButton"
onClick={openModal}
color={colors.destructive}
isProcessing={isResetting}
variant={variants.text}
>
Reset
</Button>
);
const confirmButton = (
<Button
key="confirmButton"
onClick={onReset}
color={colors.destructive}
iconPrefix={mdiRefresh}
type={Button.ButtonTypes.reset}
>
Reset form
</Button>
);
const abortButton = (
<Button
key="cancelButton"
onClick={closeModal}
color={colors.destructive}
variant={variants.text}
StyledContainer={ResetButtonContainer}
type={Button.ButtonTypes.button}
>
Go back
</Button>
);
const Header = (
<>
<Text key="headerText" iconPrefix={mdiAccountCircleOutline}>
Edit Your Profile
</Text>
<Divider width="100%" />
</>
);
return (
<>
<Card
elevation={1}
header={Header}
footer={[cancelButton, saveButton]}
StyledFooter={StyledFooter}
StyledBody={StyledBody}
>
<Label
labelText="First Name"
htmlFor="firstName"
isValid={state.firstName !== ''}
isRequired
key="firstName"
>
<TextInput
onChange={createTextInputCallback('firstName')}
value={state.firstName}
isValid={typeof state.firstName !== 'undefined' && state.firstName.length > 0}
errorMessage="First Name cannot be blank"
id="firstName"
/>
</Label>
<Label labelText="Last Name" htmlFor="lastName" key="lastName">
<TextInput
onChange={createTextInputCallback('lastName')}
value={state.lastName}
id="lastName"
/>
</Label>
<Label
labelText="Age"
htmlFor="age"
isRequired
isValid={!!state.age && +state.age > 13}
key="age"
>
<TextInput
onChange={createTextInputCallback('age')}
value={`${state.age}`}
errorMessage="Must be 13+"
isValid={!!state.age && +state.age > 13}
id="age"
type="number"
/>
</Label>
<Label
labelText="Bio"
htmlFor="bio"
key="bio"
isRequired
isValid={!!state.bio && state.bio.length > 30}
>
<TextInput
onChange={createTextInputCallback('bio')}
value={state.bio}
id="bio"
isValid={!!state.bio && state.bio.length > 30}
errorMessage="Write a little more"
isMultiline
rows={3}
cols={25}
/>
</Label>
<Label labelText="Title" htmlFor="title" key="title">
<TextInput onChange={createTextInputCallback('title')} value={state.title} id="title" />
</Label>
<Label labelText="Company" htmlFor="company" key="company">
<TextInput
onChange={createTextInputCallback('company')}
value={state.company}
id="company"
/>
</Label>
<Label labelText="City" htmlFor="city" key="city">
<TextInput onChange={createTextInputCallback('city')} value={state.city} id="city" />
</Label>
<Label labelText="State" htmlFor="state" key="state">
<Dropdown
name="state-dropdown"
options={stateAbbreviations.map(abr => ({ id: abr, optionValue: abr }))}
color={colors.primaryDark}
values={[state.state]}
searchable
onSelect={val => {
setState({ ...state, state: `${val}` });
}}
variant={variants.fill}
/>
</Label>
<Label labelText="Notifications" htmlFor="notifications" key="notifications">
<Toggle
onToggle={() => {
setState({ ...state, notifications: !state.notifications });
}}
checked={state.notifications}
inputProps={{ onChange: () => {} }}
color={state.notifications ? '#8f8' : 'white'}
/>
{state.notifications ? ' Enabled' : ' Disabled'}
</Label>
</Card>
{isModalOpen && (
<Modal onClose={closeModal} onClickOutside={closeModal} backgroundDarkness={0.5}>
<Card
elevation={1}
header="Would you like to continue?"
footer={[abortButton, confirmButton]}
>
You will lose any unsaved changes, are you sure you would like to reset?
</Card>
</Modal>
)}
</>
);
};
export default {
title: 'Form Example',
parameters: {
design: {
type: 'figma',
url: 'https://www.figma.com/file/3r2G00brulOwr9j7F6JF59/Generic-UI-Style?node-id=0%3A1',
},
},
} as Meta;