index.js
6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import classNames from 'classnames';
import omit from 'omit.js';
import debounce from 'lodash/debounce';
import { ConfigConsumer } from '../config-provider';
import { tuple } from '../_util/type';
import { isValidElement, cloneElement } from '../_util/reactNode';
var SpinSizes = tuple('small', 'default', 'large'); // Render indicator
var defaultIndicator = null;
function renderIndicator(prefixCls, props) {
var indicator = props.indicator;
var dotClassName = "".concat(prefixCls, "-dot"); // should not be render default indicator when indicator value is null
if (indicator === null) {
return null;
}
if (isValidElement(indicator)) {
return cloneElement(indicator, {
className: classNames(indicator.props.className, dotClassName)
});
}
if (isValidElement(defaultIndicator)) {
return cloneElement(defaultIndicator, {
className: classNames(defaultIndicator.props.className, dotClassName)
});
}
return /*#__PURE__*/React.createElement("span", {
className: classNames(dotClassName, "".concat(prefixCls, "-dot-spin"))
}, /*#__PURE__*/React.createElement("i", {
className: "".concat(prefixCls, "-dot-item")
}), /*#__PURE__*/React.createElement("i", {
className: "".concat(prefixCls, "-dot-item")
}), /*#__PURE__*/React.createElement("i", {
className: "".concat(prefixCls, "-dot-item")
}), /*#__PURE__*/React.createElement("i", {
className: "".concat(prefixCls, "-dot-item")
}));
}
function shouldDelay(spinning, delay) {
return !!spinning && !!delay && !isNaN(Number(delay));
}
var Spin = /*#__PURE__*/function (_React$Component) {
_inherits(Spin, _React$Component);
var _super = _createSuper(Spin);
function Spin(props) {
var _this;
_classCallCheck(this, Spin);
_this = _super.call(this, props);
_this.debouncifyUpdateSpinning = function (props) {
var _ref = props || _this.props,
delay = _ref.delay;
if (delay) {
_this.cancelExistingSpin();
_this.updateSpinning = debounce(_this.originalUpdateSpinning, delay);
}
};
_this.updateSpinning = function () {
var spinning = _this.props.spinning;
var currentSpinning = _this.state.spinning;
if (currentSpinning !== spinning) {
_this.setState({
spinning: spinning
});
}
};
_this.renderSpin = function (_ref2) {
var _classNames;
var getPrefixCls = _ref2.getPrefixCls,
direction = _ref2.direction;
var _a = _this.props,
customizePrefixCls = _a.prefixCls,
className = _a.className,
size = _a.size,
tip = _a.tip,
wrapperClassName = _a.wrapperClassName,
style = _a.style,
restProps = __rest(_a, ["prefixCls", "className", "size", "tip", "wrapperClassName", "style"]);
var spinning = _this.state.spinning;
var prefixCls = getPrefixCls('spin', customizePrefixCls);
var spinClassName = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-sm"), size === 'small'), _defineProperty(_classNames, "".concat(prefixCls, "-lg"), size === 'large'), _defineProperty(_classNames, "".concat(prefixCls, "-spinning"), spinning), _defineProperty(_classNames, "".concat(prefixCls, "-show-text"), !!tip), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames), className); // fix https://fb.me/react-unknown-prop
var divProps = omit(restProps, ['spinning', 'delay', 'indicator']);
var spinElement = /*#__PURE__*/React.createElement("div", _extends({}, divProps, {
style: style,
className: spinClassName
}), renderIndicator(prefixCls, _this.props), tip ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-text")
}, tip) : null);
if (_this.isNestedPattern()) {
var containerClassName = classNames("".concat(prefixCls, "-container"), _defineProperty({}, "".concat(prefixCls, "-blur"), spinning));
return /*#__PURE__*/React.createElement("div", _extends({}, divProps, {
className: classNames("".concat(prefixCls, "-nested-loading"), wrapperClassName)
}), spinning && /*#__PURE__*/React.createElement("div", {
key: "loading"
}, spinElement), /*#__PURE__*/React.createElement("div", {
className: containerClassName,
key: "container"
}, _this.props.children));
}
return spinElement;
};
var spinning = props.spinning,
delay = props.delay;
var shouldBeDelayed = shouldDelay(spinning, delay);
_this.state = {
spinning: spinning && !shouldBeDelayed
};
_this.originalUpdateSpinning = _this.updateSpinning;
_this.debouncifyUpdateSpinning(props);
return _this;
}
_createClass(Spin, [{
key: "componentDidMount",
value: function componentDidMount() {
this.updateSpinning();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.debouncifyUpdateSpinning();
this.updateSpinning();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.cancelExistingSpin();
}
}, {
key: "cancelExistingSpin",
value: function cancelExistingSpin() {
var updateSpinning = this.updateSpinning;
if (updateSpinning && updateSpinning.cancel) {
updateSpinning.cancel();
}
}
}, {
key: "isNestedPattern",
value: function isNestedPattern() {
return !!(this.props && typeof this.props.children !== 'undefined');
}
}, {
key: "render",
value: function render() {
return /*#__PURE__*/React.createElement(ConfigConsumer, null, this.renderSpin);
}
}], [{
key: "setDefaultIndicator",
value: function setDefaultIndicator(indicator) {
defaultIndicator = indicator;
}
}]);
return Spin;
}(React.Component);
Spin.defaultProps = {
spinning: true,
size: 'default',
wrapperClassName: ''
};
export default Spin;