UNPKG

antd

Version:

An enterprise-class UI design language and React components implementation

208 lines (173 loc) 6.94 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _createSuper from "@babel/runtime/helpers/esm/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 "rc-util/es/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 = "".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;