@workday/canvas-kit-docs 16.1.2 → 16.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,30 +1,38 @@
1
1
  import React from 'react';
2
2
 
3
- import {FormField, FormFieldGroup} from '@workday/canvas-kit-react/form-field';
3
+ import {Switch} from '@workday/canvas-kit-preview-react/switch';
4
+ import {useUniqueId} from '@workday/canvas-kit-react/common';
5
+ import {FormField} from '@workday/canvas-kit-react/form-field';
4
6
  import {SegmentedControl} from '@workday/canvas-kit-react/segmented-control';
5
7
  import {Select} from '@workday/canvas-kit-react/select';
6
- import {Switch} from '@workday/canvas-kit-react/switch';
7
- import {Heading, Text} from '@workday/canvas-kit-react/text';
8
+ import {Heading, Subtext, Text} from '@workday/canvas-kit-react/text';
8
9
  import {TextInput} from '@workday/canvas-kit-react/text-input';
9
10
  import {calc, createStencil, createStyles, px2rem} from '@workday/canvas-kit-styling';
10
11
  import {base, system} from '@workday/canvas-tokens-web';
11
12
 
13
+ type DensityLevel = 'high' | 'medium' | 'low';
14
+ type LabelOrientation = 'vertical' | 'horizontalStart' | 'horizontalEnd';
15
+ type ContainerAlignment = 'left' | 'center';
16
+ type DensityTypeLevel = 'subtext.medium' | 'subtext.large' | 'body.small';
17
+
18
+ const densitySummary: Record<
19
+ DensityLevel,
20
+ {inputHeight: string; stackGap: string; typeLevel: DensityTypeLevel}
21
+ > = {
22
+ high: {inputHeight: '32px', stackGap: '16px', typeLevel: 'subtext.medium'},
23
+ medium: {inputHeight: '40px', stackGap: '24px', typeLevel: 'subtext.large'},
24
+ low: {inputHeight: '48px', stackGap: '32px', typeLevel: 'body.small'},
25
+ };
26
+
12
27
  const formStyles = createStyles({
13
28
  margin: `${px2rem(12)} ${'0'}`,
14
29
  maxWidth: px2rem(600),
15
30
  minWidth: '0',
16
31
  });
17
32
 
