271 lines
6.9 KiB
JavaScript
271 lines
6.9 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = exports.styles = void 0;
|
|
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
|
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
|
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
|
|
var React = _interopRequireWildcard(require("react"));
|
|
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
|
|
var _clsx = _interopRequireDefault(require("clsx"));
|
|
|
|
var _utils = require("@material-ui/utils");
|
|
|
|
var _useControlled3 = _interopRequireDefault(require("../utils/useControlled"));
|
|
|
|
var _useFormControl = _interopRequireDefault(require("../FormControl/useFormControl"));
|
|
|
|
var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
|
|
|
|
var _IconButton = _interopRequireDefault(require("../IconButton"));
|
|
|
|
var styles = {
|
|
root: {
|
|
padding: 9
|
|
},
|
|
checked: {},
|
|
disabled: {},
|
|
input: {
|
|
cursor: 'inherit',
|
|
position: 'absolute',
|
|
opacity: 0,
|
|
width: '100%',
|
|
height: '100%',
|
|
top: 0,
|
|
left: 0,
|
|
margin: 0,
|
|
padding: 0,
|
|
zIndex: 1
|
|
}
|
|
};
|
|
/**
|
|
* @ignore - internal component.
|
|
*/
|
|
|
|
exports.styles = styles;
|
|
var SwitchBase = /*#__PURE__*/React.forwardRef(function SwitchBase(props, ref) {
|
|
var autoFocus = props.autoFocus,
|
|
checkedProp = props.checked,
|
|
checkedIcon = props.checkedIcon,
|
|
classes = props.classes,
|
|
className = props.className,
|
|
defaultChecked = props.defaultChecked,
|
|
disabledProp = props.disabled,
|
|
icon = props.icon,
|
|
id = props.id,
|
|
inputProps = props.inputProps,
|
|
inputRef = props.inputRef,
|
|
name = props.name,
|
|
onBlur = props.onBlur,
|
|
onChange = props.onChange,
|
|
onFocus = props.onFocus,
|
|
readOnly = props.readOnly,
|
|
required = props.required,
|
|
tabIndex = props.tabIndex,
|
|
type = props.type,
|
|
value = props.value,
|
|
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"]);
|
|
|
|
var _useControlled = (0, _useControlled3.default)({
|
|
controlled: checkedProp,
|
|
default: Boolean(defaultChecked),
|
|
name: 'SwitchBase',
|
|
state: 'checked'
|
|
}),
|
|
_useControlled2 = (0, _slicedToArray2.default)(_useControlled, 2),
|
|
checked = _useControlled2[0],
|
|
setCheckedState = _useControlled2[1];
|
|
|
|
var muiFormControl = (0, _useFormControl.default)();
|
|
|
|
var handleFocus = function handleFocus(event) {
|
|
if (onFocus) {
|
|
onFocus(event);
|
|
}
|
|
|
|
if (muiFormControl && muiFormControl.onFocus) {
|
|
muiFormControl.onFocus(event);
|
|
}
|
|
};
|
|
|
|
var handleBlur = function handleBlur(event) {
|
|
if (onBlur) {
|
|
onBlur(event);
|
|
}
|
|
|
|
if (muiFormControl && muiFormControl.onBlur) {
|
|
muiFormControl.onBlur(event);
|
|
}
|
|
};
|
|
|
|
var handleInputChange = function handleInputChange(event) {
|
|
var newChecked = event.target.checked;
|
|
setCheckedState(newChecked);
|
|
|
|
if (onChange) {
|
|
// TODO v5: remove the second argument.
|
|
onChange(event, newChecked);
|
|
}
|
|
};
|
|
|
|
var disabled = disabledProp;
|
|
|
|
if (muiFormControl) {
|
|
if (typeof disabled === 'undefined') {
|
|
disabled = muiFormControl.disabled;
|
|
}
|
|
}
|
|
|
|
var hasLabelFor = type === 'checkbox' || type === 'radio';
|
|
return /*#__PURE__*/React.createElement(_IconButton.default, (0, _extends2.default)({
|
|
component: "span",
|
|
className: (0, _clsx.default)(classes.root, className, checked && classes.checked, disabled && classes.disabled),
|
|
disabled: disabled,
|
|
tabIndex: null,
|
|
role: undefined,
|
|
onFocus: handleFocus,
|
|
onBlur: handleBlur,
|
|
ref: ref
|
|
}, other), /*#__PURE__*/React.createElement("input", (0, _extends2.default)({
|
|
autoFocus: autoFocus,
|
|
checked: checkedProp,
|
|
defaultChecked: defaultChecked,
|
|
className: classes.input,
|
|
disabled: disabled,
|
|
id: hasLabelFor && id,
|
|
name: name,
|
|
onChange: handleInputChange,
|
|
readOnly: readOnly,
|
|
ref: inputRef,
|
|
required: required,
|
|
tabIndex: tabIndex,
|
|
type: type,
|
|
value: value
|
|
}, inputProps)), checked ? checkedIcon : icon);
|
|
}); // NB: If changed, please update Checkbox, Switch and Radio
|
|
// so that the API documentation is updated.
|
|
|
|
process.env.NODE_ENV !== "production" ? SwitchBase.propTypes = {
|
|
/**
|
|
* If `true`, the `input` element will be focused during the first mount.
|
|
*/
|
|
autoFocus: _propTypes.default.bool,
|
|
|
|
/**
|
|
* If `true`, the component is checked.
|
|
*/
|
|
checked: _propTypes.default.bool,
|
|
|
|
/**
|
|
* The icon to display when the component is checked.
|
|
*/
|
|
checkedIcon: _propTypes.default.node.isRequired,
|
|
|
|
/**
|
|
* Override or extend the styles applied to the component.
|
|
* See [CSS API](#css) below for more details.
|
|
*/
|
|
classes: _propTypes.default.object.isRequired,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
className: _propTypes.default.string,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
defaultChecked: _propTypes.default.bool,
|
|
|
|
/**
|
|
* If `true`, the switch will be disabled.
|
|
*/
|
|
disabled: _propTypes.default.bool,
|
|
|
|
/**
|
|
* The icon to display when the component is unchecked.
|
|
*/
|
|
icon: _propTypes.default.node.isRequired,
|
|
|
|
/**
|
|
* The id of the `input` element.
|
|
*/
|
|
id: _propTypes.default.string,
|
|
|
|
/**
|
|
* [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
|
|
*/
|
|
inputProps: _propTypes.default.object,
|
|
|
|
/**
|
|
* Pass a ref to the `input` element.
|
|
*/
|
|
inputRef: _utils.refType,
|
|
|
|
/*
|
|
* @ignore
|
|
*/
|
|
name: _propTypes.default.string,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onBlur: _propTypes.default.func,
|
|
|
|
/**
|
|
* Callback fired when the state is changed.
|
|
*
|
|
* @param {object} event The event source of the callback.
|
|
* You can pull out the new checked state by accessing `event.target.checked` (boolean).
|
|
*/
|
|
onChange: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onFocus: _propTypes.default.func,
|
|
|
|
/**
|
|
* It prevents the user from changing the value of the field
|
|
* (not from interacting with the field).
|
|
*/
|
|
readOnly: _propTypes.default.bool,
|
|
|
|
/**
|
|
* If `true`, the `input` element will be required.
|
|
*/
|
|
required: _propTypes.default.bool,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
tabIndex: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
|
|
|
|
/**
|
|
* The input component prop `type`.
|
|
*/
|
|
type: _propTypes.default.string.isRequired,
|
|
|
|
/**
|
|
* The value of the component.
|
|
*/
|
|
value: _propTypes.default.any
|
|
} : void 0;
|
|
|
|
var _default = (0, _withStyles.default)(styles, {
|
|
name: 'PrivateSwitchBase'
|
|
})(SwitchBase);
|
|
|
|
exports.default = _default; |