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
| const Spin: React.FC<SpinClassProps> = (props) => {
const { spinPrefixCls: prefixCls, spinning: customSpinning = true, delay = 0, className, rootClassName, size = 'default', tip, wrapperClassName, style, children, hashId, ...restProps } = props;
const [spinning, setSpinning] = React.useState<boolean>( () => customSpinning && !shouldDelay(customSpinning, delay), );
React.useEffect(() => { if (customSpinning) { const showSpinning = debounce(delay, () => { setSpinning(true); }); showSpinning(); return () => { showSpinning?.cancel?.(); }; }
setSpinning(false); }, [delay, customSpinning]);
const isNestedPattern = React.useMemo<boolean>(() => typeof children !== 'undefined', [children]);
if (process.env.NODE_ENV !== 'production') { const warning = devUseWarning('Spin');
warning(!tip || isNestedPattern, 'usage', '`tip` only work in nest pattern.'); } const { direction, spin } = React.useContext<ConfigConsumerProps>(ConfigContext); const spinClassName = classNames( prefixCls, spin?.className, { [`${prefixCls}-sm`]: size === 'small', [`${prefixCls}-lg`]: size === 'large', [`${prefixCls}-spinning`]: spinning, [`${prefixCls}-show-text`]: !!tip, [`${prefixCls}-rtl`]: direction === 'rtl', }, className, rootClassName, hashId, ); const containerClassName = classNames(`${prefixCls}-container`, { [`${prefixCls}-blur`]: spinning, });
const divProps = omit(restProps, ['indicator', 'prefixCls']);
const mergedStyle: React.CSSProperties = { ...spin?.style, ...style };
const spinElement: React.ReactNode = ( <div {...divProps} style={mergedStyle} className={spinClassName} aria-live="polite" aria-busy={spinning} > {renderIndicator(prefixCls, props)} {tip && isNestedPattern ? <div className={`${prefixCls}-text`}>{tip}</div> : null} </div> ); if (isNestedPattern) { return ( <div {...divProps} className={classNames(`${prefixCls}-nested-loading`, wrapperClassName, hashId)} > {spinning && <div key="loading">{spinElement}</div>} <div className={containerClassName} key="container"> {children} </div> </div> ); } return spinElement; };
|