UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

197 lines (192 loc) 6.89 kB
/** * MSKCC 2021, 2024 */ import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js'; import PropTypes from 'prop-types'; import React__default, { createContext, useState, useRef } from 'react'; import cx from 'classnames'; import { Legend } from '../Text/index.js'; import { usePrefix } from '../../internal/usePrefix.js'; import mergeRefs from '../../tools/mergeRefs.js'; import setupGetInstanceId from '../../tools/setupGetInstanceId.js'; const getInstanceId = setupGetInstanceId(); const RadioButtonGroupContext = /*#__PURE__*/createContext(null); const RadioButtonGroup = /*#__PURE__*/React__default.forwardRef((props, ref) => { const { children, className, defaultSelected, disabled, helperText, invalid = false, invalidText, labelPosition = 'right', legendText, name, onChange = () => {}, orientation = 'horizontal', readOnly, radioSize = 'sm', valueSelected, warn = false, warnText, ...rest } = props; const prefix = usePrefix(); const [selected, setSelected] = useState(valueSelected ?? defaultSelected); const [prevValueSelected, setPrevValueSelected] = useState(valueSelected); const { current: radioButtonGroupInstanceId } = useRef(getInstanceId()); /** * prop + state alignment - getDerivedStateFromProps * only update if selected prop changes */ if (valueSelected !== prevValueSelected) { setSelected(valueSelected); setPrevValueSelected(valueSelected); } function getRadioButtons() { const mappedChildren = React__default.Children.map(children, radioButton => { const { value } = radioButton?.props ?? undefined; const newProps = { name: name, key: value, value: value, onChange: handleOnChange, checked: value === selected }; if (!selected && radioButton?.props.checked) { newProps.checked = true; } if (radioButton) { return /*#__PURE__*/React__default.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}--fieldset ${prefix}--radio-button-group`, `msk-radio-button-group--${radioSize}`, { [`${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 }); 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__default.createElement("div", { id: helperId, className: helperClasses }, helperText) : null; const divRef = useRef(null); return /*#__PURE__*/React__default.createElement("div", { className: wrapperClasses, ref: mergeRefs(divRef, ref) }, /*#__PURE__*/React__default.createElement("fieldset", _extends({ className: fieldsetClasses, disabled: disabled, "data-invalid": invalid ? true : undefined, "aria-describedby": showHelper && helperText ? helperId : undefined }, rest), legendText && /*#__PURE__*/React__default.createElement(Legend, { className: `${prefix}--label` }, legendText), getRadioButtons()), /*#__PURE__*/React__default.createElement("div", { className: `${prefix}--radio-button__validation-msg` }, !readOnly && invalid && /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("span", { className: `${prefix}--radio-button__invalid-icon msk-icon` }, "error"), /*#__PURE__*/React__default.createElement("div", { className: `${prefix}--form-requirement` }, invalidText)), showWarning && /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("span", { className: `${prefix}--radio-button__invalid-icon ${prefix}--radio-button__invalid-icon--warning msk-icon` }, "warning"), /*#__PURE__*/React__default.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, /** * 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']), /** * Set the size of the radio button */ radioSize: PropTypes.oneOf(['sm', 'md', 'lg']), /** * Whether the RadioButtonGroup should be read-only */ readOnly: PropTypes.bool, /** * 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 { RadioButtonGroupContext, RadioButtonGroup as default };