UNPKG

7.93 kBJavaScriptView Raw
1import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
2import _typeof from "@babel/runtime/helpers/esm/typeof";
3import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4import _extends from "@babel/runtime/helpers/esm/extends";
5import * as React from 'react';
6import PropTypes from 'prop-types';
7import { refType } from '@mui/utils';
8import { unstable_composeClasses as composeClasses } from '@mui/base';
9import { alpha } from '@mui/system';
10import SwitchBase from '../internal/SwitchBase';
11import useThemeProps from '../styles/useThemeProps';
12import RadioButtonIcon from './RadioButtonIcon';
13import capitalize from '../utils/capitalize';
14import createChainedFunction from '../utils/createChainedFunction';
15import useRadioGroup from '../RadioGroup/useRadioGroup';
16import radioClasses, { getRadioUtilityClass } from './radioClasses';
17import styled, { rootShouldForwardProp } from '../styles/styled';
18import { jsx as _jsx } from "react/jsx-runtime";
19
20var useUtilityClasses = function useUtilityClasses(ownerState) {
21 var classes = ownerState.classes,
22 color = ownerState.color;
23 var slots = {
24 root: ['root', "color".concat(capitalize(color))]
25 };
26 return _extends({}, classes, composeClasses(slots, getRadioUtilityClass, classes));
27};
28
29var RadioRoot = styled(SwitchBase, {
30 shouldForwardProp: function shouldForwardProp(prop) {
31 return rootShouldForwardProp(prop) || prop === 'classes';
32 },
33 name: 'MuiRadio',
34 slot: 'Root',
35 overridesResolver: function overridesResolver(props, styles) {
36 var ownerState = props.ownerState;
37 return [styles.root, styles["color".concat(capitalize(ownerState.color))]];
38 }
39})(function (_ref) {
40 var theme = _ref.theme,
41 ownerState = _ref.ownerState;
42 return _extends({
43 color: (theme.vars || theme).palette.text.secondary,
44 '&:hover': {
45 backgroundColor: theme.vars ? "rgba(".concat(ownerState.color === 'default' ? theme.vars.palette.action.activeChannel : theme.vars.palette[ownerState.color].mainChannel, " / ").concat(theme.vars.palette.action.hoverOpacity, ")") : alpha(ownerState.color === 'default' ? theme.palette.action.active : theme.palette[ownerState.color].main, theme.palette.action.hoverOpacity),
46 // Reset on touch devices, it doesn't add specificity
47 '@media (hover: none)': {
48 backgroundColor: 'transparent'
49 }
50 }
51 }, ownerState.color !== 'default' && _defineProperty({}, "&.".concat(radioClasses.checked), {
52 color: (theme.vars || theme).palette[ownerState.color].main
53 }), _defineProperty({}, "&.".concat(radioClasses.disabled), {
54 color: (theme.vars || theme).palette.action.disabled
55 }));
56});
57
58function areEqualValues(a, b) {
59 if (_typeof(b) === 'object' && b !== null) {
60 return a === b;
61 } // The value could be a number, the DOM will stringify it anyway.
62
63
64 return String(a) === String(b);
65}
66
67var defaultCheckedIcon = /*#__PURE__*/_jsx(RadioButtonIcon, {
68 checked: true
69});
70
71var defaultIcon = /*#__PURE__*/_jsx(RadioButtonIcon, {});
72
73var Radio = /*#__PURE__*/React.forwardRef(function Radio(inProps, ref) {
74 var _defaultIcon$props$fo, _defaultCheckedIcon$p;
75
76 var props = useThemeProps({
77 props: inProps,
78 name: 'MuiRadio'
79 });
80
81 var checkedProp = props.checked,
82 _props$checkedIcon = props.checkedIcon,
83 checkedIcon = _props$checkedIcon === void 0 ? defaultCheckedIcon : _props$checkedIcon,
84 _props$color = props.color,
85 color = _props$color === void 0 ? 'primary' : _props$color,
86 _props$icon = props.icon,
87 icon = _props$icon === void 0 ? defaultIcon : _props$icon,
88 nameProp = props.name,
89 onChangeProp = props.onChange,
90 _props$size = props.size,
91 size = _props$size === void 0 ? 'medium' : _props$size,
92 other = _objectWithoutProperties(props, ["checked", "checkedIcon", "color", "icon", "name", "onChange", "size"]);
93
94 var ownerState = _extends({}, props, {
95 color: color,
96 size: size
97 });
98
99 var classes = useUtilityClasses(ownerState);
100 var radioGroup = useRadioGroup();
101 var checked = checkedProp;
102 var onChange = createChainedFunction(onChangeProp, radioGroup && radioGroup.onChange);
103 var name = nameProp;
104
105 if (radioGroup) {
106 if (typeof checked === 'undefined') {
107 checked = areEqualValues(radioGroup.value, props.value);
108 }
109
110 if (typeof name === 'undefined') {
111 name = radioGroup.name;
112 }
113 }
114
115 return /*#__PURE__*/_jsx(RadioRoot, _extends({
116 type: "radio",
117 icon: /*#__PURE__*/React.cloneElement(icon, {
118 fontSize: (_defaultIcon$props$fo = defaultIcon.props.fontSize) != null ? _defaultIcon$props$fo : size
119 }),
120 checkedIcon: /*#__PURE__*/React.cloneElement(checkedIcon, {
121 fontSize: (_defaultCheckedIcon$p = defaultCheckedIcon.props.fontSize) != null ? _defaultCheckedIcon$p : size
122 }),
123 ownerState: ownerState,
124 classes: classes,
125 name: name,
126 checked: checked,
127 onChange: onChange,
128 ref: ref
129 }, other));
130});
131process.env.NODE_ENV !== "production" ? Radio.propTypes
132/* remove-proptypes */
133= {
134 // ----------------------------- Warning --------------------------------
135 // | These PropTypes are generated from the TypeScript type definitions |
136 // | To update them edit the d.ts file and run "yarn proptypes" |
137 // ----------------------------------------------------------------------
138
139 /**
140 * If `true`, the component is checked.
141 */
142 checked: PropTypes.bool,
143
144 /**
145 * The icon to display when the component is checked.
146 * @default <RadioButtonIcon checked />
147 */
148 checkedIcon: PropTypes.node,
149
150 /**
151 * Override or extend the styles applied to the component.
152 */
153 classes: PropTypes.object,
154
155 /**
156 * The color of the component.
157 * It supports both default and custom theme colors, which can be added as shown in the
158 * [palette customization guide](https://mui.com/material-ui/customization/palette/#adding-new-colors).
159 * @default 'primary'
160 */
161 color: PropTypes
162 /* @typescript-to-proptypes-ignore */
163 .oneOfType([PropTypes.oneOf(['default', 'primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
164
165 /**
166 * If `true`, the component is disabled.
167 */
168 disabled: PropTypes.bool,
169
170 /**
171 * If `true`, the ripple effect is disabled.
172 */
173 disableRipple: PropTypes.bool,
174
175 /**
176 * The icon to display when the component is unchecked.
177 * @default <RadioButtonIcon />
178 */
179 icon: PropTypes.node,
180
181 /**
182 * The id of the `input` element.
183 */
184 id: PropTypes.string,
185
186 /**
187 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
188 */
189 inputProps: PropTypes.object,
190
191 /**
192 * Pass a ref to the `input` element.
193 */
194 inputRef: refType,
195
196 /**
197 * Name attribute of the `input` element.
198 */
199 name: PropTypes.string,
200
201 /**
202 * Callback fired when the state is changed.
203 *
204 * @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
205 * You can pull out the new value by accessing `event.target.value` (string).
206 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
207 */
208 onChange: PropTypes.func,
209
210 /**
211 * If `true`, the `input` element is required.
212 */
213 required: PropTypes.bool,
214
215 /**
216 * The size of the component.
217 * `small` is equivalent to the dense radio styling.
218 * @default 'medium'
219 */
220 size: PropTypes
221 /* @typescript-to-proptypes-ignore */
222 .oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.string]),
223
224 /**
225 * The system prop that allows defining system overrides as well as additional CSS styles.
226 */
227 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
228
229 /**
230 * The value of the component. The DOM API casts this to a string.
231 */
232 value: PropTypes.any
233} : void 0;
234export default Radio;
\No newline at end of file