284 lines
8.0 KiB
JavaScript
284 lines
8.0 KiB
JavaScript
"use strict";
|
|
|
|
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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
|
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
|
|
var _getPrototypeOf = _interopRequireDefault(require("@babel/runtime/core-js/object/get-prototype-of"));
|
|
|
|
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
|
|
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
|
|
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
|
|
|
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
|
|
|
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
|
|
|
|
var _react = _interopRequireDefault(require("react"));
|
|
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
|
|
var _classnames = _interopRequireDefault(require("classnames"));
|
|
|
|
var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
|
|
|
|
var _IconButton = _interopRequireDefault(require("../IconButton"));
|
|
|
|
// @inheritedComponent IconButton
|
|
var styles = {
|
|
root: {
|
|
display: 'inline-flex',
|
|
alignItems: 'center',
|
|
transition: 'none',
|
|
'&:hover': {
|
|
// Disable the hover effect for the IconButton.
|
|
backgroundColor: 'transparent'
|
|
}
|
|
},
|
|
checked: {},
|
|
disabled: {},
|
|
input: {
|
|
cursor: 'inherit',
|
|
position: 'absolute',
|
|
opacity: 0,
|
|
width: '100%',
|
|
height: '100%',
|
|
top: 0,
|
|
left: 0,
|
|
margin: 0,
|
|
padding: 0
|
|
}
|
|
};
|
|
exports.styles = styles;
|
|
|
|
var SwitchBase =
|
|
/*#__PURE__*/
|
|
function (_React$Component) {
|
|
(0, _inherits2.default)(SwitchBase, _React$Component);
|
|
|
|
function SwitchBase(props, context) {
|
|
var _this;
|
|
|
|
(0, _classCallCheck2.default)(this, SwitchBase);
|
|
_this = (0, _possibleConstructorReturn2.default)(this, (SwitchBase.__proto__ || (0, _getPrototypeOf.default)(SwitchBase)).call(this, props, context));
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "state", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: {}
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "input", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: null
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "isControlled", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: null
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "handleInputChange", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: function value(event) {
|
|
var checked = event.target.checked;
|
|
|
|
if (!_this.isControlled) {
|
|
_this.setState({
|
|
checked: checked
|
|
});
|
|
}
|
|
|
|
if (_this.props.onChange) {
|
|
_this.props.onChange(event, checked);
|
|
}
|
|
}
|
|
});
|
|
_this.isControlled = props.checked != null;
|
|
|
|
if (!_this.isControlled) {
|
|
// not controlled, use internal state
|
|
_this.state.checked = props.defaultChecked !== undefined ? props.defaultChecked : false;
|
|
}
|
|
|
|
return _this;
|
|
}
|
|
|
|
(0, _createClass2.default)(SwitchBase, [{
|
|
key: "render",
|
|
value: function render() {
|
|
var _classNames;
|
|
|
|
var _props = this.props,
|
|
checkedProp = _props.checked,
|
|
checkedIcon = _props.checkedIcon,
|
|
classes = _props.classes,
|
|
classNameProp = _props.className,
|
|
disabledProp = _props.disabled,
|
|
icon = _props.icon,
|
|
id = _props.id,
|
|
inputProps = _props.inputProps,
|
|
inputRef = _props.inputRef,
|
|
name = _props.name,
|
|
onChange = _props.onChange,
|
|
tabIndex = _props.tabIndex,
|
|
type = _props.type,
|
|
value = _props.value,
|
|
other = (0, _objectWithoutProperties2.default)(_props, ["checked", "checkedIcon", "classes", "className", "disabled", "icon", "id", "inputProps", "inputRef", "name", "onChange", "tabIndex", "type", "value"]);
|
|
var muiFormControl = this.context.muiFormControl;
|
|
var disabled = disabledProp;
|
|
|
|
if (muiFormControl) {
|
|
if (typeof disabled === 'undefined') {
|
|
disabled = muiFormControl.disabled;
|
|
}
|
|
}
|
|
|
|
var checked = this.isControlled ? checkedProp : this.state.checked;
|
|
var hasLabelFor = type === 'checkbox' || type === 'radio';
|
|
return _react.default.createElement(_IconButton.default, (0, _extends2.default)({
|
|
component: "span",
|
|
className: (0, _classnames.default)(classes.root, (_classNames = {}, (0, _defineProperty2.default)(_classNames, classes.checked, checked), (0, _defineProperty2.default)(_classNames, classes.disabled, disabled), _classNames), classNameProp),
|
|
disabled: disabled,
|
|
tabIndex: null,
|
|
role: undefined
|
|
}, other), checked ? checkedIcon : icon, _react.default.createElement("input", (0, _extends2.default)({
|
|
id: hasLabelFor && id,
|
|
type: type,
|
|
name: name,
|
|
checked: checked,
|
|
onChange: this.handleInputChange,
|
|
className: classes.input,
|
|
disabled: disabled,
|
|
tabIndex: tabIndex,
|
|
value: value,
|
|
ref: inputRef
|
|
}, inputProps)));
|
|
}
|
|
}]);
|
|
return SwitchBase;
|
|
}(_react.default.Component); // NB: If changed, please update Checkbox, Switch and Radio
|
|
// so that the API documentation is updated.
|
|
|
|
|
|
SwitchBase.propTypes = process.env.NODE_ENV !== "production" ? {
|
|
/**
|
|
* If `true`, the component is checked.
|
|
*/
|
|
checked: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.string]),
|
|
|
|
/**
|
|
* The icon to display when the component is checked.
|
|
*/
|
|
checkedIcon: _propTypes.default.node.isRequired,
|
|
|
|
/**
|
|
* Useful to extend the style applied to components.
|
|
*/
|
|
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,
|
|
|
|
/**
|
|
* If `true`, the ripple effect will be disabled.
|
|
*/
|
|
disableRipple: _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,
|
|
|
|
/**
|
|
* If `true`, the component appears indeterminate.
|
|
*/
|
|
indeterminate: _propTypes.default.bool,
|
|
|
|
/**
|
|
* The icon to display when the component is indeterminate.
|
|
*/
|
|
indeterminateIcon: _propTypes.default.node,
|
|
|
|
/**
|
|
* Properties applied to the `input` element.
|
|
*/
|
|
inputProps: _propTypes.default.object,
|
|
|
|
/**
|
|
* Use that property to pass a ref callback to the native input component.
|
|
*/
|
|
inputRef: _propTypes.default.func,
|
|
|
|
/*
|
|
* @ignore
|
|
*/
|
|
name: _propTypes.default.string,
|
|
|
|
/**
|
|
* Callback fired when the state is changed.
|
|
*
|
|
* @param {object} event The event source of the callback.
|
|
* You can pull out the new value by accessing `event.target.checked`.
|
|
* @param {boolean} checked The `checked` value of the switch
|
|
*/
|
|
onChange: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
tabIndex: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
|
|
|
|
/**
|
|
* The input component property `type`.
|
|
*/
|
|
type: _propTypes.default.string,
|
|
|
|
/**
|
|
* The value of the component.
|
|
*/
|
|
value: _propTypes.default.string
|
|
} : {};
|
|
SwitchBase.defaultProps = {
|
|
type: 'checkbox'
|
|
};
|
|
SwitchBase.contextTypes = {
|
|
muiFormControl: _propTypes.default.object
|
|
};
|
|
|
|
var _default = (0, _withStyles.default)(styles, {
|
|
name: 'MuiSwitchBase'
|
|
})(SwitchBase);
|
|
|
|
exports.default = _default; |