UNPKG

5.79 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3const _excluded = ["checked", "defaultChecked", "disabled", "onBlur", "onChange", "onFocus", "onFocusVisible", "readOnly", "required", "slotProps", "slots"];
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import composeClasses from '../composeClasses';
7import useSwitch from '../useSwitch';
8import { useSlotProps } from '../utils';
9import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
10import { getSwitchUtilityClass } from './switchClasses';
11import { jsx as _jsx } from "react/jsx-runtime";
12import { jsxs as _jsxs } from "react/jsx-runtime";
13const useUtilityClasses = ownerState => {
14 const {
15 checked,
16 disabled,
17 focusVisible,
18 readOnly
19 } = ownerState;
20 const slots = {
21 root: ['root', checked && 'checked', disabled && 'disabled', focusVisible && 'focusVisible', readOnly && 'readOnly'],
22 thumb: ['thumb'],
23 input: ['input'],
24 track: ['track']
25 };
26 return composeClasses(slots, useClassNamesOverride(getSwitchUtilityClass));
27};
28
29/**
30 * The foundation for building custom-styled switches.
31 *
32 * Demos:
33 *
34 * - [Switch](https://mui.com/base/react-switch/)
35 *
36 * API:
37 *
38 * - [Switch API](https://mui.com/base/react-switch/components-api/#switch)
39 */
40const Switch = /*#__PURE__*/React.forwardRef(function Switch(props, forwardedRef) {
41 var _slots$root, _slots$thumb, _slots$input, _slots$track;
42 const {
43 checked: checkedProp,
44 defaultChecked,
45 disabled: disabledProp,
46 onBlur,
47 onChange,
48 onFocus,
49 onFocusVisible,
50 readOnly: readOnlyProp,
51 slotProps = {},
52 slots = {}
53 } = props,
54 other = _objectWithoutPropertiesLoose(props, _excluded);
55 const useSwitchProps = {
56 checked: checkedProp,
57 defaultChecked,
58 disabled: disabledProp,
59 onBlur,
60 onChange,
61 onFocus,
62 onFocusVisible,
63 readOnly: readOnlyProp
64 };
65 const {
66 getInputProps,
67 checked,
68 disabled,
69 focusVisible,
70 readOnly
71 } = useSwitch(useSwitchProps);
72 const ownerState = _extends({}, props, {
73 checked,
74 disabled,
75 focusVisible,
76 readOnly
77 });
78 const classes = useUtilityClasses(ownerState);
79 const Root = (_slots$root = slots.root) != null ? _slots$root : 'span';
80 const rootProps = useSlotProps({
81 elementType: Root,
82 externalSlotProps: slotProps.root,
83 externalForwardedProps: other,
84 additionalProps: {
85 ref: forwardedRef
86 },
87 ownerState,
88 className: classes.root
89 });
90 const Thumb = (_slots$thumb = slots.thumb) != null ? _slots$thumb : 'span';
91 const thumbProps = useSlotProps({
92 elementType: Thumb,
93 externalSlotProps: slotProps.thumb,
94 ownerState,
95 className: classes.thumb
96 });
97 const Input = (_slots$input = slots.input) != null ? _slots$input : 'input';
98 const inputProps = useSlotProps({
99 elementType: Input,
100 getSlotProps: getInputProps,
101 externalSlotProps: slotProps.input,
102 ownerState,
103 className: classes.input
104 });
105 const Track = slots.track === null ? () => null : (_slots$track = slots.track) != null ? _slots$track : 'span';
106 const trackProps = useSlotProps({
107 elementType: Track,
108 externalSlotProps: slotProps.track,
109 ownerState,
110 className: classes.track
111 });
112 return /*#__PURE__*/_jsxs(Root, _extends({}, rootProps, {
113 children: [/*#__PURE__*/_jsx(Track, _extends({}, trackProps)), /*#__PURE__*/_jsx(Thumb, _extends({}, thumbProps)), /*#__PURE__*/_jsx(Input, _extends({}, inputProps))]
114 }));
115});
116process.env.NODE_ENV !== "production" ? Switch.propTypes /* remove-proptypes */ = {
117 // ----------------------------- Warning --------------------------------
118 // | These PropTypes are generated from the TypeScript type definitions |
119 // | To update them edit TypeScript types and run "yarn proptypes" |
120 // ----------------------------------------------------------------------
121 /**
122 * If `true`, the component is checked.
123 */
124 checked: PropTypes.bool,
125 /**
126 * The default checked state. Use when the component is not controlled.
127 */
128 defaultChecked: PropTypes.bool,
129 /**
130 * If `true`, the component is disabled.
131 */
132 disabled: PropTypes.bool,
133 /**
134 * @ignore
135 */
136 onBlur: PropTypes.func,
137 /**
138 * Callback fired when the state is changed.
139 *
140 * @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
141 * You can pull out the new value by accessing `event.target.value` (string).
142 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
143 */
144 onChange: PropTypes.func,
145 /**
146 * @ignore
147 */
148 onFocus: PropTypes.func,
149 /**
150 * @ignore
151 */
152 onFocusVisible: PropTypes.func,
153 /**
154 * If `true`, the component is read only.
155 */
156 readOnly: PropTypes.bool,
157 /**
158 * If `true`, the `input` element is required.
159 */
160 required: PropTypes.bool,
161 /**
162 * The props used for each slot inside the Switch.
163 * @default {}
164 */
165 slotProps: PropTypes.shape({
166 input: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
167 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
168 thumb: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
169 track: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
170 }),
171 /**
172 * The components used for each slot inside the Switch.
173 * Either a string to use a HTML element or a component.
174 * @default {}
175 */
176 slots: PropTypes /* @typescript-to-proptypes-ignore */.shape({
177 input: PropTypes.elementType,
178 root: PropTypes.elementType,
179 thumb: PropTypes.elementType,
180 track: PropTypes.oneOfType([PropTypes.elementType, PropTypes.oneOf([null])])
181 })
182} : void 0;
183export default Switch;
\No newline at end of file