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