UNPKG

@carbon/react

Version:

React components for the Carbon Design System

111 lines (109 loc) 4.22 kB
/** * Copyright IBM Corp. 2016, 2026 * * 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 { usePrefix } from "../../internal/usePrefix.js"; import { Text } from "../Text/Text.js"; import { useId } from "../../internal/useId.js"; import { deprecate } from "../../prop-types/deprecate.js"; import { isComponentElement } from "../../internal/utils.js"; import { useNormalizedInputProps } from "../../internal/useNormalizedInputProps.js"; import { AILabel } from "../AILabel/index.js"; import { mergeRefs } from "../../tools/mergeRefs.js"; import classNames from "classnames"; import React, { cloneElement, useRef } from "react"; import PropTypes from "prop-types"; import { jsx, jsxs } from "react/jsx-runtime"; //#region src/components/RadioButton/RadioButton.tsx /** * Copyright IBM Corp. 2016, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const RadioButton = 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(); const uid = useId("radio-button"); const uniqueId = id || uid; const normalizedProps = useNormalizedInputProps({ id: uniqueId, readOnly, disabled, invalid, invalidText, warn, warnText }); function handleOnChange(event) { onChange(value, name, event); } const innerLabelClasses = classNames(`${prefix}--radio-button__label-text`, { [`${prefix}--visually-hidden`]: hideLabel }); const wrapperClasses = classNames(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 = useRef(null); const candidate = slug ?? decorator; const normalizedDecorator = isComponentElement(candidate, AILabel) ? cloneElement(candidate, { size: candidate.props?.["kind"] === "inline" ? "md" : "mini" }) : candidate; return /* @__PURE__ */ jsxs("div", { className: wrapperClasses, children: [ /* @__PURE__ */ jsx("input", { ...rest, type: "radio", className: `${prefix}--radio-button`, onChange: handleOnChange, id: uniqueId, ref: mergeRefs(inputRef, ref), disabled: normalizedProps.disabled, value, name, required, readOnly }), /* @__PURE__ */ jsxs("label", { htmlFor: uniqueId, className: `${prefix}--radio-button__label`, children: [/* @__PURE__ */ jsx("span", { className: `${prefix}--radio-button__appearance` }), labelText && /* @__PURE__ */ jsxs(Text, { className: innerLabelClasses, children: [labelText, slug ? normalizedDecorator : decorator ? /* @__PURE__ */ jsx("div", { className: `${prefix}--radio-button-wrapper-inner--decorator`, children: normalizedDecorator }) : ""] })] }), normalizedProps.validation ] }); }); RadioButton.displayName = "RadioButton"; RadioButton.propTypes = { checked: PropTypes.bool, className: PropTypes.string, decorator: PropTypes.node, defaultChecked: PropTypes.bool, disabled: PropTypes.bool, hideLabel: PropTypes.bool, id: PropTypes.string, labelPosition: PropTypes.oneOf(["right", "left"]), labelText: PropTypes.node.isRequired, name: PropTypes.string, onChange: PropTypes.func, onClick: PropTypes.func, required: PropTypes.bool, invalid: PropTypes.bool, invalidText: PropTypes.node, warn: PropTypes.bool, warnText: PropTypes.node, readOnly: PropTypes.bool, slug: deprecate(PropTypes.node, "The `slug` prop has been deprecated and will be removed in the next major version. Use the decorator prop instead."), value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]) }; //#endregion export { RadioButton as default };