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.

140 lines (138 loc) 4.66 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { CompositeRoot } from '../composite/root/CompositeRoot.js'; import { useComponentRenderer } from '../utils/useComponentRenderer.js'; import { useEventCallback } from '../utils/useEventCallback.js'; import { useDirection } from '../direction-provider/DirectionContext.js'; import { useRadioGroup } from './useRadioGroup.js'; import { RadioGroupContext } from './RadioGroupContext.js'; import { useFieldRootContext } from '../field/root/FieldRootContext.js'; /** * Provides a shared state to a series of radio buttons. * Renders a `<div>` element. * * Documentation: [Base UI Radio Group](https://base-ui.com/react/components/radio) */ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const RadioGroup = /*#__PURE__*/React.forwardRef(function RadioGroup(props, forwardedRef) { const { render, className, disabled: disabledProp, readOnly, required, onValueChange: onValueChangeProp, name, ...otherProps } = props; const direction = useDirection(); const { getRootProps, getInputProps, checkedValue, setCheckedValue, touched, setTouched } = useRadioGroup(props); const { state: fieldState, disabled: fieldDisabled } = useFieldRootContext(); const disabled = fieldDisabled || disabledProp; const onValueChange = useEventCallback(onValueChangeProp ?? (() => {})); const state = React.useMemo(() => ({ ...fieldState, disabled: disabled ?? false, required: required ?? false, readOnly: readOnly ?? false }), [fieldState, disabled, readOnly, required]); const contextValue = React.useMemo(() => ({ checkedValue, setCheckedValue, onValueChange, disabled, readOnly, required, touched, setTouched }), [checkedValue, setCheckedValue, onValueChange, disabled, readOnly, required, touched, setTouched]); const { renderElement } = useComponentRenderer({ propGetter: getRootProps, render: render ?? 'div', ref: forwardedRef, className, state, extraProps: otherProps }); return /*#__PURE__*/_jsxs(RadioGroupContext.Provider, { value: contextValue, children: [/*#__PURE__*/_jsx(CompositeRoot, { direction: direction, enableHomeAndEndKeys: false, render: renderElement() }), /*#__PURE__*/_jsx("input", { ...getInputProps() })] }); }); process.env.NODE_ENV !== "production" ? RadioGroup.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]), /** * The uncontrolled value of the radio button that should be initially selected. * * To render a controlled radio group, use the `value` prop instead. */ defaultValue: PropTypes.any, /** * Whether the component should ignore user interaction. * @default false */ disabled: PropTypes.bool, /** * Identifies the field when a form is submitted. */ name: PropTypes.string, /** * Callback fired when the value changes. */ onValueChange: PropTypes.func, /** * Whether the user should be unable to select a different radio button in the group. * @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 controlled value of the radio item that should be currently selected. * * To render an uncontrolled radio group, use the `defaultValue` prop instead. */ value: PropTypes.any } : void 0; export { RadioGroup };