ant-design/components/spin/index.jsx

98 lines
2.6 KiB
React
Raw Normal View History

2015-10-27 10:10:27 +08:00
import React from 'react';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
2016-06-01 12:08:08 +08:00
import isCssAnimationSupported from '../_util/isCssAnimationSupported';
import warning from 'warning';
2015-10-31 02:00:03 +08:00
export default class Spin extends React.Component {
static defaultProps = {
prefixCls: 'ant-spin',
spinning: true,
}
constructor(props) {
super(props);
2016-05-09 15:17:09 +08:00
const spinning = this.getSpinning(props);
this.state = {
2016-05-09 13:10:43 +08:00
spinning,
};
}
2016-05-20 14:26:44 +08:00
static propTypes = {
className: React.PropTypes.string,
size: React.PropTypes.oneOf(['small', 'default', 'large']),
}
isNestedPattern() {
2015-10-31 02:00:03 +08:00
return !!(this.props && this.props.children);
}
componentDidMount() {
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()) {
// Show text in IE8/9
findDOMNode(this).className += ` ${this.props.prefixCls}-show-text`;
}
}
2016-05-09 16:20:46 +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
componentWillReceiveProps(nextProps) {
2016-05-09 15:17:09 +08:00
const spinning = this.getSpinning(nextProps);
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
}
2016-05-09 13:10:43 +08:00
if (spinning) {
2016-05-09 16:11:54 +08:00
this.debounceTimeout = setTimeout(() => this.setState({ spinning }), 250);
} else {
this.setState({ spinning });
}
}
2016-05-09 16:20:46 +08:00
render() {
2016-05-20 14:25:02 +08:00
const { className, size, prefixCls, tip, ...restProps } = this.props;
const { spinning } = this.state;
2015-10-27 10:34:05 +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',
[`${prefixCls}-spinning`]: spinning,
[`${prefixCls}-show-text`]: !!this.props.tip,
2016-05-20 14:25:02 +08:00
[className]: !!className,
2015-10-27 10:10:27 +08:00
});
const spinElement = (
2016-05-20 14:25:02 +08:00
<div {...restProps} className={spinClassName}>
2016-06-18 15:43:42 +08:00
<span className={`${prefixCls}-dot`} />
<div className={`${prefixCls}-text`}>{tip || '加载中...'}</div>
</div>
);
2015-10-31 02:00:03 +08:00
if (this.isNestedPattern()) {
return (
2016-05-20 14:25:02 +08:00
<div {...restProps} className={spinning ? (`${prefixCls}-nested-loading`) : ''}>
2015-10-31 02:00:03 +08:00
{spinElement}
<div className={`${prefixCls}-container`}>
2015-10-31 02:00:03 +08:00
{this.props.children}
</div>
</div>
);
}
return spinElement;
2015-10-27 10:10:27 +08:00
}
}