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