UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

117 lines (113 loc) 3.44 kB
/** * MSKCC 2021, 2024 */ import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js'; import PropTypes from 'prop-types'; import React__default, { useRef } from 'react'; import cx from 'classnames'; import '../Text/index.js'; import { usePrefix } from '../../internal/usePrefix.js'; import { useId } from '../../internal/useId.js'; import mergeRefs from '../../tools/mergeRefs.js'; import { Text } from '../Text/Text.js'; const RadioButton = /*#__PURE__*/React__default.forwardRef((props, ref) => { const { className, disabled, hideLabel, id, labelPosition = 'right', labelText = '', name, onChange = () => {}, value = '', ...rest } = props; const prefix = usePrefix(); const uid = useId('radio-button'); const uniqueId = id || uid; function handleOnChange(event) { onChange(value, name, event); } const innerLabelClasses = cx(`${prefix}--radio-button__label-text`, { [`${prefix}--visually-hidden`]: hideLabel }); const wrapperClasses = cx(className, `${prefix}--radio-button-wrapper`, { [`${prefix}--radio-button-wrapper--label-${labelPosition}`]: labelPosition !== 'right' }); const inputRef = useRef(null); return /*#__PURE__*/React__default.createElement("div", { className: wrapperClasses }, /*#__PURE__*/React__default.createElement("input", _extends({}, rest, { type: "radio", className: `${prefix}--radio-button`, onChange: handleOnChange, id: uniqueId, ref: mergeRefs(inputRef, ref), disabled: disabled, value: value, name: name })), /*#__PURE__*/React__default.createElement("label", { htmlFor: uniqueId, className: `${prefix}--radio-button__label` }, /*#__PURE__*/React__default.createElement("span", { className: `${prefix}--radio-button__appearance` }), labelText && /*#__PURE__*/React__default.createElement(Text, { className: innerLabelClasses }, labelText))); }); RadioButton.displayName = 'RadioButton'; RadioButton.propTypes = { /** * Specify whether the `<RadioButton>` is currently checked */ checked: PropTypes.bool, /** * Provide an optional className to be applied to the containing node */ className: PropTypes.string, /** * Specify whether the `<RadioButton>` should be checked by default */ defaultChecked: PropTypes.bool, /** * Specify whether the control is disabled */ disabled: PropTypes.bool, /** * Specify whether the label should be hidden, or not */ hideLabel: PropTypes.bool, /** * Provide a unique id for the underlying `<input>` node */ id: PropTypes.string, /** * Provide where label text should be placed * NOTE: `top`/`bottom` are deprecated */ labelPosition: PropTypes.oneOf(['right', 'left']), /** * Provide label text to be read by screen readers when interacting with the * control */ labelText: PropTypes.node.isRequired, /** * Provide a name for the underlying `<input>` node */ name: PropTypes.string, /** * Provide an optional `onChange` hook that is called each time the value of * the underlying `<input>` changes */ onChange: PropTypes.func, /** * Provide a handler that is invoked when a user clicks on the control */ onClick: PropTypes.func, /** * Specify the value of the `<RadioButton>` */ value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]) }; export { RadioButton as default };