| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; |
| 3 |
import { Component } from 'react'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Check/check.mjs'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
import { getOUIAProps } from '../../helpers'; |
| 7 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 8 |
import { ASTERISK } from '../../helpers/htmlConstants'; |
| 9 |
// tslint:disable-next-line:no-empty |
| 10 |
const defaultOnChange = () => { }; |
| 11 |
class Checkbox extends Component { |
| 12 |
constructor() { |
| 13 |
super(...arguments); |
| 14 |
this.handleChange = (event) => { |
| 15 |
this.props.onChange(event, event.currentTarget.checked); |
| 16 |
}; |
| 17 |
} |
| 18 |
render() { |
| 19 |
const _a = this.props, { 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedBy, className, inputClassName, onChange, isLabelWrapped, labelPosition = 'end', isValid, isDisabled, isRequired, isChecked, label, checked, defaultChecked, description, body, ouiaId, ouiaSafe, component } = _a, props = __rest(_a, ['aria-label', 'aria-describedby', "className", "inputClassName", "onChange", "isLabelWrapped", "labelPosition", "isValid", "isDisabled", "isRequired", "isChecked", "label", "checked", "defaultChecked", "description", "body", "ouiaId", "ouiaSafe", "component"]); |
| 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 |
return (_jsx(SSRSafeIds, { prefix: "pf-checkbox-description-", ouiaComponentType: "Checkbox", children: (descriptionId, generatedOuiaId) => { |
| 35 |
let ariaDescribedByValue; |
| 36 |
if (ariaDescribedBy !== undefined) { |
| 37 |
ariaDescribedByValue = ariaDescribedBy === '' ? undefined : ariaDescribedBy; |
| 38 |
} |
| 39 |
else if (description) { |
| 40 |
ariaDescribedByValue = descriptionId; |
| 41 |
} |
| 42 |
const inputRendered = (_jsx("input", Object.assign({}, props, { className: css(styles.checkInput, inputClassName), type: "checkbox", onChange: this.handleChange, "aria-invalid": !isValid, "aria-label": ariaLabel, "aria-describedby": ariaDescribedByValue, disabled: isDisabled, required: isRequired, ref: (elem) => { |
| 43 |
elem && (elem.indeterminate = isChecked === null); |
| 44 |
} }, checkedProps, getOUIAProps(Checkbox.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe)))); |
| 45 |
const wrapWithLabel = (isLabelWrapped && !component) || component === 'label'; |
| 46 |
const Label = wrapWithLabel ? 'span' : 'label'; |
| 47 |
const labelRendered = label ? (_jsxs(Label, { className: css(styles.checkLabel, isDisabled && styles.modifiers.disabled), htmlFor: !wrapWithLabel ? props.id : undefined, children: [label, isRequired && (_jsx("span", { className: css(styles.checkLabelRequired), "aria-hidden": "true", children: ASTERISK }))] })) : null; |
| 48 |
const WrapperComponent = component !== null && component !== void 0 ? component : (wrapWithLabel ? 'label' : 'div'); |
| 49 |
checkedProps.checked = checkedProps.checked === null ? false : checkedProps.checked; |
| 50 |
return (_jsxs(WrapperComponent, { className: css(styles.check, !label && styles.modifiers.standalone, className), htmlFor: wrapWithLabel ? props.id : undefined, children: [labelPosition === 'start' ? (_jsxs(_Fragment, { children: [labelRendered, inputRendered] })) : (_jsxs(_Fragment, { children: [inputRendered, labelRendered] })), description && (_jsx("span", { id: descriptionId, className: css(styles.checkDescription), children: description })), body && _jsx("span", { className: css(styles.checkBody), children: body })] })); |
| 51 |
} })); |
| 52 |
} |
| 53 |
} |
| 54 |
Checkbox.displayName = 'Checkbox'; |
| 55 |
Checkbox.defaultProps = { |
| 56 |
className: '', |
| 57 |
isLabelWrapped: false, |
| 58 |
isValid: true, |
| 59 |
isDisabled: false, |
| 60 |
isRequired: false, |
| 61 |
isChecked: false, |
| 62 |
onChange: defaultOnChange, |
| 63 |
ouiaSafe: true |
| 64 |
}; |
| 65 |
export { Checkbox }; |
| 66 |
//# sourceMappingURL=Checkbox.js.map |