Debian Codegraph @

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

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.Switch = 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 switch_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Switch/switch"));
8 const react_styles_1 = require("@patternfly/react-styles");
9 const check_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/check-icon'));
10 const helpers_1 = require("../../helpers");
11 const SSRSafeIds_1 = require("../../helpers/SSRSafeIds/SSRSafeIds");
12 class Switch extends react_1.Component {
13 constructor(props) {
14 super(props);
15 if (!props.label && !props['aria-label'] && !props['aria-labelledby']) {
16 // eslint-disable-next-line no-console
17 console.error('Switch: Switch requires at least one of label, aria-labelledby, or aria-label props to be specified');
18 }
19 }
20 render() {
21 const _a = this.props, { id: idProp, className, label, isChecked, defaultChecked, hasCheckIcon, isDisabled, onChange, isReversed, ouiaId, ouiaSafe, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy } = _a, props = tslib_1.__rest(_a, ["id", "className", "label", "isChecked", "defaultChecked", "hasCheckIcon", "isDisabled", "onChange", "isReversed", "ouiaId", "ouiaSafe", 'aria-label', 'aria-labelledby']);
22 return ((0, jsx_runtime_1.jsx)(SSRSafeIds_1.SSRSafeIds, { prefix: "pf-switch-", ouiaComponentType: "Switch", children: (generatedId, generatedOuiaId) => {
23 const id = idProp || generatedId;
24 const hasAccessibleName = label || ariaLabel || ariaLabelledBy;
25 const isAriaLabelledBy = hasAccessibleName && (!ariaLabel || ariaLabelledBy);
26 const useDefaultAriaLabelledBy = !ariaLabelledBy && !ariaLabel;
27 const ariaLabelledByIds = ariaLabelledBy !== null && ariaLabelledBy !== void 0 ? ariaLabelledBy : `${id}-label`;
28 return ((0, jsx_runtime_1.jsxs)("label", Object.assign({ className: (0, react_styles_1.css)(switch_1.default.switch, isReversed && switch_1.default.modifiers.reverse, className), htmlFor: id }, (0, helpers_1.getOUIAProps)(Switch.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe), { children: [(0, jsx_runtime_1.jsx)("input", Object.assign({ id: id, className: (0, react_styles_1.css)(switch_1.default.switchInput), type: "checkbox", role: "switch", onChange: (event) => onChange(event, event.target.checked) }, (defaultChecked !== undefined ? { defaultChecked } : { checked: isChecked }), { disabled: isDisabled, "aria-labelledby": isAriaLabelledBy ? ariaLabelledByIds : null, "aria-label": ariaLabel }, props)), label !== undefined ? ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(switch_1.default.switchToggle), children: hasCheckIcon && ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(switch_1.default.switchToggleIcon), children: (0, jsx_runtime_1.jsx)(check_icon_1.default, {}) })) }), (0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(switch_1.default.switchLabel), id: isAriaLabelledBy && useDefaultAriaLabelledBy ? `${id}-label` : null, "aria-hidden": "true", children: label })] })) : ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(switch_1.default.switchToggle), children: (0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(switch_1.default.switchToggleIcon), children: (0, jsx_runtime_1.jsx)(check_icon_1.default, {}) }) }))] })));
29 } }));
30 }
31 }
32 exports.Switch = Switch;
33 Switch.displayName = 'Switch';
34 Switch.defaultProps = {
35 isChecked: true,
36 isDisabled: false,
37 isReversed: false,
38 'aria-label': undefined,
39 'aria-labelledby': undefined,
40 onChange: () => undefined
41 };
42 //# sourceMappingURL=Switch.js.map