1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.default = exports.styles = void 0;
|
9 |
|
10 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
11 |
|
12 | var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
13 |
|
14 | var _react = _interopRequireDefault(require("react"));
|
15 |
|
16 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
17 |
|
18 | var _clsx = _interopRequireDefault(require("clsx"));
|
19 |
|
20 | var _utils = require("@material-ui/utils");
|
21 |
|
22 | var _useFormControl = _interopRequireDefault(require("../FormControl/useFormControl"));
|
23 |
|
24 | var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
|
25 |
|
26 | var _IconButton = _interopRequireDefault(require("../IconButton"));
|
27 |
|
28 | var styles = {
|
29 | root: {
|
30 | padding: 9
|
31 | },
|
32 | checked: {},
|
33 | disabled: {},
|
34 | input: {
|
35 | cursor: 'inherit',
|
36 | position: 'absolute',
|
37 | opacity: 0,
|
38 | width: '100%',
|
39 | height: '100%',
|
40 | top: 0,
|
41 | left: 0,
|
42 | margin: 0,
|
43 | padding: 0,
|
44 | zIndex: 1
|
45 | }
|
46 | };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | exports.styles = styles;
|
52 |
|
53 | var SwitchBase = _react.default.forwardRef(function SwitchBase(props, ref) {
|
54 | var autoFocus = props.autoFocus,
|
55 | checkedProp = props.checked,
|
56 | checkedIcon = props.checkedIcon,
|
57 | classes = props.classes,
|
58 | className = props.className,
|
59 | defaultChecked = props.defaultChecked,
|
60 | disabledProp = props.disabled,
|
61 | icon = props.icon,
|
62 | id = props.id,
|
63 | inputProps = props.inputProps,
|
64 | inputRef = props.inputRef,
|
65 | name = props.name,
|
66 | onBlur = props.onBlur,
|
67 | onChange = props.onChange,
|
68 | onFocus = props.onFocus,
|
69 | readOnly = props.readOnly,
|
70 | required = props.required,
|
71 | tabIndex = props.tabIndex,
|
72 | type = props.type,
|
73 | value = props.value,
|
74 | other = (0, _objectWithoutProperties2.default)(props, ["autoFocus", "checked", "checkedIcon", "classes", "className", "defaultChecked", "disabled", "icon", "id", "inputProps", "inputRef", "name", "onBlur", "onChange", "onFocus", "readOnly", "required", "tabIndex", "type", "value"]);
|
75 |
|
76 | var _React$useRef = _react.default.useRef(checkedProp != null),
|
77 | isControlled = _React$useRef.current;
|
78 |
|
79 | var _React$useState = _react.default.useState(Boolean(defaultChecked)),
|
80 | checkedState = _React$useState[0],
|
81 | setCheckedState = _React$useState[1];
|
82 |
|
83 | var checked = isControlled ? checkedProp : checkedState;
|
84 | var muiFormControl = (0, _useFormControl.default)();
|
85 |
|
86 | var handleFocus = function handleFocus(event) {
|
87 | if (onFocus) {
|
88 | onFocus(event);
|
89 | }
|
90 |
|
91 | if (muiFormControl && muiFormControl.onFocus) {
|
92 | muiFormControl.onFocus(event);
|
93 | }
|
94 | };
|
95 |
|
96 | var handleBlur = function handleBlur(event) {
|
97 | if (onBlur) {
|
98 | onBlur(event);
|
99 | }
|
100 |
|
101 | if (muiFormControl && muiFormControl.onBlur) {
|
102 | muiFormControl.onBlur(event);
|
103 | }
|
104 | };
|
105 |
|
106 | var handleInputChange = function handleInputChange(event) {
|
107 | var newChecked = event.target.checked;
|
108 |
|
109 | if (!isControlled) {
|
110 | setCheckedState(newChecked);
|
111 | }
|
112 |
|
113 | if (onChange) {
|
114 | onChange(event, newChecked);
|
115 | }
|
116 | };
|
117 |
|
118 | var disabled = disabledProp;
|
119 |
|
120 | if (muiFormControl) {
|
121 | if (typeof disabled === 'undefined') {
|
122 | disabled = muiFormControl.disabled;
|
123 | }
|
124 | }
|
125 |
|
126 | var hasLabelFor = type === 'checkbox' || type === 'radio';
|
127 | return _react.default.createElement(_IconButton.default, (0, _extends2.default)({
|
128 | component: "span",
|
129 | className: (0, _clsx.default)(classes.root, className, checked && classes.checked, disabled && classes.disabled),
|
130 | disabled: disabled,
|
131 | tabIndex: null,
|
132 | role: undefined,
|
133 | onFocus: handleFocus,
|
134 | onBlur: handleBlur,
|
135 | ref: ref
|
136 | }, other), _react.default.createElement("input", (0, _extends2.default)({
|
137 | autoFocus: autoFocus,
|
138 | checked: checkedProp,
|
139 | defaultChecked: defaultChecked,
|
140 | className: classes.input,
|
141 | disabled: disabled,
|
142 | id: hasLabelFor && id,
|
143 | name: name,
|
144 | onChange: handleInputChange,
|
145 | readOnly: readOnly,
|
146 | ref: inputRef,
|
147 | required: required,
|
148 | tabIndex: tabIndex,
|
149 | type: type,
|
150 | value: value
|
151 | }, inputProps)), checked ? checkedIcon : icon);
|
152 | });
|
153 |
|
154 |
|
155 |
|
156 | process.env.NODE_ENV !== "production" ? SwitchBase.propTypes = {
|
157 | |
158 |
|
159 |
|
160 | autoFocus: _propTypes.default.bool,
|
161 |
|
162 | |
163 |
|
164 |
|
165 | checked: _propTypes.default.bool,
|
166 |
|
167 | |
168 |
|
169 |
|
170 | checkedIcon: _propTypes.default.node.isRequired,
|
171 |
|
172 | |
173 |
|
174 |
|
175 |
|
176 | classes: _propTypes.default.object.isRequired,
|
177 |
|
178 | |
179 |
|
180 |
|
181 | className: _propTypes.default.string,
|
182 |
|
183 | |
184 |
|
185 |
|
186 | defaultChecked: _propTypes.default.bool,
|
187 |
|
188 | |
189 |
|
190 |
|
191 | disabled: _propTypes.default.bool,
|
192 |
|
193 | |
194 |
|
195 |
|
196 | icon: _propTypes.default.node.isRequired,
|
197 |
|
198 | |
199 |
|
200 |
|
201 | id: _propTypes.default.string,
|
202 |
|
203 | |
204 |
|
205 |
|
206 | inputProps: _propTypes.default.object,
|
207 |
|
208 | |
209 |
|
210 |
|
211 | inputRef: _utils.refType,
|
212 |
|
213 | |
214 |
|
215 |
|
216 | name: _propTypes.default.string,
|
217 |
|
218 | |
219 |
|
220 |
|
221 | onBlur: _propTypes.default.func,
|
222 |
|
223 | |
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | onChange: _propTypes.default.func,
|
230 |
|
231 | |
232 |
|
233 |
|
234 | onFocus: _propTypes.default.func,
|
235 |
|
236 | |
237 |
|
238 |
|
239 |
|
240 | readOnly: _propTypes.default.bool,
|
241 |
|
242 | |
243 |
|
244 |
|
245 | required: _propTypes.default.bool,
|
246 |
|
247 | |
248 |
|
249 |
|
250 | tabIndex: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
|
251 |
|
252 | |
253 |
|
254 |
|
255 | type: _propTypes.default.string.isRequired,
|
256 |
|
257 | |
258 |
|
259 |
|
260 | value: _propTypes.default.any
|
261 | } : void 0;
|
262 |
|
263 | var _default = (0, _withStyles.default)(styles, {
|
264 | name: 'PrivateSwitchBase'
|
265 | })(SwitchBase);
|
266 |
|
267 | exports.default = _default; |
\ | No newline at end of file |