1 | "use strict";
|
2 | 'use client';
|
3 |
|
4 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
5 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
6 | Object.defineProperty(exports, "__esModule", {
|
7 | value: true
|
8 | });
|
9 | exports.default = void 0;
|
10 | var React = _interopRequireWildcard(require("react"));
|
11 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
12 | var _clsx = _interopRequireDefault(require("clsx"));
|
13 | var _refType = _interopRequireDefault(require("@mui/utils/refType"));
|
14 | var _composeClasses = _interopRequireDefault(require("@mui/utils/composeClasses"));
|
15 | var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
|
16 | var _rootShouldForwardProp = _interopRequireDefault(require("../styles/rootShouldForwardProp"));
|
17 | var _zeroStyled = require("../zero-styled");
|
18 | var _useControlled = _interopRequireDefault(require("../utils/useControlled"));
|
19 | var _useFormControl = _interopRequireDefault(require("../FormControl/useFormControl"));
|
20 | var _ButtonBase = _interopRequireDefault(require("../ButtonBase"));
|
21 | var _switchBaseClasses = require("./switchBaseClasses");
|
22 | var _jsxRuntime = require("react/jsx-runtime");
|
23 | const useUtilityClasses = ownerState => {
|
24 | const {
|
25 | classes,
|
26 | checked,
|
27 | disabled,
|
28 | edge
|
29 | } = ownerState;
|
30 | const slots = {
|
31 | root: ['root', checked && 'checked', disabled && 'disabled', edge && `edge${(0, _capitalize.default)(edge)}`],
|
32 | input: ['input']
|
33 | };
|
34 | return (0, _composeClasses.default)(slots, _switchBaseClasses.getSwitchBaseUtilityClass, classes);
|
35 | };
|
36 | const SwitchBaseRoot = (0, _zeroStyled.styled)(_ButtonBase.default)({
|
37 | padding: 9,
|
38 | borderRadius: '50%',
|
39 | variants: [{
|
40 | props: {
|
41 | edge: 'start',
|
42 | size: 'small'
|
43 | },
|
44 | style: {
|
45 | marginLeft: -3
|
46 | }
|
47 | }, {
|
48 | props: ({
|
49 | edge,
|
50 | ownerState
|
51 | }) => edge === 'start' && ownerState.size !== 'small',
|
52 | style: {
|
53 | marginLeft: -12
|
54 | }
|
55 | }, {
|
56 | props: {
|
57 | edge: 'end',
|
58 | size: 'small'
|
59 | },
|
60 | style: {
|
61 | marginRight: -3
|
62 | }
|
63 | }, {
|
64 | props: ({
|
65 | edge,
|
66 | ownerState
|
67 | }) => edge === 'end' && ownerState.size !== 'small',
|
68 | style: {
|
69 | marginRight: -12
|
70 | }
|
71 | }]
|
72 | });
|
73 | const SwitchBaseInput = (0, _zeroStyled.styled)('input', {
|
74 | shouldForwardProp: _rootShouldForwardProp.default
|
75 | })({
|
76 | cursor: 'inherit',
|
77 | position: 'absolute',
|
78 | opacity: 0,
|
79 | width: '100%',
|
80 | height: '100%',
|
81 | top: 0,
|
82 | left: 0,
|
83 | margin: 0,
|
84 | padding: 0,
|
85 | zIndex: 1
|
86 | });
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | const SwitchBase = React.forwardRef(function SwitchBase(props, ref) {
|
92 | const {
|
93 | autoFocus,
|
94 | checked: checkedProp,
|
95 | checkedIcon,
|
96 | className,
|
97 | defaultChecked,
|
98 | disabled: disabledProp,
|
99 | disableFocusRipple = false,
|
100 | edge = false,
|
101 | icon,
|
102 | id,
|
103 | inputProps,
|
104 | inputRef,
|
105 | name,
|
106 | onBlur,
|
107 | onChange,
|
108 | onFocus,
|
109 | readOnly,
|
110 | required = false,
|
111 | tabIndex,
|
112 | type,
|
113 | value,
|
114 | ...other
|
115 | } = props;
|
116 | const [checked, setCheckedState] = (0, _useControlled.default)({
|
117 | controlled: checkedProp,
|
118 | default: Boolean(defaultChecked),
|
119 | name: 'SwitchBase',
|
120 | state: 'checked'
|
121 | });
|
122 | const muiFormControl = (0, _useFormControl.default)();
|
123 | const handleFocus = event => {
|
124 | if (onFocus) {
|
125 | onFocus(event);
|
126 | }
|
127 | if (muiFormControl && muiFormControl.onFocus) {
|
128 | muiFormControl.onFocus(event);
|
129 | }
|
130 | };
|
131 | const handleBlur = event => {
|
132 | if (onBlur) {
|
133 | onBlur(event);
|
134 | }
|
135 | if (muiFormControl && muiFormControl.onBlur) {
|
136 | muiFormControl.onBlur(event);
|
137 | }
|
138 | };
|
139 | const handleInputChange = event => {
|
140 |
|
141 | if (event.nativeEvent.defaultPrevented) {
|
142 | return;
|
143 | }
|
144 | const newChecked = event.target.checked;
|
145 | setCheckedState(newChecked);
|
146 | if (onChange) {
|
147 |
|
148 | onChange(event, newChecked);
|
149 | }
|
150 | };
|
151 | let disabled = disabledProp;
|
152 | if (muiFormControl) {
|
153 | if (typeof disabled === 'undefined') {
|
154 | disabled = muiFormControl.disabled;
|
155 | }
|
156 | }
|
157 | const hasLabelFor = type === 'checkbox' || type === 'radio';
|
158 | const ownerState = {
|
159 | ...props,
|
160 | checked,
|
161 | disabled,
|
162 | disableFocusRipple,
|
163 | edge
|
164 | };
|
165 | const classes = useUtilityClasses(ownerState);
|
166 | return (0, _jsxRuntime.jsxs)(SwitchBaseRoot, {
|
167 | component: "span",
|
168 | className: (0, _clsx.default)(classes.root, className),
|
169 | centerRipple: true,
|
170 | focusRipple: !disableFocusRipple,
|
171 | disabled: disabled,
|
172 | tabIndex: null,
|
173 | role: undefined,
|
174 | onFocus: handleFocus,
|
175 | onBlur: handleBlur,
|
176 | ownerState: ownerState,
|
177 | ref: ref,
|
178 | ...other,
|
179 | children: [(0, _jsxRuntime.jsx)(SwitchBaseInput, {
|
180 | autoFocus: autoFocus,
|
181 | checked: checkedProp,
|
182 | defaultChecked: defaultChecked,
|
183 | className: classes.input,
|
184 | disabled: disabled,
|
185 | id: hasLabelFor ? id : undefined,
|
186 | name: name,
|
187 | onChange: handleInputChange,
|
188 | readOnly: readOnly,
|
189 | ref: inputRef,
|
190 | required: required,
|
191 | ownerState: ownerState,
|
192 | tabIndex: tabIndex,
|
193 | type: type,
|
194 | ...(type === 'checkbox' && value === undefined ? {} : {
|
195 | value
|
196 | }),
|
197 | ...inputProps
|
198 | }), checked ? checkedIcon : icon]
|
199 | });
|
200 | });
|
201 |
|
202 |
|
203 |
|
204 | process.env.NODE_ENV !== "production" ? SwitchBase.propTypes = {
|
205 | |
206 |
|
207 |
|
208 | autoFocus: _propTypes.default.bool,
|
209 | |
210 |
|
211 |
|
212 | checked: _propTypes.default.bool,
|
213 | |
214 |
|
215 |
|
216 | checkedIcon: _propTypes.default.node.isRequired,
|
217 | |
218 |
|
219 |
|
220 | classes: _propTypes.default.object,
|
221 | |
222 |
|
223 |
|
224 | className: _propTypes.default.string,
|
225 | |
226 |
|
227 |
|
228 | defaultChecked: _propTypes.default.bool,
|
229 | |
230 |
|
231 |
|
232 | disabled: _propTypes.default.bool,
|
233 | |
234 |
|
235 |
|
236 |
|
237 | disableFocusRipple: _propTypes.default.bool,
|
238 | |
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | edge: _propTypes.default.oneOf(['end', 'start', false]),
|
246 | |
247 |
|
248 |
|
249 | icon: _propTypes.default.node.isRequired,
|
250 | |
251 |
|
252 |
|
253 | id: _propTypes.default.string,
|
254 | |
255 |
|
256 |
|
257 | inputProps: _propTypes.default.object,
|
258 | |
259 |
|
260 |
|
261 | inputRef: _refType.default,
|
262 | |
263 |
|
264 |
|
265 | name: _propTypes.default.string,
|
266 | |
267 |
|
268 |
|
269 | onBlur: _propTypes.default.func,
|
270 | |
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | onChange: _propTypes.default.func,
|
277 | |
278 |
|
279 |
|
280 | onFocus: _propTypes.default.func,
|
281 | |
282 |
|
283 |
|
284 |
|
285 | readOnly: _propTypes.default.bool,
|
286 | |
287 |
|
288 |
|
289 | required: _propTypes.default.bool,
|
290 | |
291 |
|
292 |
|
293 | sx: _propTypes.default.object,
|
294 | |
295 |
|
296 |
|
297 | tabIndex: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
|
298 | |
299 |
|
300 |
|
301 | type: _propTypes.default.string.isRequired,
|
302 | |
303 |
|
304 |
|
305 | value: _propTypes.default.any
|
306 | } : void 0;
|
307 | var _default = exports.default = SwitchBase; |
\ | No newline at end of file |