| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; |
| 3 |
import { Component } from 'react'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Radio/radio.mjs'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
import { getOUIAProps } from '../../helpers'; |
| 7 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 8 |
class Radio extends Component { |
| 9 |
constructor(props) { |
| 10 |
super(props); |
| 11 |
this.handleChange = (event) => { |
| 12 |
this.props.onChange(event, event.currentTarget.checked); |
| 13 |
}; |
| 14 |
if (!props.label && !props['aria-label']) { |
| 15 |
// eslint-disable-next-line no-console |
| 16 |
console.error('Radio:', 'Radio requires an aria-label to be specified'); |
| 17 |
} |
| 18 |
} |
| 19 |
render() { |
| 20 |
const _a = this.props, { 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedBy, checked, className, inputClassName, defaultChecked, isLabelWrapped, labelPosition = 'end', isChecked, isDisabled, isValid, label, |
| 21 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 22 |
onChange, description, body, ouiaId, ouiaSafe = true, component } = _a, props = __rest(_a, ['aria-label', 'aria-describedby', "checked", "className", "inputClassName", "defaultChecked", "isLabelWrapped", "labelPosition", "isChecked", "isDisabled", "isValid", "label", "onChange", "description", "body", "ouiaId", "ouiaSafe", "component"]); |
| 23 |
if (!props.id) { |
| 24 |
// eslint-disable-next-line no-console |
| 25 |
console.error('Radio:', 'id is required to make input accessible'); |
| 26 |
} |
| 27 |
return (_jsx(SSRSafeIds, { prefix: "pf-radio-description-", ouiaComponentType: "Radio", children: (descriptionId, generatedOuiaId) => { |
| 28 |
let ariaDescribedByValue; |
| 29 |
if (ariaDescribedBy !== undefined) { |
| 30 |
ariaDescribedByValue = ariaDescribedBy === '' ? undefined : ariaDescribedBy; |
| 31 |
} |
| 32 |
else if (description) { |
| 33 |
ariaDescribedByValue = descriptionId; |
| 34 |
} |
| 35 |
const inputRendered = (_jsx("input", Object.assign({}, props, { className: css(styles.radioInput, inputClassName), type: "radio", onChange: this.handleChange, "aria-invalid": !isValid, "aria-describedby": ariaDescribedByValue, disabled: isDisabled, checked: checked || isChecked }, (checked === undefined && { defaultChecked }), (!label && { 'aria-label': ariaLabel }), getOUIAProps(Radio.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe)))); |
| 36 |
const wrapWithLabel = (isLabelWrapped && !component) || component === 'label'; |
| 37 |
const Label = wrapWithLabel ? 'span' : 'label'; |
| 38 |
const labelRendered = label ? (_jsx(Label, { className: css(styles.radioLabel, isDisabled && styles.modifiers.disabled), htmlFor: !wrapWithLabel ? props.id : undefined, children: label })) : null; |
| 39 |
const WrapperComponent = component !== null && component !== void 0 ? component : (wrapWithLabel ? 'label' : 'div'); |
| 40 |
return (_jsxs(WrapperComponent, { className: css(styles.radio, !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.radioDescription), children: description })), body && _jsx("span", { className: css(styles.radioBody), children: body })] })); |
| 41 |
} })); |
| 42 |
} |
| 43 |
} |
| 44 |
Radio.displayName = 'Radio'; |
| 45 |
Radio.defaultProps = { |
| 46 |
className: '', |
| 47 |
isDisabled: false, |
| 48 |
isValid: true, |
| 49 |
onChange: () => { } |
| 50 |
}; |
| 51 |
export { Radio }; |
| 52 |
//# sourceMappingURL=Radio.js.map |