2023-09-11 17:28:04 +08:00
|
|
|
import * as React from 'react';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2023-05-12 09:51:44 +08:00
|
|
|
import { debounce } from 'throttle-debounce';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
import { devUseWarning } from '../_util/warning';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
2022-12-25 18:34:42 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2024-06-03 11:30:27 +08:00
|
|
|
import Indicator from './Indicator';
|
2022-03-25 14:26:09 +08:00
|
|
|
import useStyle from './style/index';
|
2024-06-03 11:30:27 +08:00
|
|
|
import usePercent from './usePercent';
|
2015-10-31 02:00:03 +08:00
|
|
|
|
2022-12-13 14:57:40 +08:00
|
|
|
const SpinSizes = ['small', 'default', 'large'] as const;
|
2023-11-29 17:23:45 +08:00
|
|
|
export type SpinSize = (typeof SpinSizes)[number];
|
2019-06-16 20:51:47 +08:00
|
|
|
export type SpinIndicator = React.ReactElement<HTMLElement>;
|
2018-02-14 12:30:31 +08:00
|
|
|
|
2016-08-10 09:46:56 +08:00
|
|
|
export interface SpinProps {
|
2024-05-23 17:02:39 +08:00
|
|
|
/** Customize prefix class name */
|
2016-08-10 09:46:56 +08:00
|
|
|
prefixCls?: string;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** Additional class name of Spin */
|
2016-08-10 09:46:56 +08:00
|
|
|
className?: string;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** Additional root class name of Spin */
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** Whether Spin is spinning */
|
2016-08-10 09:46:56 +08:00
|
|
|
spinning?: boolean;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** Style of Spin */
|
2017-11-22 14:46:40 +08:00
|
|
|
style?: React.CSSProperties;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** Size of Spin, options: `small`, `default` and `large` */
|
2018-04-16 10:58:17 +08:00
|
|
|
size?: SpinSize;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** Customize description content when Spin has children */
|
2021-11-05 18:44:49 +08:00
|
|
|
tip?: React.ReactNode;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** Specifies a delay in milliseconds for loading state (prevent flush) */
|
2017-01-01 01:13:34 +08:00
|
|
|
delay?: number;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** The className of wrapper when Spin has children */
|
2017-03-21 17:12:43 +08:00
|
|
|
wrapperClassName?: string;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** React node of the spinning indicator */
|
2018-02-14 12:30:31 +08:00
|
|
|
indicator?: SpinIndicator;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** Children of Spin */
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
2024-05-23 17:02:39 +08:00
|
|
|
/** Display a backdrop with the `Spin` component */
|
2023-10-19 13:47:48 +08:00
|
|
|
fullscreen?: boolean;
|
2024-06-03 11:30:27 +08:00
|
|
|
percent?: number | 'auto';
|
2016-08-10 09:46:56 +08:00
|
|
|
}
|
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
export type SpinType = React.FC<SpinProps> & {
|
2022-03-25 14:26:09 +08:00
|
|
|
setDefaultIndicator: (indicator: React.ReactNode) => void;
|
|
|
|
};
|
|
|
|
|
2018-06-06 19:32:30 +08:00
|
|
|
// Render indicator
|
2024-06-03 11:30:27 +08:00
|
|
|
let defaultIndicator: React.ReactNode | undefined;
|
2018-06-06 19:32:30 +08:00
|
|
|
|
2018-09-08 22:43:39 +08:00
|
|
|
function shouldDelay(spinning?: boolean, delay?: number): boolean {
|
|
|
|
return !!spinning && !!delay && !isNaN(Number(delay));
|
|
|
|
}
|
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const Spin: SpinType = (props) => {
|
2022-10-14 22:07:02 +08:00
|
|
|
const {
|
2023-12-14 14:58:53 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2022-10-14 22:07:02 +08:00
|
|
|
spinning: customSpinning = true,
|
2022-12-16 15:02:32 +08:00
|
|
|
delay = 0,
|
2022-10-14 22:07:02 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-10-14 22:07:02 +08:00
|
|
|
size = 'default',
|
|
|
|
tip,
|
|
|
|
wrapperClassName,
|
|
|
|
style,
|
|
|
|
children,
|
2024-01-19 22:20:50 +08:00
|
|
|
fullscreen = false,
|
2024-06-03 11:30:27 +08:00
|
|
|
indicator,
|
|
|
|
percent,
|
2022-10-14 22:07:02 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
2019-01-07 10:25:23 +08:00
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('spin', customizePrefixCls);
|
|
|
|
|
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
|
|
|
|
2022-10-14 22:07:02 +08:00
|
|
|
const [spinning, setSpinning] = React.useState<boolean>(
|
|
|
|
() => customSpinning && !shouldDelay(customSpinning, delay),
|
|
|
|
);
|
2018-09-08 22:43:39 +08:00
|
|
|
|
2024-06-03 11:30:27 +08:00
|
|
|
const mergedPercent = usePercent(spinning, percent);
|
|
|
|
|
2022-10-14 22:07:02 +08:00
|
|
|
React.useEffect(() => {
|
2023-02-07 23:46:10 +08:00
|
|
|
if (customSpinning) {
|
|
|
|
const showSpinning = debounce(delay, () => {
|
|
|
|
setSpinning(true);
|
|
|
|
});
|
|
|
|
showSpinning();
|
|
|
|
return () => {
|
|
|
|
showSpinning?.cancel?.();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
setSpinning(false);
|
2022-10-14 22:07:02 +08:00
|
|
|
}, [delay, customSpinning]);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2023-10-19 13:47:48 +08:00
|
|
|
const isNestedPattern = React.useMemo<boolean>(
|
|
|
|
() => typeof children !== 'undefined' && !fullscreen,
|
|
|
|
[children, fullscreen],
|
|
|
|
);
|
2022-12-25 18:34:42 +08:00
|
|
|
|
2023-05-12 09:51:44 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Spin');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2024-02-24 14:50:03 +08:00
|
|
|
warning(
|
|
|
|
!tip || isNestedPattern || fullscreen,
|
|
|
|
'usage',
|
|
|
|
'`tip` only work in nest or fullscreen pattern.',
|
|
|
|
);
|
2023-05-12 09:51:44 +08:00
|
|
|
}
|
|
|
|
|
2023-06-20 19:34:45 +08:00
|
|
|
const { direction, spin } = React.useContext<ConfigConsumerProps>(ConfigContext);
|
2022-12-25 18:34:42 +08:00
|
|
|
|
|
|
|
const spinClassName = classNames(
|
|
|
|
prefixCls,
|
2023-06-20 19:34:45 +08:00
|
|
|
spin?.className,
|
2022-12-25 18:34:42 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-spinning`]: spinning,
|
|
|
|
[`${prefixCls}-show-text`]: !!tip,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
2024-06-03 11:30:27 +08:00
|
|
|
!fullscreen && rootClassName,
|
2022-12-25 18:34:42 +08:00
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2022-12-25 18:34:42 +08:00
|
|
|
);
|
2015-10-27 10:10:27 +08:00
|
|
|
|
2022-12-25 18:34:42 +08:00
|
|
|
const containerClassName = classNames(`${prefixCls}-container`, {
|
|
|
|
[`${prefixCls}-blur`]: spinning,
|
|
|
|
});
|
|
|
|
|
2023-06-25 11:42:31 +08:00
|
|
|
const mergedStyle: React.CSSProperties = { ...spin?.style, ...style };
|
2023-06-20 19:34:45 +08:00
|
|
|
|
2022-12-25 18:34:42 +08:00
|
|
|
const spinElement: React.ReactNode = (
|
|
|
|
<div
|
2024-06-03 11:30:27 +08:00
|
|
|
{...restProps}
|
2023-06-25 11:42:31 +08:00
|
|
|
style={mergedStyle}
|
2022-12-25 18:34:42 +08:00
|
|
|
className={spinClassName}
|
|
|
|
aria-live="polite"
|
|
|
|
aria-busy={spinning}
|
|
|
|
>
|
2024-06-03 11:30:27 +08:00
|
|
|
<Indicator
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
indicator={indicator ?? defaultIndicator}
|
|
|
|
percent={mergedPercent}
|
|
|
|
/>
|
2023-10-19 13:47:48 +08:00
|
|
|
{tip && (isNestedPattern || fullscreen) ? (
|
|
|
|
<div className={`${prefixCls}-text`}>{tip}</div>
|
|
|
|
) : null}
|
2022-12-25 18:34:42 +08:00
|
|
|
</div>
|
|
|
|
);
|
2016-07-08 10:14:45 +08:00
|
|
|
|
2022-12-25 18:34:42 +08:00
|
|
|
if (isNestedPattern) {
|
2023-12-14 14:58:53 +08:00
|
|
|
return wrapCSSVar(
|
2022-03-11 15:49:33 +08:00
|
|
|
<div
|
2024-06-03 11:30:27 +08:00
|
|
|
{...restProps}
|
2023-12-14 14:58:53 +08:00
|
|
|
className={classNames(`${prefixCls}-nested-loading`, wrapperClassName, hashId, cssVarCls)}
|
2022-03-11 15:49:33 +08:00
|
|
|
>
|
2022-12-25 18:34:42 +08:00
|
|
|
{spinning && <div key="loading">{spinElement}</div>}
|
|
|
|
<div className={containerClassName} key="container">
|
|
|
|
{children}
|
|
|
|
</div>
|
2023-12-14 14:58:53 +08:00
|
|
|
</div>,
|
2016-03-30 23:02:11 +08:00
|
|
|
);
|
2022-12-25 18:34:42 +08:00
|
|
|
}
|
2024-06-03 11:30:27 +08:00
|
|
|
|
|
|
|
if (fullscreen) {
|
|
|
|
return wrapCSSVar(
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
`${prefixCls}-fullscreen`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-fullscreen-show`]: spinning,
|
|
|
|
},
|
|
|
|
rootClassName,
|
|
|
|
hashId,
|
|
|
|
cssVarCls,
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{spinElement}
|
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
return wrapCSSVar(spinElement);
|
2022-03-25 14:26:09 +08:00
|
|
|
};
|
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
Spin.setDefaultIndicator = (indicator: React.ReactNode) => {
|
2022-03-25 14:26:09 +08:00
|
|
|
defaultIndicator = indicator;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-12-14 14:58:53 +08:00
|
|
|
Spin.displayName = 'Spin';
|
2022-03-25 14:26:09 +08:00
|
|
|
}
|
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
export default Spin;
|