index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import classNames from 'classnames';
  12. import { debounce } from 'throttle-debounce';
  13. import { devUseWarning } from '../_util/warning';
  14. import { useComponentConfig } from '../config-provider/context';
  15. import Indicator from './Indicator';
  16. import useStyle from './style/index';
  17. import usePercent from './usePercent';
  18. const _SpinSizes = ['small', 'default', 'large'];
  19. // Render indicator
  20. let defaultIndicator;
  21. function shouldDelay(spinning, delay) {
  22. return !!spinning && !!delay && !Number.isNaN(Number(delay));
  23. }
  24. const Spin = props => {
  25. var _a;
  26. const {
  27. prefixCls: customizePrefixCls,
  28. spinning: customSpinning = true,
  29. delay = 0,
  30. className,
  31. rootClassName,
  32. size = 'default',
  33. tip,
  34. wrapperClassName,
  35. style,
  36. children,
  37. fullscreen = false,
  38. indicator,
  39. percent
  40. } = props,
  41. restProps = __rest(props, ["prefixCls", "spinning", "delay", "className", "rootClassName", "size", "tip", "wrapperClassName", "style", "children", "fullscreen", "indicator", "percent"]);
  42. const {
  43. getPrefixCls,
  44. direction,
  45. className: contextClassName,
  46. style: contextStyle,
  47. indicator: contextIndicator
  48. } = useComponentConfig('spin');
  49. const prefixCls = getPrefixCls('spin', customizePrefixCls);
  50. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
  51. const [spinning, setSpinning] = React.useState(() => customSpinning && !shouldDelay(customSpinning, delay));
  52. const mergedPercent = usePercent(spinning, percent);
  53. React.useEffect(() => {
  54. if (customSpinning) {
  55. const showSpinning = debounce(delay, () => {
  56. setSpinning(true);
  57. });
  58. showSpinning();
  59. return () => {
  60. var _a;
  61. (_a = showSpinning === null || showSpinning === void 0 ? void 0 : showSpinning.cancel) === null || _a === void 0 ? void 0 : _a.call(showSpinning);
  62. };
  63. }
  64. setSpinning(false);
  65. }, [delay, customSpinning]);
  66. const isNestedPattern = React.useMemo(() => typeof children !== 'undefined' && !fullscreen, [children, fullscreen]);
  67. if (process.env.NODE_ENV !== 'production') {
  68. const warning = devUseWarning('Spin');
  69. process.env.NODE_ENV !== "production" ? warning(!tip || isNestedPattern || fullscreen, 'usage', '`tip` only work in nest or fullscreen pattern.') : void 0;
  70. }
  71. const spinClassName = classNames(prefixCls, contextClassName, {
  72. [`${prefixCls}-sm`]: size === 'small',
  73. [`${prefixCls}-lg`]: size === 'large',
  74. [`${prefixCls}-spinning`]: spinning,
  75. [`${prefixCls}-show-text`]: !!tip,
  76. [`${prefixCls}-rtl`]: direction === 'rtl'
  77. }, className, !fullscreen && rootClassName, hashId, cssVarCls);
  78. const containerClassName = classNames(`${prefixCls}-container`, {
  79. [`${prefixCls}-blur`]: spinning
  80. });
  81. const mergedIndicator = (_a = indicator !== null && indicator !== void 0 ? indicator : contextIndicator) !== null && _a !== void 0 ? _a : defaultIndicator;
  82. const mergedStyle = Object.assign(Object.assign({}, contextStyle), style);
  83. const spinElement = /*#__PURE__*/React.createElement("div", Object.assign({}, restProps, {
  84. style: mergedStyle,
  85. className: spinClassName,
  86. "aria-live": "polite",
  87. "aria-busy": spinning
  88. }), /*#__PURE__*/React.createElement(Indicator, {
  89. prefixCls: prefixCls,
  90. indicator: mergedIndicator,
  91. percent: mergedPercent
  92. }), tip && (isNestedPattern || fullscreen) ? (/*#__PURE__*/React.createElement("div", {
  93. className: `${prefixCls}-text`
  94. }, tip)) : null);
  95. if (isNestedPattern) {
  96. return wrapCSSVar(/*#__PURE__*/React.createElement("div", Object.assign({}, restProps, {
  97. className: classNames(`${prefixCls}-nested-loading`, wrapperClassName, hashId, cssVarCls)
  98. }), spinning && /*#__PURE__*/React.createElement("div", {
  99. key: "loading"
  100. }, spinElement), /*#__PURE__*/React.createElement("div", {
  101. className: containerClassName,
  102. key: "container"
  103. }, children)));
  104. }
  105. if (fullscreen) {
  106. return wrapCSSVar(/*#__PURE__*/React.createElement("div", {
  107. className: classNames(`${prefixCls}-fullscreen`, {
  108. [`${prefixCls}-fullscreen-show`]: spinning
  109. }, rootClassName, hashId, cssVarCls)
  110. }, spinElement));
  111. }
  112. return wrapCSSVar(spinElement);
  113. };
  114. Spin.setDefaultIndicator = indicator => {
  115. defaultIndicator = indicator;
  116. };
  117. if (process.env.NODE_ENV !== 'production') {
  118. Spin.displayName = 'Spin';
  119. }
  120. export default Spin;