choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
182 lines (158 loc) • 5.69 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["className", "prefixCls", "disabled", "loadingIcon", "checkedValue", "unCheckedValue", "checkedChildren", "tabIndex", "unCheckedChildren"];
import React, { Component } from 'react';
import noop from 'lodash/noop';
import classNames from 'classnames';
var Switch = /*#__PURE__*/function (_Component) {
_inherits(Switch, _Component);
var _super = _createSuper(Switch);
function Switch(props) {
var _this;
_classCallCheck(this, Switch);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "toggle", function () {
var onClick = _this.props.onClick;
var checked = !_this.state.checked;
_this.setChecked(checked);
onClick(checked);
});
_defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (e) {
if (e.keyCode === 37) {
// Left
_this.setChecked(false);
} else if (e.keyCode === 39) {
// Right
_this.setChecked(true);
} else if (e.keyCode === 32 || e.keyCode === 13) {
// Space, Enter
_this.toggle();
}
});
_defineProperty(_assertThisInitialized(_this), "handleMouseUp", function (e) {
if (_this.node) {
_this.node.blur();
}
if (_this.props.onMouseUp) {
_this.props.onMouseUp(e);
}
});
_defineProperty(_assertThisInitialized(_this), "saveNode", function (node) {
_this.node = node;
});
var _checked = false;
if ('checked' in props) {
_checked = !!props.checked;
} else if ('value' in props) {
_checked = props.checkedValue === props.value;
} else {
_checked = !!props.defaultChecked;
}
_this.state = {
checked: _checked
};
return _this;
}
_createClass(Switch, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this$props = this.props,
autoFocus = _this$props.autoFocus,
disabled = _this$props.disabled;
if (autoFocus && !disabled) {
this.focus();
}
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if ('checked' in nextProps) {
this.setState({
checked: !!nextProps.checked
});
} else if ('value' in nextProps) {
this.setState({
checked: nextProps.checkedValue === nextProps.value
});
}
}
}, {
key: "setChecked",
value: function setChecked(checked) {
if (this.props.disabled) {
return;
}
if (!('checked' in this.props)) {
this.setState({
checked: checked
});
}
var _this$props2 = this.props,
_this$props2$checkedV = _this$props2.checkedValue,
checkedValue = _this$props2$checkedV === void 0 ? true : _this$props2$checkedV,
_this$props2$unChecke = _this$props2.unCheckedValue,
unCheckedValue = _this$props2$unChecke === void 0 ? false : _this$props2$unChecke;
var value = checked ? checkedValue : unCheckedValue;
this.props.onChange(value);
}
}, {
key: "focus",
value: function focus() {
this.node.focus();
}
}, {
key: "blur",
value: function blur() {
this.node.blur();
}
}, {
key: "render",
value: function render() {
var _classNames;
var _this$props3 = this.props,
className = _this$props3.className,
prefixCls = _this$props3.prefixCls,
disabled = _this$props3.disabled,
loadingIcon = _this$props3.loadingIcon,
checkedValue = _this$props3.checkedValue,
unCheckedValue = _this$props3.unCheckedValue,
checkedChildren = _this$props3.checkedChildren,
tabIndex = _this$props3.tabIndex,
unCheckedChildren = _this$props3.unCheckedChildren,
restProps = _objectWithoutProperties(_this$props3, _excluded);
var checked = this.state.checked;
var switchTabIndex = disabled ? -1 : tabIndex || 0;
var switchClassName = classNames((_classNames = {}, _defineProperty(_classNames, className, !!className), _defineProperty(_classNames, prefixCls, true), _defineProperty(_classNames, "".concat(prefixCls, "-checked"), checked), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames));
return /*#__PURE__*/React.createElement("span", _extends({}, restProps, {
className: switchClassName,
tabIndex: switchTabIndex,
ref: this.saveNode,
onKeyDown: this.handleKeyDown,
onClick: this.toggle,
onMouseUp: this.handleMouseUp
}), loadingIcon, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-inner")
}, checked ? checkedChildren : unCheckedChildren));
}
}]);
return Switch;
}(Component);
Switch.defaultProps = {
prefixCls: 'rc-switch',
checkedChildren: null,
unCheckedChildren: null,
className: '',
defaultChecked: false,
checkedValue: true,
unCheckedValue: false,
onChange: noop,
onClick: noop
};
export default Switch;
//# sourceMappingURL=Switch.js.map