1 | import { __rest } from "tslib";
|
2 | import * as React from 'react';
|
3 | import styles from '@patternfly/react-styles/css/components/Check/check';
|
4 | import { css } from '@patternfly/react-styles';
|
5 | import { getDefaultOUIAId, getOUIAProps } from '../../helpers';
|
6 |
|
7 | const defaultOnChange = () => { };
|
8 | export 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 |
|
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 | }
|
42 | Checkbox.displayName = 'Checkbox';
|
43 | Checkbox.defaultProps = {
|
44 | className: '',
|
45 | isValid: true,
|
46 | isDisabled: false,
|
47 | isChecked: false,
|
48 | onChange: defaultOnChange,
|
49 | ouiaSafe: true
|
50 | };
|
51 |
|
\ | No newline at end of file |