ant-design/components/spin/index.tsx

162 lines
4.2 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Animate from 'rc-animate';
import omit from 'omit.js';
2015-10-31 02:00:03 +08:00
export type SpinSize = 'small' | 'default' | 'large';
export type SpinIndicator = React.ReactElement<any>;
2016-08-10 09:46:56 +08:00
export interface SpinProps {
prefixCls?: string;
className?: string;
spinning?: boolean;
style?: React.CSSProperties;
size?: SpinSize;
2016-08-10 09:46:56 +08:00
tip?: string;
delay?: number;
2017-03-21 17:12:43 +08:00
wrapperClassName?: string;
indicator?: SpinIndicator;
2016-08-10 09:46:56 +08:00
}
export interface SpinState {
spinning?: boolean;
notCssAnimationSupported?: boolean;
}
export default class Spin extends React.Component<SpinProps, SpinState> {
static defaultProps = {
prefixCls: 'ant-spin',
spinning: true,
size: 'default' as SpinSize,
2017-03-21 17:12:43 +08:00
wrapperClassName: '',
2016-07-13 11:14:24 +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,
indicator: PropTypes.node,
2016-07-13 17:22:23 +08:00
};
2016-08-10 09:46:56 +08:00
debounceTimeout: number;
delayTimeout: number;
2016-08-10 09:46:56 +08:00
constructor(props: SpinProps) {
super(props);
const spinning = props.spinning;
this.state = {
2016-05-09 13:10:43 +08:00
spinning,
};
}
2016-05-20 14:26:44 +08:00
isNestedPattern() {
2015-10-31 02:00:03 +08:00
return !!(this.props && this.props.children);
}
componentWillUnmount() {
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
}
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
}
componentWillReceiveProps(nextProps: SpinProps) {
const currentSpinning = this.props.spinning;
const spinning = nextProps.spinning;
const { delay } = this.props;
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
}
if (currentSpinning && !spinning) {
2017-10-09 21:03:00 +08:00
this.debounceTimeout = window.setTimeout(() => this.setState({ spinning }), 200);
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
} else {
if (spinning && delay && !isNaN(Number(delay))) {
2017-06-06 20:38:23 +08:00
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
2017-10-09 21:03:00 +08:00
this.delayTimeout = window.setTimeout(() => this.setState({ spinning }), delay);
} else {
this.setState({ spinning });
}
}
}
renderIndicator() {
const { prefixCls, indicator } = this.props;
const dotClassName = `${prefixCls}-dot`;
if (React.isValidElement(indicator)) {
return React.cloneElement((indicator as SpinIndicator), {
className: classNames((indicator as SpinIndicator).props.className, dotClassName),
});
}
return (
<span className={classNames(dotClassName, `${prefixCls}-dot-spin`)}>
<i />
<i />
<i />
<i />
</span>
);
}
render() {
const { className, size, prefixCls, tip, wrapperClassName, ...restProps } = this.props;
const { spinning } = this.state;
2015-10-27 10:34:05 +08:00
const spinClassName = classNames(prefixCls, {
2016-01-27 12:00:27 +08:00
[`${prefixCls}-sm`]: size === 'small',
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-spinning`]: spinning,
[`${prefixCls}-show-text`]: !!tip,
}, 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',
'delay',
'indicator',
2016-07-08 10:14:45 +08:00
]);
const spinElement = (
<div {...divProps} className={spinClassName} >
{this.renderIndicator()}
{tip ? <div className={`${prefixCls}-text`}>{tip}</div> : null}
</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;
}
const containerClassName = classNames({
[`${prefixCls}-container`]: true,
2016-11-07 17:53:12 +08:00
[`${prefixCls}-blur`]: spinning,
});
2015-10-31 02:00:03 +08:00
return (
<Animate
{...divProps}
component="div"
2017-03-21 17:12:43 +08:00
className={animateClassName}
2017-01-27 14:27:18 +08:00
style={null}
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>
</Animate>
2015-10-31 02:00:03 +08:00
);
}
return spinElement;
2015-10-27 10:10:27 +08:00
}
}