2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2016-11-04 13:50:32 +08:00
|
|
|
import Animate from 'rc-animate';
|
2016-09-10 17:17:55 +08:00
|
|
|
import omit from 'omit.js';
|
2015-10-31 02:00:03 +08:00
|
|
|
|
2018-04-16 14:44:42 +08:00
|
|
|
export type SpinSize = 'small' | 'default' | 'large';
|
2018-02-14 12:30:31 +08:00
|
|
|
export type SpinIndicator = React.ReactElement<any>;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
function renderIndicator(props: SpinProps): React.ReactNode {
|
|
|
|
const { prefixCls, indicator } = props;
|
|
|
|
const dotClassName = `${prefixCls}-dot`;
|
|
|
|
if (React.isValidElement(indicator)) {
|
|
|
|
return React.cloneElement((indicator as SpinIndicator), {
|
|
|
|
className: classNames((indicator as SpinIndicator).props.className, dotClassName),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (React.isValidElement(defaultIndicator)) {
|
|
|
|
return React.cloneElement((defaultIndicator as SpinIndicator), {
|
|
|
|
className: classNames((defaultIndicator as SpinIndicator).props.className, dotClassName),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span className={classNames(dotClassName, `${prefixCls}-dot-spin`)}>
|
|
|
|
<i />
|
|
|
|
<i />
|
|
|
|
<i />
|
|
|
|
<i />
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 = {
|
|
|
|
prefixCls: 'ant-spin',
|
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
|
|
|
|
2016-07-13 17:22:23 +08:00
|
|
|
static propTypes = {
|
2016-08-10 09:46:56 +08:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
className: PropTypes.string,
|
|
|
|
spinning: PropTypes.bool,
|
|
|
|
size: PropTypes.oneOf(['small', 'default', 'large']),
|
2017-03-21 17:12:43 +08:00
|
|
|
wrapperClassName: PropTypes.string,
|
2017-11-04 12:05:25 +08:00
|
|
|
indicator: PropTypes.node,
|
2016-07-13 17:22:23 +08:00
|
|
|
};
|
|
|
|
|
2018-06-06 19:32:30 +08:00
|
|
|
static setDefaultIndicator(indicator: React.ReactNode) {
|
|
|
|
defaultIndicator = indicator;
|
|
|
|
}
|
|
|
|
|
2016-08-10 09:46:56 +08:00
|
|
|
debounceTimeout: number;
|
2017-01-01 01:13:34 +08:00
|
|
|
delayTimeout: number;
|
2016-08-10 09:46:56 +08:00
|
|
|
|
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;
|
2016-05-09 10:49:14 +08:00
|
|
|
this.state = {
|
2018-09-08 23:55:06 +08:00
|
|
|
spinning: spinning && !shouldDelay(spinning, delay),
|
2016-05-09 10:49:14 +08:00
|
|
|
};
|
|
|
|
}
|
2016-05-20 14:26:44 +08:00
|
|
|
|
2015-10-28 19:33:25 +08:00
|
|
|
isNestedPattern() {
|
2015-10-31 02:00:03 +08:00
|
|
|
return !!(this.props && this.props.children);
|
2016-03-20 20:41:34 +08:00
|
|
|
}
|
2015-10-28 19:33:25 +08:00
|
|
|
|
2018-06-08 23:13:47 +08:00
|
|
|
componentDidMount() {
|
|
|
|
const { spinning, delay } = this.props;
|
2018-09-08 22:43:39 +08:00
|
|
|
if (shouldDelay(spinning, delay)) {
|
2018-10-09 00:02:03 +08:00
|
|
|
this.delayTimeout = window.setTimeout(this.delayUpdateSpinning, delay);
|
2018-06-08 23:13:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 20:49:18 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.debounceTimeout) {
|
|
|
|
clearTimeout(this.debounceTimeout);
|
|
|
|
}
|
2017-01-01 01:13:34 +08:00
|
|
|
if (this.delayTimeout) {
|
|
|
|
clearTimeout(this.delayTimeout);
|
|
|
|
}
|
2016-05-11 20:49:18 +08:00
|
|
|
}
|
|
|
|
|
2018-08-19 16:56:46 +08:00
|
|
|
componentDidUpdate() {
|
|
|
|
const currentSpinning = this.state.spinning;
|
|
|
|
const spinning = this.props.spinning;
|
|
|
|
if (currentSpinning === spinning) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-01 01:13:34 +08:00
|
|
|
const { delay } = this.props;
|
|
|
|
|
2016-05-09 10:49:14 +08:00
|
|
|
if (this.debounceTimeout) {
|
|
|
|
clearTimeout(this.debounceTimeout);
|
|
|
|
}
|
2016-07-25 16:31:59 +08:00
|
|
|
if (currentSpinning && !spinning) {
|
2017-10-09 21:03:00 +08:00
|
|
|
this.debounceTimeout = window.setTimeout(() => this.setState({ spinning }), 200);
|
2017-01-01 01:13:34 +08:00
|
|
|
if (this.delayTimeout) {
|
|
|
|
clearTimeout(this.delayTimeout);
|
|
|
|
}
|
2016-05-09 10:49:14 +08:00
|
|
|
} else {
|
2018-10-09 00:02:03 +08:00
|
|
|
if (shouldDelay(spinning, delay)) {
|
2017-06-06 20:38:23 +08:00
|
|
|
if (this.delayTimeout) {
|
|
|
|
clearTimeout(this.delayTimeout);
|
|
|
|
}
|
2018-10-09 00:02:03 +08:00
|
|
|
this.delayTimeout = window.setTimeout(this.delayUpdateSpinning, delay);
|
2017-01-01 01:13:34 +08:00
|
|
|
} else {
|
|
|
|
this.setState({ spinning });
|
|
|
|
}
|
2016-05-09 10:49:14 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-14 12:30:31 +08:00
|
|
|
|
2018-10-09 00:02:03 +08:00
|
|
|
delayUpdateSpinning = () => {
|
|
|
|
const { spinning } = this.props;
|
|
|
|
if (this.state.spinning !== spinning) {
|
|
|
|
this.setState({ spinning });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-09 10:49:14 +08:00
|
|
|
render() {
|
2018-02-14 12:30:31 +08:00
|
|
|
const { className, size, prefixCls, tip, wrapperClassName, ...restProps } = this.props;
|
2018-05-22 13:01:28 +08:00
|
|
|
const { spinning } = this.state;
|
2015-10-27 10:34:05 +08:00
|
|
|
|
2016-11-30 10:20:23 +08:00
|
|
|
const spinClassName = classNames(prefixCls, {
|
2016-01-27 12:00:27 +08:00
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
2016-04-14 15:26:55 +08:00
|
|
|
[`${prefixCls}-spinning`]: spinning,
|
2018-05-22 13:01:28 +08:00
|
|
|
[`${prefixCls}-show-text`]: !!tip,
|
2016-11-30 10:20:23 +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
|
|
|
|
const divProps = omit(restProps, [
|
|
|
|
'spinning',
|
2017-01-01 01:13:34 +08:00
|
|
|
'delay',
|
2018-02-14 12:30:31 +08:00
|
|
|
'indicator',
|
2016-07-08 10:14:45 +08:00
|
|
|
]);
|
|
|
|
|
2016-03-30 23:02:11 +08:00
|
|
|
const spinElement = (
|
2016-11-04 13:50:32 +08:00
|
|
|
<div {...divProps} className={spinClassName} >
|
2018-06-06 19:32:30 +08:00
|
|
|
{renderIndicator(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()) {
|
2017-03-21 17:12:43 +08:00
|
|
|
let animateClassName = prefixCls + '-nested-loading';
|
|
|
|
if (wrapperClassName) {
|
|
|
|
animateClassName += ' ' + wrapperClassName;
|
|
|
|
}
|
2016-11-04 13:50:32 +08:00
|
|
|
const containerClassName = classNames({
|
|
|
|
[`${prefixCls}-container`]: true,
|
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 (
|
2016-11-04 13:50:32 +08:00
|
|
|
<Animate
|
|
|
|
{...divProps}
|
|
|
|
component="div"
|
2017-03-21 17:12:43 +08:00
|
|
|
className={animateClassName}
|
2017-01-27 14:27:18 +08:00
|
|
|
style={null}
|
2016-11-04 13:50:32 +08:00
|
|
|
transitionName="fade"
|
|
|
|
>
|
|
|
|
{spinning && <div key="loading">{spinElement}</div>}
|
|
|
|
<div className={containerClassName} key="container">
|
2015-10-31 02:00:03 +08:00
|
|
|
{this.props.children}
|
|
|
|
</div>
|
2016-11-04 13:50:32 +08:00
|
|
|
</Animate>
|
2015-10-31 02:00:03 +08:00
|
|
|
);
|
|
|
|
}
|
2016-01-07 14:21:29 +08:00
|
|
|
return spinElement;
|
2015-10-27 10:10:27 +08:00
|
|
|
}
|
2016-03-20 20:41:34 +08:00
|
|
|
}
|
2018-06-06 19:32:30 +08:00
|
|
|
|
|
|
|
export default Spin;
|