2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2016-09-10 17:17:55 +08:00
|
|
|
import omit from 'omit.js';
|
2019-01-04 23:35:01 +08:00
|
|
|
import debounce from 'lodash/debounce';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2018-12-22 21:21:42 +08:00
|
|
|
import { tuple } from '../_util/type';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { isValidElement, cloneElement } from '../_util/reactNode';
|
2015-10-31 02:00:03 +08:00
|
|
|
|
2018-12-22 21:21:42 +08:00
|
|
|
const SpinSizes = tuple('small', 'default', 'large');
|
2019-11-27 09:40:47 +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 {
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
spinning?: boolean;
|
2017-11-22 14:46:40 +08:00
|
|
|
style?: React.CSSProperties;
|
2018-04-16 10:58:17 +08:00
|
|
|
size?: SpinSize;
|
2016-08-10 09:46:56 +08:00
|
|
|
tip?: string;
|
2017-01-01 01:13:34 +08:00
|
|
|
delay?: number;
|
2017-03-21 17:12:43 +08:00
|
|
|
wrapperClassName?: string;
|
2018-02-14 12:30:31 +08:00
|
|
|
indicator?: SpinIndicator;
|
2016-08-10 09:46:56 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 14:53:25 +08:00
|
|
|
export interface SpinState {
|
|
|
|
spinning?: boolean;
|
|
|
|
notCssAnimationSupported?: boolean;
|
|
|
|
}
|
|
|
|
|
2018-06-06 19:32:30 +08:00
|
|
|
// Render indicator
|
|
|
|
let defaultIndicator: React.ReactNode = null;
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
function renderIndicator(prefixCls: string, props: SpinProps): React.ReactNode {
|
|
|
|
const { indicator } = props;
|
2018-06-06 19:32:30 +08:00
|
|
|
const dotClassName = `${prefixCls}-dot`;
|
2019-11-27 09:40:47 +08:00
|
|
|
|
|
|
|
// should not be render default indicator when indicator value is null
|
|
|
|
if (indicator === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-14 20:54:49 +08:00
|
|
|
if (isValidElement(indicator)) {
|
|
|
|
return cloneElement(indicator, {
|
2019-06-16 20:51:47 +08:00
|
|
|
className: classNames(indicator.props.className, dotClassName),
|
2018-06-06 19:32:30 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-14 20:54:49 +08:00
|
|
|
if (isValidElement(defaultIndicator)) {
|
|
|
|
return cloneElement(defaultIndicator as SpinIndicator, {
|
2018-06-06 19:32:30 +08:00
|
|
|
className: classNames((defaultIndicator as SpinIndicator).props.className, dotClassName),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span className={classNames(dotClassName, `${prefixCls}-dot-spin`)}>
|
2019-03-28 12:20:12 +08:00
|
|
|
<i className={`${prefixCls}-dot-item`} />
|
|
|
|
<i className={`${prefixCls}-dot-item`} />
|
|
|
|
<i className={`${prefixCls}-dot-item`} />
|
|
|
|
<i className={`${prefixCls}-dot-item`} />
|
2018-12-07 20:02:01 +08:00
|
|
|
</span>
|
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));
|
|
|
|
}
|
|
|
|
|
2018-06-06 19:32:30 +08:00
|
|
|
class Spin extends React.Component<SpinProps, SpinState> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
2016-04-14 15:26:55 +08:00
|
|
|
spinning: true,
|
2018-04-16 10:58:17 +08:00
|
|
|
size: 'default' as SpinSize,
|
2017-03-21 17:12:43 +08:00
|
|
|
wrapperClassName: '',
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2018-06-06 19:32:30 +08:00
|
|
|
static setDefaultIndicator(indicator: React.ReactNode) {
|
|
|
|
defaultIndicator = indicator;
|
|
|
|
}
|
|
|
|
|
2019-01-07 10:25:23 +08:00
|
|
|
originalUpdateSpinning: () => void;
|
|
|
|
|
2017-11-21 14:53:25 +08:00
|
|
|
constructor(props: SpinProps) {
|
2016-05-09 10:49:14 +08:00
|
|
|
super(props);
|
2018-09-08 22:43:39 +08:00
|
|
|
|
|
|
|
const { spinning, delay } = props;
|
2019-01-04 23:51:07 +08:00
|
|
|
const shouldBeDelayed = shouldDelay(spinning, delay);
|
2016-05-09 10:49:14 +08:00
|
|
|
this.state = {
|
2019-01-04 23:51:07 +08:00
|
|
|
spinning: spinning && !shouldBeDelayed,
|
2016-05-09 10:49:14 +08:00
|
|
|
};
|
2019-01-07 10:25:23 +08:00
|
|
|
this.originalUpdateSpinning = this.updateSpinning;
|
|
|
|
this.debouncifyUpdateSpinning(props);
|
2016-05-09 10:49:14 +08:00
|
|
|
}
|
2016-05-20 14:26:44 +08:00
|
|
|
|
2019-01-04 23:35:01 +08:00
|
|
|
componentDidMount() {
|
|
|
|
this.updateSpinning();
|
|
|
|
}
|
2017-01-01 01:13:34 +08:00
|
|
|
|
2019-01-04 23:35:01 +08:00
|
|
|
componentDidUpdate() {
|
2019-01-07 10:25:23 +08:00
|
|
|
this.debouncifyUpdateSpinning();
|
2019-01-04 23:35:01 +08:00
|
|
|
this.updateSpinning();
|
2016-05-09 10:49:14 +08:00
|
|
|
}
|
2018-02-14 12:30:31 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.cancelExistingSpin();
|
|
|
|
}
|
|
|
|
|
2019-01-07 10:25:23 +08:00
|
|
|
debouncifyUpdateSpinning = (props?: SpinProps) => {
|
|
|
|
const { delay } = props || this.props;
|
|
|
|
if (delay) {
|
2019-04-15 14:30:04 +08:00
|
|
|
this.cancelExistingSpin();
|
2019-01-07 10:25:23 +08:00
|
|
|
this.updateSpinning = debounce(this.originalUpdateSpinning, delay);
|
|
|
|
}
|
2019-01-26 11:55:53 +08:00
|
|
|
};
|
2019-01-07 10:25:23 +08:00
|
|
|
|
2019-01-04 23:35:01 +08:00
|
|
|
updateSpinning = () => {
|
2018-10-09 00:02:03 +08:00
|
|
|
const { spinning } = this.props;
|
2019-01-04 23:35:01 +08:00
|
|
|
const { spinning: currentSpinning } = this.state;
|
|
|
|
if (currentSpinning !== spinning) {
|
2018-10-09 00:02:03 +08:00
|
|
|
this.setState({ spinning });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
cancelExistingSpin() {
|
|
|
|
const { updateSpinning } = this;
|
|
|
|
if (updateSpinning && (updateSpinning as any).cancel) {
|
|
|
|
(updateSpinning as any).cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isNestedPattern() {
|
|
|
|
return !!(this.props && this.props.children);
|
|
|
|
}
|
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
renderSpin = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2018-12-05 19:12:18 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2018-12-07 20:02:01 +08:00
|
|
|
className,
|
|
|
|
size,
|
|
|
|
tip,
|
|
|
|
wrapperClassName,
|
2018-12-18 19:18:08 +08:00
|
|
|
style,
|
2018-12-05 19:12:18 +08:00
|
|
|
...restProps
|
|
|
|
} = this.props;
|
2018-05-22 13:01:28 +08:00
|
|
|
const { spinning } = this.state;
|
2015-10-27 10:34:05 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('spin', customizePrefixCls);
|
2018-12-07 20:02:01 +08:00
|
|
|
const spinClassName = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-spinning`]: spinning,
|
|
|
|
[`${prefixCls}-show-text`]: !!tip,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2018-12-07 20:02:01 +08:00
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2015-10-27 10:10:27 +08:00
|
|
|
|
2016-07-08 10:14:45 +08:00
|
|
|
// fix https://fb.me/react-unknown-prop
|
2018-12-07 20:02:01 +08:00
|
|
|
const divProps = omit(restProps, ['spinning', 'delay', 'indicator']);
|
2016-07-08 10:14:45 +08:00
|
|
|
|
2016-03-30 23:02:11 +08:00
|
|
|
const spinElement = (
|
2018-12-18 11:02:09 +08:00
|
|
|
<div {...divProps} style={style} className={spinClassName}>
|
2018-12-05 19:12:18 +08:00
|
|
|
{renderIndicator(prefixCls, this.props)}
|
2016-08-23 16:17:32 +08:00
|
|
|
{tip ? <div className={`${prefixCls}-text`}>{tip}</div> : null}
|
2016-03-30 23:02:11 +08:00
|
|
|
</div>
|
|
|
|
);
|
2015-10-31 02:00:03 +08:00
|
|
|
if (this.isNestedPattern()) {
|
2018-12-17 23:27:24 +08:00
|
|
|
const containerClassName = classNames(`${prefixCls}-container`, {
|
2016-11-07 17:53:12 +08:00
|
|
|
[`${prefixCls}-blur`]: spinning,
|
2016-11-04 13:50:32 +08:00
|
|
|
});
|
2015-10-31 02:00:03 +08:00
|
|
|
return (
|
2018-12-17 23:27:24 +08:00
|
|
|
<div {...divProps} className={classNames(`${prefixCls}-nested-loading`, wrapperClassName)}>
|
2016-11-04 13:50:32 +08:00
|
|
|
{spinning && <div key="loading">{spinElement}</div>}
|
|
|
|
<div className={containerClassName} key="container">
|
2015-10-31 02:00:03 +08:00
|
|
|
{this.props.children}
|
|
|
|
</div>
|
2018-12-17 23:27:24 +08:00
|
|
|
</div>
|
2015-10-31 02:00:03 +08:00
|
|
|
);
|
|
|
|
}
|
2016-01-07 14:21:29 +08:00
|
|
|
return spinElement;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderSpin}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-03-20 20:41:34 +08:00
|
|
|
}
|
2018-06-06 19:32:30 +08:00
|
|
|
|
|
|
|
export default Spin;
|