1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import clsx from 'clsx';
|
6 | import refType from '@mui/utils/refType';
|
7 | import composeClasses from '@mui/utils/composeClasses';
|
8 | import { alpha } from '@mui/system/colorManipulator';
|
9 | import SwitchBase from "../internal/SwitchBase.js";
|
10 | import RadioButtonIcon from "./RadioButtonIcon.js";
|
11 | import capitalize from "../utils/capitalize.js";
|
12 | import createChainedFunction from "../utils/createChainedFunction.js";
|
13 | import useFormControl from "../FormControl/useFormControl.js";
|
14 | import useRadioGroup from "../RadioGroup/useRadioGroup.js";
|
15 | import radioClasses, { getRadioUtilityClass } from "./radioClasses.js";
|
16 | import rootShouldForwardProp from "../styles/rootShouldForwardProp.js";
|
17 | import { styled } from "../zero-styled/index.js";
|
18 | import memoTheme from "../utils/memoTheme.js";
|
19 | import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
|
20 | import { useDefaultProps } from "../DefaultPropsProvider/index.js";
|
21 | import { jsx as _jsx } from "react/jsx-runtime";
|
22 | const useUtilityClasses = ownerState => {
|
23 | const {
|
24 | classes,
|
25 | color,
|
26 | size
|
27 | } = ownerState;
|
28 | const slots = {
|
29 | root: ['root', `color${capitalize(color)}`, size !== 'medium' && `size${capitalize(size)}`]
|
30 | };
|
31 | return {
|
32 | ...classes,
|
33 | ...composeClasses(slots, getRadioUtilityClass, classes)
|
34 | };
|
35 | };
|
36 | const RadioRoot = styled(SwitchBase, {
|
37 | shouldForwardProp: prop => rootShouldForwardProp(prop) || prop === 'classes',
|
38 | name: 'MuiRadio',
|
39 | slot: 'Root',
|
40 | overridesResolver: (props, styles) => {
|
41 | const {
|
42 | ownerState
|
43 | } = props;
|
44 | return [styles.root, ownerState.size !== 'medium' && styles[`size${capitalize(ownerState.size)}`], styles[`color${capitalize(ownerState.color)}`]];
|
45 | }
|
46 | })(memoTheme(({
|
47 | theme
|
48 | }) => ({
|
49 | color: (theme.vars || theme).palette.text.secondary,
|
50 | [`&.${radioClasses.disabled}`]: {
|
51 | color: (theme.vars || theme).palette.action.disabled
|
52 | },
|
53 | variants: [{
|
54 | props: {
|
55 | color: 'default',
|
56 | disabled: false,
|
57 | disableRipple: false
|
58 | },
|
59 | style: {
|
60 | '&:hover': {
|
61 | backgroundColor: theme.vars ? `rgba(${theme.vars.palette.action.activeChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha(theme.palette.action.active, theme.palette.action.hoverOpacity)
|
62 | }
|
63 | }
|
64 | }, ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter()).map(([color]) => ({
|
65 | props: {
|
66 | color,
|
67 | disabled: false,
|
68 | disableRipple: false
|
69 | },
|
70 | style: {
|
71 | '&:hover': {
|
72 | backgroundColor: theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha(theme.palette[color].main, theme.palette.action.hoverOpacity)
|
73 | }
|
74 | }
|
75 | })), ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter()).map(([color]) => ({
|
76 | props: {
|
77 | color,
|
78 | disabled: false
|
79 | },
|
80 | style: {
|
81 | [`&.${radioClasses.checked}`]: {
|
82 | color: (theme.vars || theme).palette[color].main
|
83 | }
|
84 | }
|
85 | })), {
|
86 |
|
87 | props: {
|
88 | disableRipple: false
|
89 | },
|
90 | style: {
|
91 |
|
92 | '&:hover': {
|
93 | '@media (hover: none)': {
|
94 | backgroundColor: 'transparent'
|
95 | }
|
96 | }
|
97 | }
|
98 | }]
|
99 | })));
|
100 | function areEqualValues(a, b) {
|
101 | if (typeof b === 'object' && b !== null) {
|
102 | return a === b;
|
103 | }
|
104 |
|
105 |
|
106 | return String(a) === String(b);
|
107 | }
|
108 | const defaultCheckedIcon = _jsx(RadioButtonIcon, {
|
109 | checked: true
|
110 | });
|
111 | const defaultIcon = _jsx(RadioButtonIcon, {});
|
112 | const Radio = React.forwardRef(function Radio(inProps, ref) {
|
113 | const props = useDefaultProps({
|
114 | props: inProps,
|
115 | name: 'MuiRadio'
|
116 | });
|
117 | const {
|
118 | checked: checkedProp,
|
119 | checkedIcon = defaultCheckedIcon,
|
120 | color = 'primary',
|
121 | icon = defaultIcon,
|
122 | name: nameProp,
|
123 | onChange: onChangeProp,
|
124 | size = 'medium',
|
125 | className,
|
126 | disabled: disabledProp,
|
127 | disableRipple = false,
|
128 | ...other
|
129 | } = props;
|
130 | const muiFormControl = useFormControl();
|
131 | let disabled = disabledProp;
|
132 | if (muiFormControl) {
|
133 | if (typeof disabled === 'undefined') {
|
134 | disabled = muiFormControl.disabled;
|
135 | }
|
136 | }
|
137 | disabled ??= false;
|
138 | const ownerState = {
|
139 | ...props,
|
140 | disabled,
|
141 | disableRipple,
|
142 | color,
|
143 | size
|
144 | };
|
145 | const classes = useUtilityClasses(ownerState);
|
146 | const radioGroup = useRadioGroup();
|
147 | let checked = checkedProp;
|
148 | const onChange = createChainedFunction(onChangeProp, radioGroup && radioGroup.onChange);
|
149 | let name = nameProp;
|
150 | if (radioGroup) {
|
151 | if (typeof checked === 'undefined') {
|
152 | checked = areEqualValues(radioGroup.value, props.value);
|
153 | }
|
154 | if (typeof name === 'undefined') {
|
155 | name = radioGroup.name;
|
156 | }
|
157 | }
|
158 | return _jsx(RadioRoot, {
|
159 | type: "radio",
|
160 | icon: React.cloneElement(icon, {
|
161 | fontSize: defaultIcon.props.fontSize ?? size
|
162 | }),
|
163 | checkedIcon: React.cloneElement(checkedIcon, {
|
164 | fontSize: defaultCheckedIcon.props.fontSize ?? size
|
165 | }),
|
166 | disabled: disabled,
|
167 | ownerState: ownerState,
|
168 | classes: classes,
|
169 | name: name,
|
170 | checked: checked,
|
171 | onChange: onChange,
|
172 | ref: ref,
|
173 | className: clsx(classes.root, className),
|
174 | ...other
|
175 | });
|
176 | });
|
177 | process.env.NODE_ENV !== "production" ? Radio.propTypes = {
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | |
183 |
|
184 |
|
185 | checked: PropTypes.bool,
|
186 | |
187 |
|
188 |
|
189 |
|
190 | checkedIcon: PropTypes.node,
|
191 | |
192 |
|
193 |
|
194 | classes: PropTypes.object,
|
195 | |
196 |
|
197 |
|
198 | className: PropTypes.string,
|
199 | |
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | color: PropTypes .oneOfType([PropTypes.oneOf(['default', 'primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
|
206 | |
207 |
|
208 |
|
209 | disabled: PropTypes.bool,
|
210 | |
211 |
|
212 |
|
213 |
|
214 | disableRipple: PropTypes.bool,
|
215 | |
216 |
|
217 |
|
218 |
|
219 | icon: PropTypes.node,
|
220 | |
221 |
|
222 |
|
223 | id: PropTypes.string,
|
224 | |
225 |
|
226 |
|
227 | inputProps: PropTypes.object,
|
228 | |
229 |
|
230 |
|
231 | inputRef: refType,
|
232 | |
233 |
|
234 |
|
235 | name: PropTypes.string,
|
236 | |
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | onChange: PropTypes.func,
|
244 | |
245 |
|
246 |
|
247 |
|
248 | required: PropTypes.bool,
|
249 | |
250 |
|
251 |
|
252 |
|
253 |
|
254 | size: PropTypes .oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.string]),
|
255 | |
256 |
|
257 |
|
258 | sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
|
259 | |
260 |
|
261 |
|
262 | value: PropTypes.any
|
263 | } : void 0;
|
264 | export default Radio; |
\ | No newline at end of file |