@carbon/react
Version:
React components for the Carbon Design System
148 lines (144 loc) • 4.85 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.
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import PropTypes from 'prop-types';
import React, { useRef, cloneElement } from 'react';
import cx from 'classnames';
import '../Text/index.js';
import { deprecate } from '../../prop-types/deprecate.js';
import { usePrefix } from '../../internal/usePrefix.js';
import { useId } from '../../internal/useId.js';
import mergeRefs from '../../tools/mergeRefs.js';
import { AILabel } from '../AILabel/index.js';
import { isComponentElement } from '../../internal/utils.js';
import { Text } from '../Text/Text.js';
const RadioButton = /*#__PURE__*/React.forwardRef((props, ref) => {
const {
className,
decorator,
disabled,
hideLabel,
id,
labelPosition = 'right',
labelText = '',
name,
onChange = () => {},
value = '',
slug,
required,
...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',
[`${prefix}--radio-button-wrapper--slug`]: slug,
[`${prefix}--radio-button-wrapper--decorator`]: decorator
});
const inputRef = useRef(null);
const candidate = slug ?? decorator;
const candidateIsAILabel = isComponentElement(candidate, AILabel);
const normalizedDecorator = candidateIsAILabel ? /*#__PURE__*/cloneElement(candidate, {
size: candidate.props?.['kind'] === 'inline' ? 'md' : 'mini'
}) : null;
return /*#__PURE__*/React.createElement("div", {
className: wrapperClasses
}, /*#__PURE__*/React.createElement("input", _extends({}, rest, {
type: "radio",
className: `${prefix}--radio-button`,
onChange: handleOnChange,
id: uniqueId,
ref: mergeRefs(inputRef, ref),
disabled: disabled,
value: value,
name: name,
required: required
})), /*#__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, {
className: innerLabelClasses
}, labelText, slug ? normalizedDecorator : decorator ? /*#__PURE__*/React.createElement("div", {
className: `${prefix}--radio-button-wrapper-inner--decorator`
}, normalizedDecorator) : '')));
});
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,
/**
* **Experimental**: Provide a `Slug` component to be rendered inside the `RadioButton` component
*/
slug: 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])
};
export { RadioButton as default };