UNPKG

@base-ui-components/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

121 lines (119 loc) 4.13 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useComponentRenderer } from '../../utils/useComponentRenderer.js'; import { useRadioGroupContext } from '../../radio-group/RadioGroupContext.js'; import { useRadioRoot } from './useRadioRoot.js'; import { RadioRootContext } from './RadioRootContext.js'; import { CompositeItem } from '../../composite/item/CompositeItem.js'; import { NOOP } from '../../utils/noop.js'; import { useFieldRootContext } from '../../field/root/FieldRootContext.js'; import { customStyleHookMapping } from '../utils/customStyleHookMapping.js'; /** * Represents the radio button itself. * Renders a `<button>` element and a hidden `<input>` beside. * * Documentation: [Base UI Radio](https://base-ui.com/react/components/radio) */ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const RadioRoot = /*#__PURE__*/React.forwardRef(function RadioRoot(props, forwardedRef) { const { render, className, disabled: disabledProp = false, readOnly: readOnlyProp = false, required: requiredProp = false, ...otherProps } = props; const { disabled: disabledRoot, readOnly: readOnlyRoot, required: requiredRoot, setCheckedValue } = useRadioGroupContext(); const { state: fieldState, disabled: fieldDisabled } = useFieldRootContext(); const disabled = fieldDisabled || disabledRoot || disabledProp; const readOnly = readOnlyRoot || readOnlyProp; const required = requiredRoot || requiredProp; const { getRootProps, getInputProps, checked } = useRadioRoot({ ...props, disabled, readOnly }); const state = React.useMemo(() => ({ ...fieldState, required, disabled, readOnly, checked }), [fieldState, disabled, readOnly, checked, required]); const contextValue = React.useMemo(() => state, [state]); const { renderElement } = useComponentRenderer({ propGetter: getRootProps, render: render ?? 'button', ref: forwardedRef, className, state, extraProps: otherProps, customStyleHookMapping }); return /*#__PURE__*/_jsxs(RadioRootContext.Provider, { value: contextValue, children: [setCheckedValue === NOOP ? renderElement() : /*#__PURE__*/_jsx(CompositeItem, { render: renderElement() }), /*#__PURE__*/_jsx("input", { ...getInputProps() })] }); }); process.env.NODE_ENV !== "production" ? RadioRoot.propTypes /* remove-proptypes */ = { // ┌────────────────────────────── Warning ──────────────────────────────┐ // │ These PropTypes are generated from the TypeScript type definitions. │ // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ // └─────────────────────────────────────────────────────────────────────┘ /** * @ignore */ children: PropTypes.node, /** * CSS class applied to the element, or a function that * returns a class based on the component’s state. */ className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]), /** * Whether the component should ignore user interaction. * @default false */ disabled: PropTypes.bool, /** * Whether the user should be unable to select the radio button. * @default false */ readOnly: PropTypes.bool, /** * Allows you to replace the component’s HTML element * with a different tag, or compose it with another component. * * Accepts a `ReactElement` or a function that returns the element to render. */ render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]), /** * Whether the user must choose a value before submitting a form. * @default false */ required: PropTypes.bool, /** * The unique identifying value of the radio in a group. */ value: PropTypes.any.isRequired } : void 0; export { RadioRoot };