@carbon/react
Version:
React components for the Carbon Design System
158 lines (150 loc) • 5.83 kB
JavaScript
/**
* 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');
require('../Text/index.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 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,
decorator,
disabled,
hideLabel,
id,
labelPosition = 'right',
labelText = '',
name,
onChange = () => {},
value = '',
slug,
required,
...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',
[`${prefix}--radio-button-wrapper--slug`]: slug,
[`${prefix}--radio-button-wrapper--decorator`]: decorator
});
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'
}) : 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,
required: required
})), /*#__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, slug ? normalizedDecorator : decorator ? /*#__PURE__*/React__default["default"].createElement("div", {
className: `${prefix}--radio-button-wrapper-inner--decorator`
}, normalizedDecorator) : '')));
});
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,
/**
* **Experimental**: Provide a decorator component to be rendered inside the `RadioButton` component
*/
decorator: PropTypes__default["default"].node,
/**
* 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,
/**
* `true` to specify if the control is required.
*/
required: PropTypes__default["default"].bool,
/**
* **Experimental**: Provide a `Slug` component to be rendered inside the `RadioButton` component
*/
slug: deprecate["default"](PropTypes__default["default"].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__default["default"].oneOfType([PropTypes__default["default"].string, PropTypes__default["default"].number])
};
exports["default"] = RadioButton;