UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

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