UNPKG

8.3 kBJavaScriptView Raw
1import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3import _extends from "@babel/runtime/helpers/esm/extends";
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import clsx from 'clsx';
7import { refType } from '@mui/utils';
8import { unstable_composeClasses as composeClasses } from '@mui/base';
9import capitalize from '../utils/capitalize';
10import styled from '../styles/styled';
11import useControlled from '../utils/useControlled';
12import useFormControl from '../FormControl/useFormControl';
13import ButtonBase from '../ButtonBase';
14import { getSwitchBaseUtilityClass } from './switchBaseClasses';
15import { jsx as _jsx } from "react/jsx-runtime";
16import { jsxs as _jsxs } from "react/jsx-runtime";
17
18var 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
30var 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});
41var 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 * @ignore - internal component.
55 */
56
57var SwitchBase = /*#__PURE__*/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 // Workaround for https://github.com/facebook/react/issues/9023
117 if (event.nativeEvent.defaultPrevented) {
118 return;
119 }
120
121 var newChecked = event.target.checked;
122 setCheckedState(newChecked);
123
124 if (onChange) {
125 // TODO v6: remove the second argument.
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 /*#__PURE__*/_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: [/*#__PURE__*/_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}); // NB: If changed, please update Checkbox, Switch and Radio
181// so that the API documentation is updated.
182
183process.env.NODE_ENV !== "production" ? SwitchBase.propTypes = {
184 /**
185 * If `true`, the `input` element is focused during the first mount.
186 */
187 autoFocus: PropTypes.bool,
188
189 /**
190 * If `true`, the component is checked.
191 */
192 checked: PropTypes.bool,
193
194 /**
195 * The icon to display when the component is checked.
196 */
197 checkedIcon: PropTypes.node.isRequired,
198
199 /**
200 * Override or extend the styles applied to the component.
201 * See [CSS API](#css) below for more details.
202 */
203 classes: PropTypes.object,
204
205 /**
206 * @ignore
207 */
208 className: PropTypes.string,
209
210 /**
211 * @ignore
212 */
213 defaultChecked: PropTypes.bool,
214
215 /**
216 * If `true`, the component is disabled.
217 */
218 disabled: PropTypes.bool,
219
220 /**
221 * If `true`, the keyboard focus ripple is disabled.
222 * @default false
223 */
224 disableFocusRipple: PropTypes.bool,
225
226 /**
227 * If given, uses a negative margin to counteract the padding on one
228 * side (this is often helpful for aligning the left or right
229 * side of the icon with content above or below, without ruining the border
230 * size and shape).
231 * @default false
232 */
233 edge: PropTypes.oneOf(['end', 'start', false]),
234
235 /**
236 * The icon to display when the component is unchecked.
237 */
238 icon: PropTypes.node.isRequired,
239
240 /**
241 * The id of the `input` element.
242 */
243 id: PropTypes.string,
244
245 /**
246 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
247 */
248 inputProps: PropTypes.object,
249
250 /**
251 * Pass a ref to the `input` element.
252 */
253 inputRef: refType,
254
255 /*
256 * @ignore
257 */
258 name: PropTypes.string,
259
260 /**
261 * @ignore
262 */
263 onBlur: PropTypes.func,
264
265 /**
266 * Callback fired when the state is changed.
267 *
268 * @param {object} event The event source of the callback.
269 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
270 */
271 onChange: PropTypes.func,
272
273 /**
274 * @ignore
275 */
276 onFocus: PropTypes.func,
277
278 /**
279 * It prevents the user from changing the value of the field
280 * (not from interacting with the field).
281 */
282 readOnly: PropTypes.bool,
283
284 /**
285 * If `true`, the `input` element is required.
286 */
287 required: PropTypes.bool,
288
289 /**
290 * The system prop that allows defining system overrides as well as additional CSS styles.
291 */
292 sx: PropTypes.object,
293
294 /**
295 * @ignore
296 */
297 tabIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
298
299 /**
300 * The input component prop `type`.
301 */
302 type: PropTypes.string.isRequired,
303
304 /**
305 * The value of the component.
306 */
307 value: PropTypes.any
308} : void 0;
309export default SwitchBase;
\No newline at end of file