2016-07-07 20:25:03 +08:00
|
|
|
import * as React from 'react';
|
2016-08-10 09:46:56 +08:00
|
|
|
import { PropTypes } from 'react';
|
2016-03-30 23:02:11 +08:00
|
|
|
import { findDOMNode } from 'react-dom';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2016-06-01 12:08:08 +08:00
|
|
|
import isCssAnimationSupported from '../_util/isCssAnimationSupported';
|
2016-04-15 11:52:15 +08:00
|
|
|
import warning from 'warning';
|
2016-06-22 13:18:43 +08:00
|
|
|
import splitObject from '../_util/splitObject';
|
2016-07-08 10:14:45 +08:00
|
|
|
import omit from 'object.omit';
|
2015-10-31 02:00:03 +08:00
|
|
|
|
2016-08-10 09:46:56 +08:00
|
|
|
export interface SpinProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
spinning?: boolean;
|
|
|
|
size?: 'small' | 'default' | 'large';
|
|
|
|
tip?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Spin extends React.Component<SpinProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-spin',
|
2016-04-14 15:26:55 +08:00
|
|
|
spinning: true,
|
2016-08-10 09:46:56 +08:00
|
|
|
size: 'default',
|
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']),
|
2016-07-13 17:22:23 +08:00
|
|
|
};
|
|
|
|
|
2016-08-10 09:46:56 +08:00
|
|
|
debounceTimeout: number;
|
|
|
|
|
2016-05-09 10:49:14 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-05-09 15:17:09 +08:00
|
|
|
const spinning = this.getSpinning(props);
|
2016-05-09 10:49:14 +08:00
|
|
|
this.state = {
|
2016-05-09 13:10:43 +08:00
|
|
|
spinning,
|
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
|
|
|
|
2016-03-30 23:02:11 +08:00
|
|
|
componentDidMount() {
|
2016-04-15 11:52:15 +08:00
|
|
|
warning(!('spining' in this.props), '`spining` property of Popover is a spell mistake, use `spinning` instead.');
|
2016-05-26 12:52:07 +08:00
|
|
|
if (!isCssAnimationSupported()) {
|
2016-03-30 23:02:11 +08:00
|
|
|
// Show text in IE8/9
|
|
|
|
findDOMNode(this).className += ` ${this.props.prefixCls}-show-text`;
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 16:20:46 +08:00
|
|
|
|
2016-05-11 20:49:18 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.debounceTimeout) {
|
|
|
|
clearTimeout(this.debounceTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:17:09 +08:00
|
|
|
getSpinning(props) {
|
|
|
|
// Backwards support
|
2016-05-09 16:20:46 +08:00
|
|
|
if ('spining' in props) {
|
|
|
|
warning(false, '`spining` property of Spin is a spell mistake, use `spinning` instead.');
|
|
|
|
return props.spining;
|
2016-05-09 15:17:09 +08:00
|
|
|
}
|
2016-05-09 16:20:46 +08:00
|
|
|
return props.spinning;
|
2016-05-09 15:17:09 +08:00
|
|
|
}
|
2016-05-09 16:20:46 +08:00
|
|
|
|
2016-05-09 10:49:14 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2016-07-25 16:31:59 +08:00
|
|
|
const currentSpinning = this.getSpinning(this.props);
|
2016-05-09 15:17:09 +08:00
|
|
|
const spinning = this.getSpinning(nextProps);
|
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) {
|
|
|
|
this.debounceTimeout = setTimeout(() => this.setState({ spinning }), 500);
|
2016-05-09 10:49:14 +08:00
|
|
|
} else {
|
|
|
|
this.setState({ spinning });
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 16:20:46 +08:00
|
|
|
|
2016-05-09 10:49:14 +08:00
|
|
|
render() {
|
2016-06-22 13:18:43 +08:00
|
|
|
const [{
|
2016-07-13 17:22:23 +08:00
|
|
|
className, size, prefixCls, tip,
|
2016-07-13 11:14:24 +08:00
|
|
|
}, restProps] = splitObject(this.props,
|
2016-06-22 13:18:43 +08:00
|
|
|
['className', 'size', 'prefixCls', 'tip']);
|
2016-05-09 10:49:14 +08:00
|
|
|
const { spinning } = this.state;
|
2015-10-27 10:34:05 +08:00
|
|
|
|
2016-04-15 11:52:15 +08:00
|
|
|
const spinClassName = classNames({
|
2015-11-06 23:49:37 +08:00
|
|
|
[prefixCls]: true,
|
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,
|
2016-03-30 23:02:11 +08:00
|
|
|
[`${prefixCls}-show-text`]: !!this.props.tip,
|
2016-05-20 14:25:02 +08:00
|
|
|
[className]: !!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',
|
|
|
|
]);
|
|
|
|
|
2016-03-30 23:02:11 +08:00
|
|
|
const spinElement = (
|
2016-07-08 10:14:45 +08:00
|
|
|
<div {...divProps} className={spinClassName}>
|
2016-06-18 15:43:42 +08:00
|
|
|
<span className={`${prefixCls}-dot`} />
|
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-28 19:33:25 +08:00
|
|
|
|
2015-10-31 02:00:03 +08:00
|
|
|
if (this.isNestedPattern()) {
|
|
|
|
return (
|
2016-07-08 10:14:45 +08:00
|
|
|
<div {...divProps} className={spinning ? (`${prefixCls}-nested-loading`) : ''}>
|
2015-10-31 02:00:03 +08:00
|
|
|
{spinElement}
|
2016-02-17 18:04:42 +08:00
|
|
|
<div className={`${prefixCls}-container`}>
|
2015-10-31 02:00:03 +08:00
|
|
|
{this.props.children}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
}
|