UNPKG

@carbon/react

Version:

React components for the Carbon Design System

190 lines (184 loc) 6.1 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 Text = require('../Text/Text.js'); require('../Text/TextDirection.js'); var deprecate = require('../../prop-types/deprecate.js'); var usePrefix = require('../../internal/usePrefix.js'); var useId = require('../../internal/useId.js'); var mergeRefs = require('../../tools/mergeRefs.js'); var index = require('../AILabel/index.js'); var utils = require('../../internal/utils.js'); var useNormalizedInputProps = require('../../internal/useNormalizedInputProps.js'); const RadioButton = /*#__PURE__*/React.forwardRef((props, ref) => { const { className, decorator, disabled = false, hideLabel, id, labelPosition = 'right', labelText = '', name, onChange = () => {}, value = '', slug, required, invalid = false, invalidText, warn = false, warnText, readOnly, ...rest } = props; const prefix = usePrefix.usePrefix(); const uid = useId.useId('radio-button'); const uniqueId = id || uid; const normalizedProps = useNormalizedInputProps.useNormalizedInputProps({ id: uniqueId, readOnly, disabled, invalid, invalidText, warn, warnText }); 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', [`${prefix}--radio-button-wrapper--slug`]: slug, [`${prefix}--radio-button-wrapper--decorator`]: decorator, [`${prefix}--radio-button-wrapper--invalid`]: normalizedProps.invalid, [`${prefix}--radio-button-wrapper--warning`]: normalizedProps.warn }); const inputRef = React.useRef(null); const candidate = slug ?? decorator; const candidateIsAILabel = utils.isComponentElement(candidate, index.AILabel); const normalizedDecorator = candidateIsAILabel ? /*#__PURE__*/React.cloneElement(candidate, { size: candidate.props?.['kind'] === 'inline' ? 'md' : 'mini' }) : candidate; return /*#__PURE__*/React.createElement("div", { className: wrapperClasses }, /*#__PURE__*/React.createElement("input", _rollupPluginBabelHelpers.extends({}, rest, { type: "radio", className: `${prefix}--radio-button`, onChange: handleOnChange, id: uniqueId, ref: mergeRefs.mergeRefs(inputRef, ref), disabled: normalizedProps.disabled, value: value, name: name, required: required, readOnly: readOnly })), /*#__PURE__*/React.createElement("label", { htmlFor: uniqueId, className: `${prefix}--radio-button__label` }, /*#__PURE__*/React.createElement("span", { className: `${prefix}--radio-button__appearance` }), labelText && /*#__PURE__*/React.createElement(Text.Text, { className: innerLabelClasses }, labelText, slug ? normalizedDecorator : decorator ? /*#__PURE__*/React.createElement("div", { className: `${prefix}--radio-button-wrapper-inner--decorator` }, normalizedDecorator) : '')), normalizedProps.validation); }); 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, /** * **Experimental**: Provide a decorator component to be rendered inside the `RadioButton` component */ decorator: PropTypes.node, /** * 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, /** * `true` to specify if the control is required. */ required: PropTypes.bool, /** * 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, /** * 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, /** * Specify whether the RadioButton should be read-only */ readOnly: PropTypes.bool, /** * **Experimental**: Provide a `Slug` component to be rendered inside the `RadioButton` 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 of the `<RadioButton>` */ value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]) }; exports.default = RadioButton;