yantd
Version:
React component library
162 lines (161 loc) • 7.29 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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 'rc-util/lib/omit';
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 = 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 (React.createElement("span", { className: classNames(dotClassName, prefixCls + "-dot-spin") },
React.createElement("i", { className: prefixCls + "-dot-item" }),
React.createElement("i", { className: prefixCls + "-dot-item" }),
React.createElement("i", { className: prefixCls + "-dot-item" }),
React.createElement("i", { className: prefixCls + "-dot-item" })));
}
function shouldDelay(spinning, delay) {
return !!spinning && !!delay && !isNaN(Number(delay));
}
var Spin = /** @class */ (function (_super) {
__extends(Spin, _super);
function Spin(props) {
var _this = _super.call(this, props) || this;
_this.debouncifyUpdateSpinning = function (props) {
var delay = (props || _this.props).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 (_a) {
var _b, _c;
var getPrefixCls = _a.getPrefixCls, direction = _a.direction;
var _d = _this.props, customizePrefixCls = _d.prefixCls, className = _d.className, size = _d.size, tip = _d.tip, wrapperClassName = _d.wrapperClassName, style = _d.style, restProps = __rest(_d, ["prefixCls", "className", "size", "tip", "wrapperClassName", "style"]);
var spinning = _this.state.spinning;
var prefixCls = getPrefixCls('spin', customizePrefixCls);
var spinClassName = classNames(prefixCls, (_b = {},
_b[prefixCls + "-sm"] = size === 'small',
_b[prefixCls + "-lg"] = size === 'large',
_b[prefixCls + "-spinning"] = spinning,
_b[prefixCls + "-show-text"] = !!tip,
_b[prefixCls + "-rtl"] = direction === 'rtl',
_b), className);
// fix https://fb.me/react-unknown-prop
var divProps = omit(restProps, ['spinning', 'delay', 'indicator']);
var spinElement = (React.createElement("div", __assign({}, divProps, { style: style, className: spinClassName }),
renderIndicator(prefixCls, _this.props),
tip ? React.createElement("div", { className: prefixCls + "-text" }, tip) : null));
if (_this.isNestedPattern()) {
var containerClassName = classNames(prefixCls + "-container", (_c = {},
_c[prefixCls + "-blur"] = spinning,
_c));
return (React.createElement("div", __assign({}, divProps, { className: classNames(prefixCls + "-nested-loading", wrapperClassName) }),
spinning && React.createElement("div", { key: "loading" }, spinElement),
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;
}
Spin.setDefaultIndicator = function (indicator) {
defaultIndicator = indicator;
};
Spin.prototype.componentDidMount = function () {
this.updateSpinning();
};
Spin.prototype.componentDidUpdate = function () {
this.debouncifyUpdateSpinning();
this.updateSpinning();
};
Spin.prototype.componentWillUnmount = function () {
this.cancelExistingSpin();
};
Spin.prototype.cancelExistingSpin = function () {
var updateSpinning = this.updateSpinning;
if (updateSpinning && updateSpinning.cancel) {
updateSpinning.cancel();
}
};
Spin.prototype.isNestedPattern = function () {
return !!(this.props && typeof this.props.children !== 'undefined');
};
Spin.prototype.render = function () {
return React.createElement(ConfigConsumer, null, this.renderSpin);
};
Spin.defaultProps = {
spinning: true,
size: 'default',
wrapperClassName: '',
};
return Spin;
}(React.Component));
export default Spin;