choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
235 lines (202 loc) • 7.01 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 _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import React, { Component } from 'react';
import PropTypes from 'prop-types';
var classNames = require('classnames');
function noop() {}
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, ["className", "prefixCls", "disabled", "loadingIcon", "checkedValue", "unCheckedValue", "checkedChildren", "tabIndex", "unCheckedChildren"]);
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 React.createElement("span", _extends({}, restProps, {
className: switchClassName,
tabIndex: switchTabIndex,
ref: this.saveNode,
onKeyDown: this.handleKeyDown,
onClick: this.toggle,
onMouseUp: this.handleMouseUp
}), loadingIcon, React.createElement("span", {
className: "".concat(prefixCls, "-inner")
}, checked ? checkedChildren : unCheckedChildren));
}
}]);
return Switch;
}(Component);
Switch.propTypes = {
className: PropTypes.string,
prefixCls: PropTypes.string,
disabled: PropTypes.bool,
checkedChildren: PropTypes.any,
unCheckedChildren: PropTypes.any,
onChange: PropTypes.func,
onMouseUp: PropTypes.func,
onClick: PropTypes.func,
tabIndex: PropTypes.number,
checked: PropTypes.bool,
defaultChecked: PropTypes.bool,
autoFocus: PropTypes.bool,
loadingIcon: PropTypes.node,
checkedValue: PropTypes.any,
unCheckedValue: PropTypes.any
};
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