UNPKG

3.78 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import composeClasses from '@mui/utils/composeClasses';
7import FormGroup from "../FormGroup/index.js";
8import { getRadioGroupUtilityClass } from "./radioGroupClasses.js";
9import useForkRef from "../utils/useForkRef.js";
10import useControlled from "../utils/useControlled.js";
11import RadioGroupContext from "./RadioGroupContext.js";
12import useId from "../utils/useId.js";
13import { jsx as _jsx } from "react/jsx-runtime";
14const useUtilityClasses = props => {
15 const {
16 classes,
17 row,
18 error
19 } = props;
20 const slots = {
21 root: ['root', row && 'row', error && 'error']
22 };
23 return composeClasses(slots, getRadioGroupUtilityClass, classes);
24};
25const RadioGroup = /*#__PURE__*/React.forwardRef(function RadioGroup(props, ref) {
26 const {
27 // private
28 // eslint-disable-next-line react/prop-types
29 actions,
30 children,
31 className,
32 defaultValue,
33 name: nameProp,
34 onChange,
35 value: valueProp,
36 ...other
37 } = props;
38 const rootRef = React.useRef(null);
39 const classes = useUtilityClasses(props);
40 const [value, setValueState] = useControlled({
41 controlled: valueProp,
42 default: defaultValue,
43 name: 'RadioGroup'
44 });
45 React.useImperativeHandle(actions, () => ({
46 focus: () => {
47 let input = rootRef.current.querySelector('input:not(:disabled):checked');
48 if (!input) {
49 input = rootRef.current.querySelector('input:not(:disabled)');
50 }
51 if (input) {
52 input.focus();
53 }
54 }
55 }), []);
56 const handleRef = useForkRef(ref, rootRef);
57 const name = useId(nameProp);
58 const contextValue = React.useMemo(() => ({
59 name,
60 onChange(event) {
61 setValueState(event.target.value);
62 if (onChange) {
63 onChange(event, event.target.value);
64 }
65 },
66 value
67 }), [name, onChange, setValueState, value]);
68 return /*#__PURE__*/_jsx(RadioGroupContext.Provider, {
69 value: contextValue,
70 children: /*#__PURE__*/_jsx(FormGroup, {
71 role: "radiogroup",
72 ref: handleRef,
73 className: clsx(classes.root, className),
74 ...other,
75 children: children
76 })
77 });
78});
79process.env.NODE_ENV !== "production" ? RadioGroup.propTypes /* remove-proptypes */ = {
80 // ┌────────────────────────────── Warning ──────────────────────────────┐
81 // │ These PropTypes are generated from the TypeScript type definitions. │
82 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
83 // └─────────────────────────────────────────────────────────────────────┘
84 /**
85 * The content of the component.
86 */
87 children: PropTypes.node,
88 /**
89 * @ignore
90 */
91 className: PropTypes.string,
92 /**
93 * The default value. Use when the component is not controlled.
94 */
95 defaultValue: PropTypes.any,
96 /**
97 * The name used to reference the value of the control.
98 * If you don't provide this prop, it falls back to a randomly generated name.
99 */
100 name: PropTypes.string,
101 /**
102 * Callback fired when a radio button is selected.
103 *
104 * @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
105 * @param {string} value The value of the selected radio button.
106 * You can pull out the new value by accessing `event.target.value` (string).
107 */
108 onChange: PropTypes.func,
109 /**
110 * Value of the selected radio button. The DOM API casts this to a string.
111 */
112 value: PropTypes.any
113} : void 0;
114export default RadioGroup;
\No newline at end of file