UNPKG

7.1 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = exports.styles = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
15
16var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
17
18var React = _interopRequireWildcard(require("react"));
19
20var _propTypes = _interopRequireDefault(require("prop-types"));
21
22var _clsx = _interopRequireDefault(require("clsx"));
23
24var _utils = require("@material-ui/utils");
25
26var _useControlled3 = _interopRequireDefault(require("../utils/useControlled"));
27
28var _useFormControl = _interopRequireDefault(require("../FormControl/useFormControl"));
29
30var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
31
32var _IconButton = _interopRequireDefault(require("../IconButton"));
33
34var styles = {
35 root: {
36 padding: 9
37 },
38 checked: {},
39 disabled: {},
40 input: {
41 cursor: 'inherit',
42 position: 'absolute',
43 opacity: 0,
44 width: '100%',
45 height: '100%',
46 top: 0,
47 left: 0,
48 margin: 0,
49 padding: 0,
50 zIndex: 1
51 }
52};
53/**
54 * @ignore - internal component.
55 */
56
57exports.styles = styles;
58var SwitchBase = /*#__PURE__*/React.forwardRef(function SwitchBase(props, ref) {
59 var autoFocus = props.autoFocus,
60 checkedProp = props.checked,
61 checkedIcon = props.checkedIcon,
62 classes = props.classes,
63 className = props.className,
64 defaultChecked = props.defaultChecked,
65 disabledProp = props.disabled,
66 icon = props.icon,
67 id = props.id,
68 inputProps = props.inputProps,
69 inputRef = props.inputRef,
70 name = props.name,
71 onBlur = props.onBlur,
72 onChange = props.onChange,
73 onFocus = props.onFocus,
74 readOnly = props.readOnly,
75 required = props.required,
76 tabIndex = props.tabIndex,
77 type = props.type,
78 value = props.value,
79 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"]);
80
81 var _useControlled = (0, _useControlled3.default)({
82 controlled: checkedProp,
83 default: Boolean(defaultChecked),
84 name: 'SwitchBase',
85 state: 'checked'
86 }),
87 _useControlled2 = (0, _slicedToArray2.default)(_useControlled, 2),
88 checked = _useControlled2[0],
89 setCheckedState = _useControlled2[1];
90
91 var muiFormControl = (0, _useFormControl.default)();
92
93 var handleFocus = function handleFocus(event) {
94 if (onFocus) {
95 onFocus(event);
96 }
97
98 if (muiFormControl && muiFormControl.onFocus) {
99 muiFormControl.onFocus(event);
100 }
101 };
102
103 var handleBlur = function handleBlur(event) {
104 if (onBlur) {
105 onBlur(event);
106 }
107
108 if (muiFormControl && muiFormControl.onBlur) {
109 muiFormControl.onBlur(event);
110 }
111 };
112
113 var handleInputChange = function handleInputChange(event) {
114 var newChecked = event.target.checked;
115 setCheckedState(newChecked);
116
117 if (onChange) {
118 // TODO v5: remove the second argument.
119 onChange(event, newChecked);
120 }
121 };
122
123 var disabled = disabledProp;
124
125 if (muiFormControl) {
126 if (typeof disabled === 'undefined') {
127 disabled = muiFormControl.disabled;
128 }
129 }
130
131 var hasLabelFor = type === 'checkbox' || type === 'radio';
132 return /*#__PURE__*/React.createElement(_IconButton.default, (0, _extends2.default)({
133 component: "span",
134 className: (0, _clsx.default)(classes.root, className, checked && classes.checked, disabled && classes.disabled),
135 disabled: disabled,
136 tabIndex: null,
137 role: undefined,
138 onFocus: handleFocus,
139 onBlur: handleBlur,
140 ref: ref
141 }, other), /*#__PURE__*/React.createElement("input", (0, _extends2.default)({
142 autoFocus: autoFocus,
143 checked: checkedProp,
144 defaultChecked: defaultChecked,
145 className: classes.input,
146 disabled: disabled,
147 id: hasLabelFor && id,
148 name: name,
149 onChange: handleInputChange,
150 readOnly: readOnly,
151 ref: inputRef,
152 required: required,
153 tabIndex: tabIndex,
154 type: type,
155 value: value
156 }, inputProps)), checked ? checkedIcon : icon);
157}); // NB: If changed, please update Checkbox, Switch and Radio
158// so that the API documentation is updated.
159
160process.env.NODE_ENV !== "production" ? SwitchBase.propTypes = {
161 /**
162 * If `true`, the `input` element will be focused during the first mount.
163 */
164 autoFocus: _propTypes.default.bool,
165
166 /**
167 * If `true`, the component is checked.
168 */
169 checked: _propTypes.default.bool,
170
171 /**
172 * The icon to display when the component is checked.
173 */
174 checkedIcon: _propTypes.default.node.isRequired,
175
176 /**
177 * Override or extend the styles applied to the component.
178 * See [CSS API](#css) below for more details.
179 */
180 classes: _propTypes.default.object.isRequired,
181
182 /**
183 * @ignore
184 */
185 className: _propTypes.default.string,
186
187 /**
188 * @ignore
189 */
190 defaultChecked: _propTypes.default.bool,
191
192 /**
193 * If `true`, the switch will be disabled.
194 */
195 disabled: _propTypes.default.bool,
196
197 /**
198 * The icon to display when the component is unchecked.
199 */
200 icon: _propTypes.default.node.isRequired,
201
202 /**
203 * The id of the `input` element.
204 */
205 id: _propTypes.default.string,
206
207 /**
208 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
209 */
210 inputProps: _propTypes.default.object,
211
212 /**
213 * Pass a ref to the `input` element.
214 */
215 inputRef: _utils.refType,
216
217 /*
218 * @ignore
219 */
220 name: _propTypes.default.string,
221
222 /**
223 * @ignore
224 */
225 onBlur: _propTypes.default.func,
226
227 /**
228 * Callback fired when the state is changed.
229 *
230 * @param {object} event The event source of the callback.
231 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
232 */
233 onChange: _propTypes.default.func,
234
235 /**
236 * @ignore
237 */
238 onFocus: _propTypes.default.func,
239
240 /**
241 * It prevents the user from changing the value of the field
242 * (not from interacting with the field).
243 */
244 readOnly: _propTypes.default.bool,
245
246 /**
247 * If `true`, the `input` element will be required.
248 */
249 required: _propTypes.default.bool,
250
251 /**
252 * @ignore
253 */
254 tabIndex: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
255
256 /**
257 * The input component prop `type`.
258 */
259 type: _propTypes.default.string.isRequired,
260
261 /**
262 * The value of the component.
263 */
264 value: _propTypes.default.any
265} : void 0;
266
267var _default = (0, _withStyles.default)(styles, {
268 name: 'PrivateSwitchBase'
269})(SwitchBase);
270
271exports.default = _default;
\No newline at end of file