-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathindex.jsx
558 lines (528 loc) · 18.4 KB
/
index.jsx
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* eslint-disable react/jsx-curly-brace-presence */
/* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */
/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */
// # Checkbox Component
// Implements the [Checkbox design pattern](https://www.lightningdesignsystem.com/components/forms/#checkbox) in React.
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
// ### shortid
// [npmjs.com/package/shortid](https://www.npmjs.com/package/shortid)
// shortid is a short, non-sequential, url-friendly, unique id generator
import shortid from 'shortid';
// ### Event Helpers
import KEYS from '../../utilities/key-code';
import EventUtil from '../../utilities/event';
// This component's `checkProps` which issues warnings to developers about properties when in development mode (similar to React's built in development tools)
import checkProps from './check-props';
import componentDoc from './component.json';
import { CHECKBOX } from '../../utilities/constants';
import Icon from '../icon';
import getAriaProps from '../../utilities/get-aria-props';
const propTypes = {
/**
* An HTML ID that is shared with ARIA-supported devices with the
* `aria-controls` attribute in order to relate the input with
* another region of the page. An example would be a select box
* that shows or hides a panel.
*/
'aria-controls': PropTypes.string,
/**
* The `aria-describedby` attribute is used to indicate the IDs of the elements that describe the object. It is used to establish a relationship between widgets or groups and text that described them. This is very similar to aria-labelledby: a label describes the essence of an object, while a description provides more information that the user might need.
*/
'aria-describedby': PropTypes.string,
/**
* The aria-labelledby attribute establishes relationships between objects and their label(s), and its value should be one or more element IDs, which refer to elements that have the text needed for labeling. List multiple element IDs in a space delimited fashion.
*/
'aria-labelledby': PropTypes.string,
/**
* `aria-owns` indicate that an element depends on the current one when the relation can't be determined by the hierarchy structure.
*/
'aria-owns': PropTypes.string,
/**
* The `aria-required` attribute is used to indicate that user input is required on an element before a form can be submitted.
*/
'aria-required': PropTypes.bool,
/**
* **Assistive text for accessibility**
* This object is merged with the default props object on every render.
* * `heading`: This is used as a visually hidden label if, no `labels.heading` is provided.
* * `label`: This is used as a visually hidden label if, no `labels.label` is provided.
*/
assistiveText: PropTypes.shape({
heading: PropTypes.string,
label: PropTypes.string,
}),
/**
* The Checkbox should be a controlled component, and will always be in the state specified. If checked is not defined, the state of the uncontrolled native `input` component will be used.
*/
checked: PropTypes.bool,
/**
* This is the initial value of an uncontrolled form element and is present only
* to provide compatibility with hybrid framework applications that are not
* entirely React. It should only be used in an application without centralized
* state (Redux, Flux). "Controlled components" with centralized state is highly recommended. See [Code Overview](https://github.com/salesforce/design-system-react/blob/master/docs/codebase-overview.md#controlled-and-uncontrolled-components) for more information.
*/
defaultChecked: PropTypes.bool,
/**
* Class names to be added to the outer container of the Checkbox.
*/
className: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
PropTypes.string,
]),
/**
* Disables the Checkbox and prevents clicking it.
*/
disabled: PropTypes.bool,
/**
* Message to display when the Checkbox is in an error state. When this is present, also visually highlights the component as in error.
*/
errorText: PropTypes.string,
/**
* A unique ID is needed in order to support keyboard navigation and ARIA support. This ID is added to the `input` element
*/
id: PropTypes.string,
/**
* The Checkbox will be indeterminate if its state can not be figured out or is partially checked. Once a checkbox is indeterminate, a click should cause it to be checked. Since a user cannot put a checkbox into an indeterminate state, it is assumed you are controlling the value of `checked` with the parent, also, and that this is a controlled component. **Note:** `indeterminate` proptype does nothing in the `toggle` variant, as [SLDS does not support it](https://lightningdesignsystem.com/components/forms/#flavor-checkbox-toggle-checkbox-toggle).
*/
indeterminate: PropTypes.bool,
/**
* **Text labels for internationalization**
* This object is merged with the default props object on every render.
* * `heading`: Heading for the visual picker variant
* * `label`: Label for the _enabled_ state of the Toggle variant. Defaults to "Enabled".
* * `toggleDisabled`: Label for the _disabled_ state of the Toggle variant. Defaults to "Disabled". Note that this uses SLDS language, and meaning, of "Enabled" and "Disabled"; referring to the state of whatever the checkbox is _toggling_, not whether the checkbox itself is enabled or disabled.
* * `toggleEnabled`: Label for the _enabled_ state of the Toggle variant. Defaults to "Enabled".
*/
labels: PropTypes.shape({
heading: PropTypes.string,
label: PropTypes.string,
toggleDisabled: PropTypes.string,
toggleEnabled: PropTypes.string,
}),
/**
* Name of the submitted form parameter.
*/
name: PropTypes.string,
/**
* This event fires when the Checkbox looses focus. It passes in `{ event }`.
*/
onBlur: PropTypes.func,
/**
* This event fires when the Checkbox changes. Passes in `event, { checked }`. This used to be `checked, event, { checked }`.
*/
onChange: PropTypes.func,
/**
* This event fires when the Checkbox is focused. It passes in `{ event }`.
*/
onFocus: PropTypes.func,
/**
* This event fires when a key is pressed down. It passes in `{ event }`.
*/
onKeyDown: PropTypes.func,
/**
* This event fires when a character is typed. See [this article](http://www.bloggingdeveloper.com/post/KeyPress-KeyDown-KeyUp-The-Difference-Between-Javascript-Key-Events.aspx) for more information. It passes in `{ event }`.
*/
onKeyPress: PropTypes.func,
/**
* This event fires when a pressed key is released. It passes in `{ event }`.
*/
onKeyUp: PropTypes.func,
/**
* Triggered to indicate that this component should receive focus.
*/
onRequestFocus: PropTypes.func,
/**
* Displays the value of the input, but does not allow changes.
*/
readOnly: PropTypes.bool,
/**
* If true, will trigger `onRequestFocus`.
*/
requestFocus: PropTypes.bool,
/**
* Highlights the Checkbox as a required field (does not perform any validation).
*/
required: PropTypes.bool,
/**
* The aria-role of the checkbox.
*/
role: PropTypes.string,
/**
* Write <code>"-1"</code> if you don't want the user to tab to the button.
*/
tabIndex: PropTypes.string,
/**
* Which UX pattern of checkbox? The default is `base` while other option is `toggle`. (**Note:** `toggle` variant does not support the `indeterminate` feature, because [SLDS does not support it](https://lightningdesignsystem.com/components/forms/#flavor-checkbox-toggle-checkbox-toggle).)
*/
variant: PropTypes.oneOf(['base', 'toggle', 'button-group', 'visual-picker']),
/**
* Determines whether visual picker is coverable when selected (only for visual picker variant)
*/
coverable: PropTypes.bool,
/**
* Determines whether the visual picker should be vertical or horizontal (only for visual picker variant)
*/
vertical: PropTypes.bool,
/**
* Allows icon to shown with checkbox (only for non-coverable visual picker variant)
*/
onRenderVisualPicker: PropTypes.func,
/**
* Allows icon to shown if checkbox is not selected (only for visual picker variant)
*/
onRenderVisualPickerSelected: PropTypes.func,
/**
* Allows icon to shown if checkbox is not selected (only for visual picker variant)
*/
onRenderVisualPickerNotSelected: PropTypes.func,
/**
* Size of checkbox in case of visual composer variant
*/
size: PropTypes.oneOf(['medium', 'large']),
};
const defaultProps = {
assistiveText: {},
labels: {
toggleDisabled: 'Disabled',
toggleEnabled: 'Enabled',
},
variant: 'base',
};
/**
* The ability to style checkboxes with CSS varies across browsers. Using this component ensures checkboxes look the same everywhere.
*/
class Checkbox extends React.Component {
constructor(props) {
super(props);
checkProps(CHECKBOX, this.props, componentDoc);
this.generatedId = shortid.generate();
}
getId = () => this.props.id || this.generatedId;
getErrorId = () =>
this.props.errorText ? `${this.getId()}-error-text` : undefined;
getAriaDescribedBy = ({ idArray = [] } = {}) =>
idArray
.concat(this.props['aria-describedby'], this.getErrorId())
.filter(Boolean)
.join(' ') || undefined;
handleChange = (event) => {
const { checked, indeterminate, onChange } = this.props;
if (typeof onChange === 'function') {
// `target.checked` is present twice to maintain backwards compatibility. Please remove first parameter `value` on the next breaking change or when `forms/checkbox` is removed.
if (this.props.oldEventParameterOrder) {
onChange(event.target.checked, event, {
checked: indeterminate ? true : !checked,
indeterminate: false,
});
} else {
// NEW API
onChange(event, {
checked: indeterminate ? true : !checked,
indeterminate: false,
});
}
}
};
handleKeyDown = (event) => {
if (event.keyCode) {
if (event.keyCode === KEYS.ENTER || event.keyCode === KEYS.SPACE) {
EventUtil.trapImmediate(event);
this.handleChange(event);
}
}
};
renderButtonGroupVariant = (props, ariaProps, assistiveText, labels) => (
<span className="slds-button slds-checkbox_button">
<input
disabled={props.disabled}
/* A form element should not have both checked and defaultChecked props. */
{...(props.checked !== undefined
? { checked: props.checked }
: { defaultChecked: props.defaultChecked })}
id={this.getId()}
name={props.name}
onBlur={props.onBlur}
onChange={this.handleChange}
onFocus={props.onFocus}
onKeyDown={props.onKeyDown}
onKeyPress={props.onKeyPress}
onKeyUp={props.onKeyUp}
ref={(component) => {
this.input = component;
}}
role={props.role}
required={props.required}
type="checkbox"
{...ariaProps}
aria-describedby={this.getAriaDescribedBy()}
/>
<label className="slds-checkbox_button__label" htmlFor={this.getId()}>
<span className="slds-checkbox_faux">{labels.label}</span>
{assistiveText.label ? (
<span className="slds-assistive-text">{assistiveText.label}</span>
) : null}
</label>
</span>
);
renderBaseVariant = (props, ariaProps, assistiveText, labels) => (
<div
className={classNames(
'slds-form-element',
{
'is-required': props.required,
'slds-has-error': props.errorText,
},
props.className
)}
>
<div className="slds-form-element__control">
<span className="slds-checkbox">
{props.required ? (
<abbr className="slds-required" title="required">
{'*'}
</abbr>
) : null}
<input
disabled={props.disabled}
/* A form element should not have both checked and defaultChecked props. */
{...(props.checked !== undefined
? { checked: props.checked }
: { defaultChecked: props.defaultChecked })}
id={this.getId()}
name={props.name}
onBlur={props.onBlur}
onChange={this.handleChange}
onFocus={props.onFocus}
onKeyDown={props.onKeyDown}
onKeyPress={props.onKeyPress}
onKeyUp={props.onKeyUp}
ref={(component) => {
if (component) {
// eslint-disable-next-line no-param-reassign
component.indeterminate = props.indeterminate;
if (this.props.requestFocus && this.props.onRequestFocus) {
this.props.onRequestFocus(component);
}
}
this.input = component;
}}
role={props.role}
required={props.required}
tabIndex={
!isNaN(Number(props.tabIndex))
? parseInt(props.tabIndex, 10)
: props.tabIndex
}
type="checkbox"
{...ariaProps}
aria-describedby={this.getAriaDescribedBy()}
/>
<label
className="slds-checkbox__label"
htmlFor={this.getId()}
id={props.labelId}
>
<span className="slds-checkbox_faux" />
{labels.label ? (
<span className="slds-form-element__label">{labels.label}</span>
) : null}
{assistiveText.label ? (
<span className="slds-assistive-text">{assistiveText.label}</span>
) : null}
</label>
</span>
</div>
{props.errorText ? (
<div className="slds-form-element__help" id={this.getErrorId()}>
{props.errorText}
</div>
) : null}
</div>
);
renderToggleVariant = (props, ariaProps, assistiveText, labels) => (
<div
className={classNames(
'slds-form-element',
{
'is-required': props.required,
'slds-has-error': props.errorText,
},
props.className
)}
>
<label className="slds-checkbox_toggle slds-grid" htmlFor={this.getId()}>
{props.required ? (
<abbr className="slds-required" title="required">
{'*'}
</abbr>
) : null}
{labels.label ? (
<span className="slds-form-element__label slds-m-bottom_none">
{labels.label}
</span>
) : null}
{assistiveText.label ? (
<span className="slds-assistive-text">{assistiveText.label}</span>
) : null}
<input
disabled={props.disabled}
id={this.getId()}
/* A form element should not have both checked and defaultChecked props. */
{...(props.checked !== undefined
? { checked: props.checked }
: { defaultChecked: props.defaultChecked })}
name={props.name}
onBlur={props.onBlur}
onChange={this.handleChange}
onFocus={props.onFocus}
onKeyDown={props.onKeyDown}
onKeyPress={props.onKeyPress}
onKeyUp={props.onKeyUp}
ref={(component) => {
this.input = component;
}}
role={props.role}
required={props.required}
type="checkbox"
{...ariaProps}
aria-describedby={this.getAriaDescribedBy({
idArray: [`${this.getId()}-desc`],
})}
/>
<span
id={`${this.getId()}-desc`}
className="slds-checkbox_faux_container"
aria-live="assertive"
>
<span className="slds-checkbox_faux" />
<span className="slds-checkbox_on">{labels.toggleEnabled}</span>
<span className="slds-checkbox_off">{labels.toggleDisabled}</span>
</span>
</label>
{props.errorText ? (
<div className="slds-form-element__help" id={this.getErrorId()}>
{props.errorText}
</div>
) : null}
</div>
);
renderVisualPickerVariant = (props, ariaProps, assistiveText) => (
<span
className={classNames(
'slds-visual-picker',
`slds-visual-picker_${this.props.size}`,
this.props.vertical ? 'slds-visual-picker_vertical' : null
)}
>
<input
disabled={props.disabled}
/* A form element should not have both checked and defaultChecked props. */
{...(props.checked !== undefined
? { checked: props.checked }
: { defaultChecked: props.defaultChecked })}
id={this.getId()}
name={props.name}
onBlur={props.onBlur}
onChange={this.handleChange}
onFocus={props.onFocus}
onKeyDown={props.onKeyDown}
onKeyPress={props.onKeyPress}
onKeyUp={props.onKeyUp}
ref={(component) => {
this.input = component;
}}
role={props.role}
required={props.required}
type="checkbox"
{...ariaProps}
aria-describedby={this.getAriaDescribedBy()}
/>
<label className="slds-checkbox_button__label" htmlFor={this.getId()}>
{this.props.coverable ? (
<div className="slds-visual-picker__figure slds-visual-picker__icon slds-align_absolute-center">
<span className="slds-is-selected">
{this.props.onRenderVisualPickerSelected()}
</span>
<span className="slds-is-not-selected">
{this.props.onRenderVisualPickerNotSelected()}
</span>
</div>
) : (
<span className="slds-visual-picker__figure slds-visual-picker__text slds-align_absolute-center">
{this.props.onRenderVisualPicker()}
</span>
)}
{!this.props.vertical ? (
<span className="slds-visual-picker__body">
{this.props.labels.heading ? (
<span className="slds-text-heading_small">
{this.props.labels.heading}
</span>
) : null}
<span className="slds-text-title">{this.props.labels.label}</span>
{assistiveText.label || assistiveText.heading ? (
<span className="slds-assistive-text">
{assistiveText.label || assistiveText.heading}
</span>
) : null}
</span>
) : null}
{!this.props.coverable ? (
<span className="slds-icon_container slds-visual-picker__text-check">
<Icon
assistiveText={this.props.assistiveText}
category="utility"
name="check"
colorVariant="base"
size="x-small"
/>
</span>
) : null}
</label>
</span>
);
render() {
const ariaProps = getAriaProps(this.props);
if (this.props.variant === 'toggle') {
ariaProps['aria-describedby'] = `${this.getId()}-desc`;
}
const assistiveText = {
...defaultProps.assistiveText,
/* Remove backward compatibility at next breaking change */
...(typeof this.props.assistiveText === 'string'
? { label: this.props.assistiveText }
: {}),
...(typeof this.props.assistiveText === 'object'
? this.props.assistiveText
: {}),
};
const labels = {
...defaultProps.labels,
/* Remove backward compatibility at next breaking change */
...(this.props.label ? { label: this.props.label } : {}),
...this.props.labels,
};
const subRenders = {
base: this.renderBaseVariant,
'button-group': this.renderButtonGroupVariant,
toggle: this.renderToggleVariant,
'visual-picker': this.renderVisualPickerVariant,
};
const variantExists = subRenders[this.props.variant];
return variantExists
? subRenders[this.props.variant](
this.props,
ariaProps,
assistiveText,
labels
)
: subRenders.base(this.props, ariaProps, assistiveText, labels);
}
}
Checkbox.displayName = CHECKBOX;
Checkbox.propTypes = propTypes;
Checkbox.defaultProps = defaultProps;
export default Checkbox;