18
- const formFieldGroupListStyles = createStyles({
19
- display: 'inline-flex',
20
- flexDirection: 'row',
21
- flexWrap: 'wrap',
22
- });
23
-
24
33
  const sideBySideInputs = createStencil({
25
34
  base: {
26
35
  display: 'inline-flex',
27
- gap: system.gap.sm,
28
36
  justifyContent: 'space-between',
29
37
  },
30
38
  modifiers: {
@@ -39,52 +47,21 @@ const sideBySideInputs = createStencil({
39
47
  },
40
48
  vertical: {
41
49
  display: 'inline-flex',
50
+ flexDirection: 'row',
42
51
  },
43
52
  },
44
53
  density: {
45
- high: {},
46
- medium: {},
47
- low: {},
48
- },
49
- },
50
- compound: [
51
- {
52
- modifiers: {labelOrientation: 'horizontalStart', density: 'high'},
53
- styles: {
54
- gap: system.gap.md,
55
- },
56
- },
57
- {
58
- modifiers: {labelOrientation: 'horizontalStart', density: 'medium'},
59
- styles: {
60
- gap: system.gap.lg,
61
- },
62
- },
63
- {
64
- modifiers: {labelOrientation: 'horizontalStart', density: 'low'},
65
- styles: {
66
- gap: system.gap.xl,
67
- },
68
- },
69
- {
70
- modifiers: {labelOrientation: 'horizontalEnd', density: 'high'},
71
- styles: {
54
+ high: {
72
55
  gap: system.gap.md,
73
56
  },
74
- },
75
- {
76
- modifiers: {labelOrientation: 'horizontalEnd', density: 'medium'},
77
- styles: {
57
+ medium: {
78
58
  gap: system.gap.lg,
79
59
  },
80
- },
81
- {
82
- modifiers: {labelOrientation: 'horizontalEnd', density: 'low'},
83
- styles: {
60
+ low: {
84
61
  gap: system.gap.xl,
85
62
  },
86
63
  },
87
- ],
64
+ },
88
65
  });
89
66
 
90
67
  const zipCodeInput = createStyles({
@@ -96,7 +73,10 @@ const zipCodeContainerStyles = createStyles({
96
73
  });
97
74
 
98
75
  const formFieldStencil = createStencil({
99
- base: {},
76
+ base: {
77
+ // FormField's built-in margin would double-space with the stack gap below.
78
+ margin: 0,
79
+ },
100
80
  modifiers: {
101
81
  density: {
102
82
  high: {
@@ -155,32 +135,6 @@ const formFieldStencil = createStencil({
155
135
  ],
156
136
  });
157
137
 
158
- const selectStencil = createStencil({
159
- base: {},
160
- modifiers: {
161
- density: {
162
- high: {
163
- height: system.size.sm,
164
- padding: `${system.padding.xxs} ${system.padding.xs}`,
165
- '& + div': {
166
- height: system.size.sm,
167
- },
168
- },
169
- medium: {
170
- height: system.size.md,
171
- padding: `${system.padding.xs}`,
172
- },
173
- low: {
174
- height: calc.add(system.size.md, base.size100),
175
- padding: `${system.padding.sm} ${system.padding.xs}`,
176
- '& + div': {
177
- height: calc.add(system.size.md, base.size100),
178
- },
179
- },
180
- },
181
- },
182
- });
183
-
184
138
  const inputStencil = createStencil({
185
139
  base: {
186
140
  minWidth: px2rem(200),
@@ -190,29 +144,25 @@ const inputStencil = createStencil({
190
144
  high: {
191
145
  height: system.size.sm,
192
146
  padding: `${system.padding.xxs} ${system.padding.xs}`,
147
+ fontSize: system.legacy.fontSize.subtext.md,
148
+ lineHeight: system.legacy.lineHeight.subtext.md,
193
149
  },
194
150
  medium: {
195
151
  height: system.size.md,
196
152
  padding: `${system.padding.xs}`,
153
+ fontSize: system.legacy.fontSize.subtext.lg,
154
+ lineHeight: system.legacy.lineHeight.subtext.lg,
197
155
  },
198
-
199
156
  low: {
200
157
  height: calc.add(system.size.md, base.size100),
201
158
  padding: `${system.padding.sm} ${system.padding.xs}`,
159
+ fontSize: system.legacy.fontSize.body.sm,
160
+ lineHeight: system.legacy.lineHeight.body.sm,
202
161
  },
203
162
  },
204
163
  },
205
164
  });
206
165
 
207
- const creditCardInputStencil = createStencil({
208
- extends: inputStencil,
209
- base: {
210
- width: calc.add(system.size.md, system.size.md),
211
- minWidth: calc.add(system.size.md, system.size.md),
212
- textAlign: 'center',
213
- },
214
- });
215
-
216
166
  const flexContainerStencil = createStencil({
217
167
  base: {
218
168
  display: 'flex',
@@ -263,37 +213,44 @@ const optionItemStyles = createStyles({
263
213
  maxWidth: 'fit-content',
264
214
  });
265
215
 
266
- // high = 32px height on inputs, space between inputs is 16px
267
- // medium 40px height on inputs, space between inputs is 24px
268
- // low = 48px height on inputs, space between inputs is 32px
269
-
270
216
  export default () => {
271
- const [density, setDensity] = React.useState<'high' | 'medium' | 'low'>('medium');
272
- const [containerAlignment, setContainerAlignment] = React.useState<'left' | 'center'>('left');
273
- const [labelOrientation, setLabelOrientation] = React.useState<
274
- 'vertical' | 'horizontalStart' | 'horizontalEnd'
275
- >('vertical');
217
+ const [density, setDensity] = React.useState<DensityLevel>('medium');
218
+ const [containerAlignment, setContainerAlignment] = React.useState<ContainerAlignment>('left');
219
+ const [labelOrientation, setLabelOrientation] = React.useState<LabelOrientation>('vertical');
220
+ const [fastShipping, setFastShipping] = React.useState(false);
221
+ const densityLabelId = useUniqueId();
222
+ const containerAlignmentLabelId = useUniqueId();
223
+ const labelOrientationLabelId = useUniqueId();
224
+
225
+ const handleDensity = (data: {id: string}) => {
226
+ setDensity(data.id as DensityLevel);
227
+ };
276
228
 
277
- const handleDensity = data => {
278
- setDensity(data.id);
229
+ const handleContainerAlignment = (data: {id: string}) => {
230
+ setContainerAlignment(data.id as ContainerAlignment);
279
231
  };
280
232
 
281
- const handleContainerAlignment = data => {
282
- setContainerAlignment(data.id);
233
+ const handleLabelOrientation = (data: {id: string}) => {
234
+ setLabelOrientation(data.id as LabelOrientation);
283
235
  };
284
236
 
285
- const handleLabelOrientation = data => {
286
- setLabelOrientation(data.id);
237
+ const handleFastShipping = (event: React.ChangeEvent<HTMLInputElement>) => {
238
+ setFastShipping(event.target.checked);
287
239
  };
288
240
 
241
+ const labelTypeLevel = densitySummary[density].typeLevel;
242
+ const fieldCs = formFieldStencil({density, labelOrientation});
243
+ const inputCs = inputStencil({density});
244
+
289
245
  return (
290
- <div>
291
- <Heading size="small">Choose Your Density and Alignment</Heading>
246
+ <>
292
247
  <div className={optionStyles}>
293
248
  <div className={optionItemStyles}>
294
- <Text>Density</Text>
295
- <SegmentedControl onSelect={data => handleDensity(data)} size="small">
296
- <SegmentedControl.List aria-label="choose a density">
249
+ <Text as="h3" id={densityLabelId}>
250
+ Density
251
+ </Text>
252
+ <SegmentedControl initialValue={density} onSelect={handleDensity} size="small">
253
+ <SegmentedControl.List aria-label="Density" aria-labelledby={densityLabelId}>
297
254
  <SegmentedControl.Item data-id="high">High</SegmentedControl.Item>
298
255
  <SegmentedControl.Item data-id="medium">Medium</SegmentedControl.Item>
299
256
  <SegmentedControl.Item data-id="low">Low</SegmentedControl.Item>
@@ -301,9 +258,18 @@ export default () => {
301
258
  </SegmentedControl>
302
259
  </div>
303
260
  <div className={optionItemStyles}>
304
- <Text>Label Orientation</Text>
305
- <SegmentedControl onSelect={data => handleLabelOrientation(data)} size="small">
306
- <SegmentedControl.List aria-label="choose a label orientation">
261
+ <Text as="h3" id={labelOrientationLabelId}>
262
+ Label Orientation
263
+ </Text>
264
+ <SegmentedControl
265
+ initialValue={labelOrientation}
266
+ onSelect={handleLabelOrientation}
267
+ size="small"
268
+ >
269
+ <SegmentedControl.List
270
+ aria-label="Label Orientation"
271
+ aria-labelledby={labelOrientationLabelId}
272
+ >
307
273
  <SegmentedControl.Item data-id="vertical">Vertical</SegmentedControl.Item>
308
274
  <SegmentedControl.Item data-id="horizontalStart">
309
275
  Horizontal Start
@@ -313,131 +279,92 @@ export default () => {
313
279
  </SegmentedControl>
314
280
  </div>
315
281
  <div className={optionItemStyles}>
316
- <Text>Container Alignment</Text>
317
- <SegmentedControl onSelect={data => handleContainerAlignment(data)} size="small">
318
- <SegmentedControl.List aria-label="choose a density">
282
+ <Text as="h3" id={containerAlignmentLabelId}>
283
+ Container Alignment
284
+ </Text>
285
+ <SegmentedControl
286
+ initialValue={containerAlignment}
287
+ onSelect={handleContainerAlignment}
288
+ size="small"
289
+ >
290
+ <SegmentedControl.List
291
+ aria-label="Container Alignment"
292
+ aria-labelledby={containerAlignmentLabelId}
293
+ >
319
294
  <SegmentedControl.Item data-id="left">Left</SegmentedControl.Item>
320
295
  <SegmentedControl.Item data-id="center">Center</SegmentedControl.Item>
321
296
  </SegmentedControl.List>
322
297
  </SegmentedControl>
323
298
  </div>
299
+ <Subtext size="large">
300
+ Input height: {densitySummary[density].inputHeight}. Field spacing:{' '}
301
+ {densitySummary[density].stackGap}. Type: {densitySummary[density].typeLevel}.
302
+ </Subtext>
324
303
  </div>
325
304
 
326
305
  <div {...containerAlignmentStencil({alignment: containerAlignment})}>
327
- <form action="#" className={formStyles}>
306
+ <form className={formStyles} autoComplete="off">
307
+ <Heading size="small">Shipping Address</Heading>
328
308
  <div {...flexContainerStencil({density})}>
329
- <FormField
330
- orientation={labelOrientation}
331
- grow
332
- cs={formFieldStencil({density, labelOrientation})}
333
- id="foo"
334
- >
335
- <FormField.Label>Choose Country</FormField.Label>
336
- <Select items={['Dominican Republic', 'Spain', 'United States']}>
337
- <FormField.Input
338
- cs={selectStencil({density})}
339
- placeholder="Choose a country"
340
- as={Select.Input}
341
- />
342
- <Select.Popper>
343
- <Select.Card>
344
- <Select.List>{item => <Select.Item>{item}</Select.Item>}</Select.List>
345
- </Select.Card>
346
- </Select.Popper>
347
- </Select>
348
- </FormField>
349
- <FormField
350
- grow
351
- orientation={labelOrientation}
352
- cs={formFieldStencil({density, labelOrientation})}
353
- >
354
- <FormField.Label>Full Name</FormField.Label>
355
- <FormField.Input as={TextInput} cs={inputStencil({density})} />
309
+ <FormField grow orientation={labelOrientation} cs={fieldCs}>
310
+ <FormField.Label typeLevel={labelTypeLevel}>Full Name</FormField.Label>
311
+ <FormField.Input as={TextInput} autoComplete="name" cs={inputCs} />
356
312
  </FormField>
357
- <FormField
358
- grow
359
- orientation={labelOrientation}
360
- cs={formFieldStencil({density, labelOrientation})}
361
- >
362
- <FormField.Label>Phone Number</FormField.Label>
363
- <FormField.Input as={TextInput} cs={inputStencil({density})} />
364
- </FormField>
365
- <FormField
366
- grow
367
- orientation={labelOrientation}
368
- cs={formFieldStencil({density, labelOrientation})}
369
- >
370
- <FormField.Label>Street Address</FormField.Label>
371
- <FormField.Input as={TextInput} cs={inputStencil({density})} />
372
- </FormField>
373
- <FormField
374
- grow
375
- orientation={labelOrientation}
376
- cs={formFieldStencil({density, labelOrientation})}
377
- >
378
- <FormField.Label>City</FormField.Label>
379
- <FormField.Input as={TextInput} cs={inputStencil({density})} />
313
+ <div {...sideBySideInputs({labelOrientation, density})}>
314
+ <FormField grow orientation={labelOrientation} cs={fieldCs}>
315
+ <FormField.Label typeLevel={labelTypeLevel}>Phone Number</FormField.Label>
316
+ <FormField.Input as={TextInput} autoComplete="tel" cs={inputCs} />
317
+ </FormField>
318
+ <FormField grow orientation={labelOrientation} cs={fieldCs}>
319
+ <FormField.Label typeLevel={labelTypeLevel}>Email</FormField.Label>
320
+ <FormField.Input as={TextInput} autoComplete="email" cs={inputCs} />
321
+ </FormField>
322
+ </div>
323
+ <FormField grow orientation={labelOrientation} cs={fieldCs}>
324
+ <FormField.Label typeLevel={labelTypeLevel}>Street Address</FormField.Label>
325
+ <FormField.Input as={TextInput} autoComplete="street-address" cs={inputCs} />
380
326
  </FormField>
381
- <div {...sideBySideInputs({labelOrientation: labelOrientation, density})}>
382
- <FormField
383
- grow
384
- orientation={labelOrientation}
385
- cs={formFieldStencil({density, labelOrientation})}
386
- >
387
- <FormField.Label>State</FormField.Label>
388
- <FormField.Input as={TextInput} cs={inputStencil({density})} />
327
+
328
+ <div {...sideBySideInputs({labelOrientation, density})}>
329
+ <FormField grow orientation={labelOrientation} cs={fieldCs}>
330
+ <FormField.Label typeLevel={labelTypeLevel}>City</FormField.Label>
331
+ <FormField.Input as={TextInput} autoComplete="address-level2" cs={inputCs} />
389
332
  </FormField>
390
- <FormField
391
- grow
392
- orientation={labelOrientation}
393
- cs={[formFieldStencil({density, labelOrientation}), zipCodeContainerStyles]}
394
- >
395
- <FormField.Label>Zip Code</FormField.Label>
396
- <FormField.Input cs={[inputStencil({density}), zipCodeInput]} as={TextInput} />
333
+ <FormField grow orientation={labelOrientation} cs={fieldCs}>
334
+ <FormField.Label typeLevel={labelTypeLevel}>State</FormField.Label>
335
+ <FormField.Input as={TextInput} autoComplete="address-level1" cs={inputCs} />
336
+ </FormField>
337
+ <FormField grow orientation={labelOrientation} cs={[fieldCs, zipCodeContainerStyles]}>
338
+ <FormField.Label typeLevel={labelTypeLevel}>Zip Code</FormField.Label>
339
+ <FormField.Input
340
+ cs={[inputCs, zipCodeInput]}
341
+ as={TextInput}
342
+ autoComplete="postal-code"
343
+ />
397
344
  </FormField>
398
345
  </div>
399
- <FormField
400
- orientation={labelOrientation}
401
- cs={formFieldStencil({density, labelOrientation})}
402
- >
403
- <FormField.Label>Enable Fast Shipping</FormField.Label>
346
+ <FormField grow orientation={labelOrientation} cs={fieldCs}>
347
+ <FormField.Label typeLevel={labelTypeLevel}>Country</FormField.Label>
404
348
  <FormField.Field>
405
- <FormField.Input as={Switch} />
349
+ <Select items={['Dominican Republic', 'Spain', 'United States']}>
350
+ <FormField.Input cs={inputCs} placeholder="Choose a country" as={Select.Input} />
351
+ <Select.Popper>
352
+ <Select.Card>
353
+ <Select.List>{item => <Select.Item>{item}</Select.Item>}</Select.List>
354
+ </Select.Card>
355
+ </Select.Popper>
356
+ </Select>
357
+ </FormField.Field>
358
+ </FormField>
359
+ <FormField orientation={labelOrientation} cs={fieldCs}>
360
+ <FormField.Label typeLevel={labelTypeLevel}>Enable Fast Shipping</FormField.Label>
361
+ <FormField.Field>
362
+ <FormField.Input as={Switch} checked={fastShipping} onChange={handleFastShipping} />
406
363
  </FormField.Field>
407
364
  </FormField>
408
- <FormFieldGroup
409
- cs={formFieldStencil({density, labelOrientation})}
410
- orientation={labelOrientation}
411
- >
412
- <FormFieldGroup.Label>Credit Card</FormFieldGroup.Label>
413
- <FormFieldGroup.Field>
414
- <FormFieldGroup.List cs={formFieldGroupListStyles}>
415
- <FormFieldGroup.Input
416
- as={TextInput}
417
- placeholder="XXXX"
418
- cs={creditCardInputStencil({density})}
419
- />
420
- <FormFieldGroup.Input
421
- as={TextInput}
422
- placeholder="XXXX"
423
- cs={creditCardInputStencil({density})}
424
- />
425
- <FormFieldGroup.Input
426
- as={TextInput}
427
- placeholder="XXXX"
428
- cs={creditCardInputStencil({density})}
429
- />
430
- <FormFieldGroup.Input
431
- cs={creditCardInputStencil({density})}
432
- placeholder="XXXX"
433
- as={TextInput}
434
- />
435
- </FormFieldGroup.List>
436
- </FormFieldGroup.Field>
437
- </FormFieldGroup>
438
365
  </div>
439
366
  </form>
440
367
  </div>
441
- </div>
368
+ </>
442
369
  );
443
370
  };