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