| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; |
| 3 |
import { Component, Fragment } from 'react'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Switch/switch.mjs'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
import CheckIcon from '@patternfly/react-icons/dist/esm/icons/check-icon'; |
| 7 |
import { getOUIAProps } from '../../helpers'; |
| 8 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 9 |
class Switch extends Component { |
| 10 |
constructor(props) { |
| 11 |
super(props); |
| 12 |
if (!props.label && !props['aria-label'] && !props['aria-labelledby']) { |
| 13 |
// eslint-disable-next-line no-console |
| 14 |
console.error('Switch: Switch requires at least one of label, aria-labelledby, or aria-label props to be specified'); |
| 15 |
} |
| 16 |
} |
| 17 |
render() { |
| 18 |
const _a = this.props, { id: idProp, className, label, isChecked, defaultChecked, hasCheckIcon, isDisabled, onChange, isReversed, ouiaId, ouiaSafe, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy } = _a, props = __rest(_a, ["id", "className", "label", "isChecked", "defaultChecked", "hasCheckIcon", "isDisabled", "onChange", "isReversed", "ouiaId", "ouiaSafe", 'aria-label', 'aria-labelledby']); |
| 19 |
return (_jsx(SSRSafeIds, { prefix: "pf-switch-", ouiaComponentType: "Switch", children: (generatedId, generatedOuiaId) => { |
| 20 |
const id = idProp || generatedId; |
| 21 |
const hasAccessibleName = label || ariaLabel || ariaLabelledBy; |
| 22 |
const isAriaLabelledBy = hasAccessibleName && (!ariaLabel || ariaLabelledBy); |
| 23 |
const useDefaultAriaLabelledBy = !ariaLabelledBy && !ariaLabel; |
| 24 |
const ariaLabelledByIds = ariaLabelledBy !== null && ariaLabelledBy !== void 0 ? ariaLabelledBy : `${id}-label`; |
| 25 |
return (_jsxs("label", Object.assign({ className: css(styles.switch, isReversed && styles.modifiers.reverse, className), htmlFor: id }, getOUIAProps(Switch.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe), { children: [_jsx("input", Object.assign({ id: id, className: css(styles.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 ? (_jsxs(Fragment, { children: [_jsx("span", { className: css(styles.switchToggle), children: hasCheckIcon && (_jsx("span", { className: css(styles.switchToggleIcon), children: _jsx(CheckIcon, {}) })) }), _jsx("span", { className: css(styles.switchLabel), id: isAriaLabelledBy && useDefaultAriaLabelledBy ? `${id}-label` : null, "aria-hidden": "true", children: label })] })) : (_jsx("span", { className: css(styles.switchToggle), children: _jsx("div", { className: css(styles.switchToggleIcon), children: _jsx(CheckIcon, {}) }) }))] }))); |
| 26 |
} })); |
| 27 |
} |
| 28 |
} |
| 29 |
Switch.displayName = 'Switch'; |
| 30 |
Switch.defaultProps = { |
| 31 |
isChecked: true, |
| 32 |
isDisabled: false, |
| 33 |
isReversed: false, |
| 34 |
'aria-label': undefined, |
| 35 |
'aria-labelledby': undefined, |
| 36 |
onChange: () => undefined |
| 37 |
}; |
| 38 |
export { Switch }; |
| 39 |
//# sourceMappingURL=Switch.js.map |