UNPKG

6.77 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = exports.styles = void 0;
9
10var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
12var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13
14var _react = _interopRequireDefault(require("react"));
15
16var _propTypes = _interopRequireDefault(require("prop-types"));
17
18var _clsx = _interopRequireDefault(require("clsx"));
19
20var _utils = require("@material-ui/utils");
21
22var _useFormControl = _interopRequireDefault(require("../FormControl/useFormControl"));
23
24var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
25
26var _IconButton = _interopRequireDefault(require("../IconButton"));
27
28var 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 * @ignore - internal component.
49 */
50
51exports.styles = styles;
52
53var 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}); // NB: If changed, please update Checkbox, Switch and Radio
153// so that the API documentation is updated.
154
155
156process.env.NODE_ENV !== "production" ? SwitchBase.propTypes = {
157 /**
158 * If `true`, the `input` element will be focused during the first mount.
159 */
160 autoFocus: _propTypes.default.bool,
161
162 /**
163 * If `true`, the component is checked.
164 */
165 checked: _propTypes.default.bool,
166
167 /**
168 * The icon to display when the component is checked.
169 */
170 checkedIcon: _propTypes.default.node.isRequired,
171
172 /**
173 * Override or extend the styles applied to the component.
174 * See [CSS API](#css) below for more details.
175 */
176 classes: _propTypes.default.object.isRequired,
177
178 /**
179 * @ignore
180 */
181 className: _propTypes.default.string,
182
183 /**
184 * @ignore
185 */
186 defaultChecked: _propTypes.default.bool,
187
188 /**
189 * If `true`, the switch will be disabled.
190 */
191 disabled: _propTypes.default.bool,
192
193 /**
194 * The icon to display when the component is unchecked.
195 */
196 icon: _propTypes.default.node.isRequired,
197
198 /**
199 * The id of the `input` element.
200 */
201 id: _propTypes.default.string,
202
203 /**
204 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
205 */
206 inputProps: _propTypes.default.object,
207
208 /**
209 * Pass a ref to the `input` element.
210 */
211 inputRef: _utils.refType,
212
213 /*
214 * @ignore
215 */
216 name: _propTypes.default.string,
217
218 /**
219 * @ignore
220 */
221 onBlur: _propTypes.default.func,
222
223 /**
224 * Callback fired when the state is changed.
225 *
226 * @param {object} event The event source of the callback.
227 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
228 */
229 onChange: _propTypes.default.func,
230
231 /**
232 * @ignore
233 */
234 onFocus: _propTypes.default.func,
235
236 /**
237 * It prevents the user from changing the value of the field
238 * (not from interacting with the field).
239 */
240 readOnly: _propTypes.default.bool,
241
242 /**
243 * If `true`, the `input` element will be required.
244 */
245 required: _propTypes.default.bool,
246
247 /**
248 * @ignore
249 */
250 tabIndex: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
251
252 /**
253 * The input component prop `type`.
254 */
255 type: _propTypes.default.string.isRequired,
256
257 /**
258 * The value of the component.
259 */
260 value: _propTypes.default.any
261} : void 0;
262
263var _default = (0, _withStyles.default)(styles, {
264 name: 'PrivateSwitchBase'
265})(SwitchBase);
266
267exports.default = _default;
\No newline at end of file