Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Checkbox/Checkbox.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.Checkbox = void 0;
4 const tslib_1 = require("tslib");
5 const jsx_runtime_1 = require("react/jsx-runtime");
6 const react_1 = require("react");
7 const check_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Check/check"));
8 const react_styles_1 = require("@patternfly/react-styles");
9 const helpers_1 = require("../../helpers");
10 const SSRSafeIds_1 = require("../../helpers/SSRSafeIds/SSRSafeIds");
11 const htmlConstants_1 = require("../../helpers/htmlConstants");
12 // tslint:disable-next-line:no-empty
13 const defaultOnChange = () => { };
14 class Checkbox extends react_1.Component {
15 constructor() {
16 super(...arguments);
17 this.handleChange = (event) => {
18 this.props.onChange(event, event.currentTarget.checked);
19 };
20 }
21 render() {
22 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 = tslib_1.__rest(_a, ['aria-label', 'aria-describedby', "className", "inputClassName", "onChange", "isLabelWrapped", "labelPosition", "isValid", "isDisabled", "isRequired", "isChecked", "label", "checked", "defaultChecked", "description", "body", "ouiaId", "ouiaSafe", "component"]);
23 if (!props.id) {
24 // eslint-disable-next-line no-console
25 console.error('Checkbox:', 'id is required to make input accessible');
26 }
27 const checkedProps = {};
28 if ([true, false].includes(checked) || isChecked === true) {
29 checkedProps.checked = checked || isChecked;
30 }
31 if (onChange !== defaultOnChange) {
32 checkedProps.checked = isChecked;
33 }
34 if ([false, true].includes(defaultChecked)) {
35 checkedProps.defaultChecked = defaultChecked;
36 }
37 return ((0, jsx_runtime_1.jsx)(SSRSafeIds_1.SSRSafeIds, { prefix: "pf-checkbox-description-", ouiaComponentType: "Checkbox", children: (descriptionId, generatedOuiaId) => {
38 let ariaDescribedByValue;
39 if (ariaDescribedBy !== undefined) {
40 ariaDescribedByValue = ariaDescribedBy === '' ? undefined : ariaDescribedBy;
41 }
42 else if (description) {
43 ariaDescribedByValue = descriptionId;
44 }
45 const inputRendered = ((0, jsx_runtime_1.jsx)("input", Object.assign({}, props, { className: (0, react_styles_1.css)(check_1.default.checkInput, inputClassName), type: "checkbox", onChange: this.handleChange, "aria-invalid": !isValid, "aria-label": ariaLabel, "aria-describedby": ariaDescribedByValue, disabled: isDisabled, required: isRequired, ref: (elem) => {
46 elem && (elem.indeterminate = isChecked === null);
47 } }, checkedProps, (0, helpers_1.getOUIAProps)(Checkbox.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe))));
48 const wrapWithLabel = (isLabelWrapped && !component) || component === 'label';
49 const Label = wrapWithLabel ? 'span' : 'label';
50 const labelRendered = label ? ((0, jsx_runtime_1.jsxs)(Label, { className: (0, react_styles_1.css)(check_1.default.checkLabel, isDisabled && check_1.default.modifiers.disabled), htmlFor: !wrapWithLabel ? props.id : undefined, children: [label, isRequired && ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(check_1.default.checkLabelRequired), "aria-hidden": "true", children: htmlConstants_1.ASTERISK }))] })) : null;
51 const WrapperComponent = component !== null && component !== void 0 ? component : (wrapWithLabel ? 'label' : 'div');
52 checkedProps.checked = checkedProps.checked === null ? false : checkedProps.checked;
53 return ((0, jsx_runtime_1.jsxs)(WrapperComponent, { className: (0, react_styles_1.css)(check_1.default.check, !label && check_1.default.modifiers.standalone, className), htmlFor: wrapWithLabel ? props.id : undefined, children: [labelPosition === 'start' ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [labelRendered, inputRendered] })) : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [inputRendered, labelRendered] })), description && ((0, jsx_runtime_1.jsx)("span", { id: descriptionId, className: (0, react_styles_1.css)(check_1.default.checkDescription), children: description })), body && (0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(check_1.default.checkBody), children: body })] }));
54 } }));
55 }
56 }
57 exports.Checkbox = Checkbox;
58 Checkbox.displayName = 'Checkbox';
59 Checkbox.defaultProps = {
60 className: '',
61 isLabelWrapped: false,
62 isValid: true,
63 isDisabled: false,
64 isRequired: false,
65 isChecked: false,
66 onChange: defaultOnChange,
67 ouiaSafe: true
68 };
69 //# sourceMappingURL=Checkbox.js.map