UNPKG

8.23 kBJavaScriptView Raw
1"use strict";
2'use client';
3
4var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
6Object.defineProperty(exports, "__esModule", {
7 value: true
8});
9exports.default = void 0;
10var React = _interopRequireWildcard(require("react"));
11var _propTypes = _interopRequireDefault(require("prop-types"));
12var _clsx = _interopRequireDefault(require("clsx"));
13var _refType = _interopRequireDefault(require("@mui/utils/refType"));
14var _composeClasses = _interopRequireDefault(require("@mui/utils/composeClasses"));
15var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
16var _rootShouldForwardProp = _interopRequireDefault(require("../styles/rootShouldForwardProp"));
17var _zeroStyled = require("../zero-styled");
18var _useControlled = _interopRequireDefault(require("../utils/useControlled"));
19var _useFormControl = _interopRequireDefault(require("../FormControl/useFormControl"));
20var _ButtonBase = _interopRequireDefault(require("../ButtonBase"));
21var _switchBaseClasses = require("./switchBaseClasses");
22var _jsxRuntime = require("react/jsx-runtime");
23const 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};
36const 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});
73const 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 * @ignore - internal component.
90 */
91const SwitchBase = /*#__PURE__*/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 // Workaround for https://github.com/facebook/react/issues/9023
141 if (event.nativeEvent.defaultPrevented) {
142 return;
143 }
144 const newChecked = event.target.checked;
145 setCheckedState(newChecked);
146 if (onChange) {
147 // TODO v6: remove the second argument.
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 /*#__PURE__*/(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: [/*#__PURE__*/(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// NB: If changed, please update Checkbox, Switch and Radio
203// so that the API documentation is updated.
204process.env.NODE_ENV !== "production" ? SwitchBase.propTypes = {
205 /**
206 * If `true`, the `input` element is focused during the first mount.
207 */
208 autoFocus: _propTypes.default.bool,
209 /**
210 * If `true`, the component is checked.
211 */
212 checked: _propTypes.default.bool,
213 /**
214 * The icon to display when the component is checked.
215 */
216 checkedIcon: _propTypes.default.node.isRequired,
217 /**
218 * Override or extend the styles applied to the component.
219 */
220 classes: _propTypes.default.object,
221 /**
222 * @ignore
223 */
224 className: _propTypes.default.string,
225 /**
226 * @ignore
227 */
228 defaultChecked: _propTypes.default.bool,
229 /**
230 * If `true`, the component is disabled.
231 */
232 disabled: _propTypes.default.bool,
233 /**
234 * If `true`, the keyboard focus ripple is disabled.
235 * @default false
236 */
237 disableFocusRipple: _propTypes.default.bool,
238 /**
239 * If given, uses a negative margin to counteract the padding on one
240 * side (this is often helpful for aligning the left or right
241 * side of the icon with content above or below, without ruining the border
242 * size and shape).
243 * @default false
244 */
245 edge: _propTypes.default.oneOf(['end', 'start', false]),
246 /**
247 * The icon to display when the component is unchecked.
248 */
249 icon: _propTypes.default.node.isRequired,
250 /**
251 * The id of the `input` element.
252 */
253 id: _propTypes.default.string,
254 /**
255 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
256 */
257 inputProps: _propTypes.default.object,
258 /**
259 * Pass a ref to the `input` element.
260 */
261 inputRef: _refType.default,
262 /*
263 * @ignore
264 */
265 name: _propTypes.default.string,
266 /**
267 * @ignore
268 */
269 onBlur: _propTypes.default.func,
270 /**
271 * Callback fired when the state is changed.
272 *
273 * @param {object} event The event source of the callback.
274 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
275 */
276 onChange: _propTypes.default.func,
277 /**
278 * @ignore
279 */
280 onFocus: _propTypes.default.func,
281 /**
282 * It prevents the user from changing the value of the field
283 * (not from interacting with the field).
284 */
285 readOnly: _propTypes.default.bool,
286 /**
287 * If `true`, the `input` element is required.
288 */
289 required: _propTypes.default.bool,
290 /**
291 * The system prop that allows defining system overrides as well as additional CSS styles.
292 */
293 sx: _propTypes.default.object,
294 /**
295 * @ignore
296 */
297 tabIndex: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
298 /**
299 * The input component prop `type`.
300 */
301 type: _propTypes.default.string.isRequired,
302 /**
303 * The value of the component.
304 */
305 value: _propTypes.default.any
306} : void 0;
307var _default = exports.default = SwitchBase;
\No newline at end of file