UNPKG

2.82 kBJavaScriptView Raw
1import { __rest } from "tslib";
2import * as React from 'react';
3import styles from '@patternfly/react-styles/css/components/Check/check';
4import { css } from '@patternfly/react-styles';
5import { getDefaultOUIAId, getOUIAProps } from '../../helpers';
6// tslint:disable-next-line:no-empty
7const defaultOnChange = () => { };
8export class Checkbox extends React.Component {
9 constructor(props) {
10 super(props);
11 this.handleChange = (event) => {
12 this.props.onChange(event.currentTarget.checked, event);
13 };
14 this.state = {
15 ouiaStateId: getDefaultOUIAId(Checkbox.displayName)
16 };
17 }
18 render() {
19 const _a = this.props, { 'aria-label': ariaLabel, className, onChange, isValid, isDisabled, isChecked, label, checked, defaultChecked, description, body, ouiaId, ouiaSafe } = _a, props = __rest(_a, ['aria-label', "className", "onChange", "isValid", "isDisabled", "isChecked", "label", "checked", "defaultChecked", "description", "body", "ouiaId", "ouiaSafe"]);
20 if (!props.id) {
21 // eslint-disable-next-line no-console
22 console.error('Checkbox:', 'id is required to make input accessible');
23 }
24 const checkedProps = {};
25 if ([true, false].includes(checked) || isChecked === true) {
26 checkedProps.checked = checked || isChecked;
27 }
28 if (onChange !== defaultOnChange) {
29 checkedProps.checked = isChecked;
30 }
31 if ([false, true].includes(defaultChecked)) {
32 checkedProps.defaultChecked = defaultChecked;
33 }
34 checkedProps.checked = checkedProps.checked === null ? false : checkedProps.checked;
35 return (React.createElement("div", { className: css(styles.check, !label && styles.modifiers.standalone, className) },
36 React.createElement("input", Object.assign({}, props, { className: css(styles.checkInput), type: "checkbox", onChange: this.handleChange, "aria-invalid": !isValid, "aria-label": ariaLabel, disabled: isDisabled, ref: elem => elem && (elem.indeterminate = isChecked === null) }, checkedProps, getOUIAProps(Checkbox.displayName, ouiaId !== undefined ? ouiaId : this.state.ouiaStateId, ouiaSafe))),
37 label && (React.createElement("label", { className: css(styles.checkLabel, isDisabled && styles.modifiers.disabled), htmlFor: props.id }, label)),
38 description && React.createElement("span", { className: css(styles.checkDescription) }, description),
39 body && React.createElement("span", { className: css(styles.checkBody) }, body)));
40 }
41}
42Checkbox.displayName = 'Checkbox';
43Checkbox.defaultProps = {
44 className: '',
45 isValid: true,
46 isDisabled: false,
47 isChecked: false,
48 onChange: defaultOnChange,
49 ouiaSafe: true
50};
51//# sourceMappingURL=Checkbox.js.map
\No newline at end of file