UNPKG

@carbon/react

Version:

React components for the Carbon Design System

221 lines (215 loc) 7.9 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. */ import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js'; import PropTypes from 'prop-types'; import React, { useState, useRef, cloneElement } from 'react'; import cx from 'classnames'; import { Legend } from '../Text/index.js'; import { usePrefix } from '../../internal/usePrefix.js'; import { WarningFilled, WarningAltFilled } from '@carbon/icons-react'; import { deprecate } from '../../prop-types/deprecate.js'; import mergeRefs from '../../tools/mergeRefs.js'; import { useId } from '../../internal/useId.js'; import { AILabel } from '../AILabel/index.js'; import { isComponentElement } from '../../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(); const [selected, setSelected] = useState(valueSelected ?? defaultSelected); const [prevValueSelected, setPrevValueSelected] = useState(valueSelected); const radioButtonGroupInstanceId = 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 && !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 && 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 helperId = !helperText ? undefined : `radio-button-group-helper-text-${radioButtonGroupInstanceId}`; const helper = helperText ? /*#__PURE__*/React.createElement("div", { id: helperId, className: helperClasses }, helperText) : null; const divRef = useRef(null); // AILabel is always size `mini` const candidate = slug ?? decorator; const candidateIsAILabel = isComponentElement(candidate, AILabel); const normalizedDecorator = candidateIsAILabel ? /*#__PURE__*/cloneElement(candidate, { size: 'mini', kind: 'default' }) : null; return /*#__PURE__*/React.createElement("div", { className: wrapperClasses, ref: mergeRefs(divRef, ref) }, /*#__PURE__*/React.createElement("fieldset", _extends({ className: fieldsetClasses, disabled: disabled, "data-invalid": invalid ? true : undefined, "aria-describedby": showHelper && helperText ? helperId : undefined }, rest), legendText && /*#__PURE__*/React.createElement(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 && invalid && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(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(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(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'; export { RadioButtonGroup as default };