UNPKG

@carbon/react

Version:

React components for the Carbon Design System

226 lines (218 loc) 8.19 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var PropTypes = require('prop-types'); var React = require('react'); var cx = require('classnames'); var createTextComponent = require('../Text/createTextComponent.js'); var usePrefix = require('../../internal/usePrefix.js'); var iconsReact = require('@carbon/icons-react'); var deprecate = require('../../prop-types/deprecate.js'); var mergeRefs = require('../../tools/mergeRefs.js'); var useId = require('../../internal/useId.js'); var index = require('../AILabel/index.js'); var utils = require('../../internal/utils.js'); const RadioButtonGroup = /*#__PURE__*/React.forwardRef((props, ref) => { const { children, className, decorator, defaultSelected, disabled, helperText, invalid = false, invalidText, labelPosition = 'right', legendText, name, onChange = () => {}, orientation = 'horizontal', readOnly, valueSelected, warn = false, warnText, slug, required, ...rest } = props; const prefix = usePrefix.usePrefix(); const [selected, setSelected] = React.useState(valueSelected ?? defaultSelected); const [prevValueSelected, setPrevValueSelected] = React.useState(valueSelected); const radioButtonGroupInstanceId = useId.useId(); /** * prop + state alignment - getDerivedStateFromProps * only update if selected prop changes */ if (valueSelected !== prevValueSelected) { setSelected(valueSelected); setPrevValueSelected(valueSelected); } function getRadioButtons() { const mappedChildren = React.Children.map(children, radioButton => { if (!radioButton) { return; } const newProps = { name: name, key: radioButton.props.value, value: radioButton.props.value, onChange: handleOnChange, checked: radioButton.props.value === selected, required: required }; if (!selected && radioButton.props.checked) { newProps.checked = true; } return /*#__PURE__*/React.cloneElement(radioButton, newProps); }); return mappedChildren; } function handleOnChange(newSelection, value, evt) { if (!readOnly) { if (newSelection !== selected) { setSelected(newSelection); onChange(newSelection, name, evt); } } } const showWarning = !readOnly && !disabled && !invalid && warn; const showHelper = !invalid && !disabled && !warn; const wrapperClasses = cx(`${prefix}--form-item`, className); const fieldsetClasses = cx(`${prefix}--radio-button-group`, { [`${prefix}--radio-button-group--${orientation}`]: orientation === 'vertical', [`${prefix}--radio-button-group--label-${labelPosition}`]: labelPosition, [`${prefix}--radio-button-group--readonly`]: readOnly, [`${prefix}--radio-button-group--invalid`]: !readOnly && !disabled && invalid, [`${prefix}--radio-button-group--warning`]: showWarning, [`${prefix}--radio-button-group--slug`]: slug, [`${prefix}--radio-button-group--decorator`]: decorator }); const helperClasses = cx(`${prefix}--form__helper-text`, { [`${prefix}--form__helper-text--disabled`]: disabled }); const hasHelper = typeof helperText !== 'undefined' && helperText !== null; const helperId = !hasHelper ? undefined : `radio-button-group-helper-text-${radioButtonGroupInstanceId}`; const helper = hasHelper && /*#__PURE__*/React.createElement("div", { id: helperId, className: helperClasses }, helperText); const divRef = React.useRef(null); // AILabel is always size `mini` const candidate = slug ?? decorator; const candidateIsAILabel = utils.isComponentElement(candidate, index.AILabel); const normalizedDecorator = candidateIsAILabel ? /*#__PURE__*/React.cloneElement(candidate, { size: 'mini', kind: 'default' }) : candidate; return /*#__PURE__*/React.createElement("div", { className: wrapperClasses, ref: mergeRefs.mergeRefs(divRef, ref) }, /*#__PURE__*/React.createElement("fieldset", _rollupPluginBabelHelpers.extends({ className: fieldsetClasses, disabled: disabled, "data-invalid": invalid ? true : undefined, "aria-describedby": showHelper && helperText ? helperId : undefined }, rest), legendText && /*#__PURE__*/React.createElement(createTextComponent.Legend, { className: `${prefix}--label` }, legendText, slug ? normalizedDecorator : decorator ? /*#__PURE__*/React.createElement("div", { className: `${prefix}--radio-button-group-inner--decorator` }, normalizedDecorator) : ''), getRadioButtons()), /*#__PURE__*/React.createElement("div", { className: `${prefix}--radio-button__validation-msg` }, !readOnly && !disabled && invalid && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(iconsReact.WarningFilled, { className: `${prefix}--radio-button__invalid-icon` }), /*#__PURE__*/React.createElement("div", { className: `${prefix}--form-requirement` }, invalidText)), showWarning && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(iconsReact.WarningAltFilled, { className: `${prefix}--radio-button__invalid-icon ${prefix}--radio-button__invalid-icon--warning` }), /*#__PURE__*/React.createElement("div", { className: `${prefix}--form-requirement` }, warnText))), showHelper && helper); }); RadioButtonGroup.propTypes = { /** * Provide a collection of `<RadioButton>` components to render in the group */ children: PropTypes.node, /** * Provide an optional className to be applied to the container node */ className: PropTypes.string, /** * **Experimental**: Provide a decorator component to be rendered inside the `RadioButtonGroup` component */ decorator: PropTypes.node, /** * Specify the `<RadioButton>` to be selected by default */ defaultSelected: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), /** * Specify whether the group is disabled */ disabled: PropTypes.bool, /** * Provide text that is used alongside the control label for additional help */ helperText: PropTypes.node, /** * Specify whether the control is currently invalid */ invalid: PropTypes.bool, /** * Provide the text that is displayed when the control is in an invalid state */ invalidText: PropTypes.node, /** * Provide where label text should be placed */ labelPosition: PropTypes.oneOf(['left', 'right']), /** * Provide a legend to the RadioButtonGroup input that you are * exposing to the user */ legendText: PropTypes.node, /** * Specify the name of the underlying `<input>` nodes */ name: PropTypes.string.isRequired, /** * Provide an optional `onChange` hook that is called whenever the value of * the group changes */ onChange: PropTypes.func, /** * Provide where radio buttons should be placed */ orientation: PropTypes.oneOf(['horizontal', 'vertical']), /** * Whether the RadioButtonGroup should be read-only */ readOnly: PropTypes.bool, /** * `true` to specify if radio selection in group is required. */ required: PropTypes.bool, /** * **Experimental**: Provide a `Slug` component to be rendered inside the `RadioButtonGroup` component */ slug: deprecate.deprecate(PropTypes.node, 'The `slug` prop has been deprecated and will be removed in the next major version. Use the decorator prop instead.'), /** * Specify the value that is currently selected in the group */ valueSelected: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), /** * Specify whether the control is currently in warning state */ warn: PropTypes.bool, /** * Provide the text that is displayed when the control is in warning state */ warnText: PropTypes.node }; RadioButtonGroup.displayName = 'RadioButtonGroup'; exports.default = RadioButtonGroup;