1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import clsx from 'clsx';
|
6 | import composeClasses from '@mui/utils/composeClasses';
|
7 | import FormGroup from "../FormGroup/index.js";
|
8 | import { getRadioGroupUtilityClass } from "./radioGroupClasses.js";
|
9 | import useForkRef from "../utils/useForkRef.js";
|
10 | import useControlled from "../utils/useControlled.js";
|
11 | import RadioGroupContext from "./RadioGroupContext.js";
|
12 | import useId from "../utils/useId.js";
|
13 | import { jsx as _jsx } from "react/jsx-runtime";
|
14 | const 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 | };
|
25 | const RadioGroup = React.forwardRef(function RadioGroup(props, ref) {
|
26 | const {
|
27 |
|
28 |
|
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 _jsx(RadioGroupContext.Provider, {
|
69 | value: contextValue,
|
70 | children: _jsx(FormGroup, {
|
71 | role: "radiogroup",
|
72 | ref: handleRef,
|
73 | className: clsx(classes.root, className),
|
74 | ...other,
|
75 | children: children
|
76 | })
|
77 | });
|
78 | });
|
79 | process.env.NODE_ENV !== "production" ? RadioGroup.propTypes = {
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | |
85 |
|
86 |
|
87 | children: PropTypes.node,
|
88 | |
89 |
|
90 |
|
91 | className: PropTypes.string,
|
92 | |
93 |
|
94 |
|
95 | defaultValue: PropTypes.any,
|
96 | |
97 |
|
98 |
|
99 |
|
100 | name: PropTypes.string,
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | onChange: PropTypes.func,
|
109 | |
110 |
|
111 |
|
112 | value: PropTypes.any
|
113 | } : void 0;
|
114 | export default RadioGroup; |
\ | No newline at end of file |