UNPKG

6.19 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = void 0;
9
10var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
12var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
14var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
16var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
17
18var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
19
20var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
21
22var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
23
24var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
25
26var _react = _interopRequireDefault(require("react"));
27
28var _classnames = _interopRequireDefault(require("classnames"));
29
30require("./index.css");
31
32var Switch =
33/*#__PURE__*/
34function (_React$Component) {
35 (0, _inherits2.default)(Switch, _React$Component);
36
37 function Switch(props) {
38 var _this;
39
40 (0, _classCallCheck2.default)(this, Switch);
41 _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(Switch).call(this, props));
42 _this.node = void 0;
43
44 _this.onClick = function (e) {
45 var checked = _this.state.checked;
46 var onClick = _this.props.onClick;
47 var newChecked = !checked;
48
49 _this.setChecked(newChecked, e);
50
51 onClick && onClick(newChecked, e);
52 };
53
54 _this.onKeyDown = function (e) {
55 if (e.keyCode === 37) {
56 _this.setChecked(false, e);
57 } else if (e.keyCode === 39) {
58 _this.setChecked(true, e);
59 }
60 };
61
62 _this.onMouseUp = function (e) {
63 var onMouseUp = _this.props.onMouseUp;
64
65 _this.blur();
66
67 onMouseUp && onMouseUp(e);
68 };
69
70 var _checked = false;
71
72 if ('checked' in props) {
73 _checked = !!props.checked;
74 } else {
75 _checked = !!props.defaultChecked;
76 }
77
78 _this.state = {
79 checked: _checked,
80 isChanged: false
81 };
82 _this.node = _react.default.createRef();
83 return _this;
84 }
85
86 (0, _createClass2.default)(Switch, [{
87 key: "componentDidMount",
88 value: function componentDidMount() {
89 var _this$props = this.props,
90 autoFocus = _this$props.autoFocus,
91 disabled = _this$props.disabled,
92 loading = _this$props.loading;
93
94 if (autoFocus && !disabled && !loading) {
95 this.focus();
96 }
97 }
98 }, {
99 key: "setChecked",
100 value: function setChecked(checked, e) {
101 var isChanged = this.state.isChanged;
102 var _this$props2 = this.props,
103 disabled = _this$props2.disabled,
104 loading = _this$props2.loading,
105 onChange = _this$props2.onChange;
106
107 if (disabled || loading) {
108 return;
109 }
110
111 if (!('checked' in this.props)) {
112 this.setState({
113 checked: checked
114 });
115 }
116
117 !isChanged && this.setState({
118 isChanged: true
119 });
120 onChange && onChange(checked, e);
121 }
122 }, {
123 key: "focus",
124 value: function focus() {
125 if (this.node.current) {
126 this.node.current.focus();
127 }
128 }
129 }, {
130 key: "blur",
131 value: function blur() {
132 if (this.node.current) {
133 this.node.current.blur();
134 }
135 }
136 }, {
137 key: "render",
138 value: function render() {
139 var _classNames;
140
141 var _this$props3 = this.props,
142 className = _this$props3.className,
143 size = _this$props3.size,
144 prefixCls = _this$props3.prefixCls,
145 disabled = _this$props3.disabled,
146 loading = _this$props3.loading,
147 checkedChildren = _this$props3.checkedChildren,
148 unCheckedChildren = _this$props3.unCheckedChildren,
149 onChange = _this$props3.onChange,
150 restProps = (0, _objectWithoutProperties2.default)(_this$props3, ["className", "size", "prefixCls", "disabled", "loading", "checkedChildren", "unCheckedChildren", "onChange"]);
151 var _this$state = this.state,
152 checked = _this$state.checked,
153 isChanged = _this$state.isChanged;
154 var switchClassName = (0, _classnames.default)((_classNames = {}, (0, _defineProperty2.default)(_classNames, prefixCls, true), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "_small"), size === 'small'), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "_checked"), checked), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "_disabled"), disabled || loading), (0, _defineProperty2.default)(_classNames, className, !!className), _classNames));
155 return _react.default.createElement("button", (0, _extends2.default)({}, restProps, {
156 type: "button",
157 disabled: disabled || loading,
158 className: switchClassName,
159 ref: this.node,
160 onKeyDown: this.onKeyDown,
161 onClick: this.onClick,
162 onMouseUp: this.onMouseUp
163 }), _react.default.createElement("span", {
164 className: "".concat(prefixCls, "__child")
165 }, checked ? checkedChildren : unCheckedChildren), // 手动切换时的动画
166 isChanged && _react.default.createElement("div", {
167 className: "".concat(prefixCls, "__ani")
168 }), _react.default.createElement("div", {
169 className: "".concat(prefixCls, "__bar")
170 }, loading && _react.default.createElement("div", {
171 className: "".concat(prefixCls, "__loading")
172 })));
173 }
174 }], [{
175 key: "getDerivedStateFromProps",
176 value: function getDerivedStateFromProps(nextProps) {
177 var newState = {};
178
179 if ('checked' in nextProps) {
180 newState.checked = !!nextProps.checked;
181 }
182
183 return newState;
184 }
185 }]);
186 return Switch;
187}(_react.default.Component);
188
189Switch.defaultProps = {
190 prefixCls: 'phoenix-switch',
191 checkedChildren: null,
192 unCheckedChildren: null,
193 className: '',
194 defaultChecked: false
195};
196var _default = Switch;
197exports.default = _default;
\No newline at end of file