ant-design/components/spin/index.tsx

181 lines
4.7 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
import classNames from 'classnames';
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;
}
// 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)) {
2018-12-07 16:17:45 +08:00
return React.cloneElement(indicator as SpinIndicator, {
className: classNames((indicator as SpinIndicator).props.className, dotClassName),
});
}
if (React.isValidElement(defaultIndicator)) {
2018-12-07 16:17:45 +08:00
return React.cloneElement(defaultIndicator as SpinIndicator, {
className: classNames((defaultIndicator as SpinIndicator).props.className, dotClassName),
});
}
return (
<span className={classNames(dotClassName, `${prefixCls}-dot-spin`)}>
2018-12-07 16:17:45 +08:00
<i />
<i />
<i />
<i />
</span>
);
}
function shouldDelay(spinning?: boolean, delay?: number): boolean {
return !!spinning && !!delay && !isNaN(Number(delay));
}
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
};
static setDefaultIndicator(indicator: React.ReactNode) {
defaultIndicator = indicator;
}
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, delay } = props;
this.state = {
2018-09-08 23:55:06 +08:00
spinning: spinning && !shouldDelay(spinning, delay),
};
}
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);
}
}
2018-08-19 16:56:46 +08:00
componentDidUpdate() {
const currentSpinning = this.state.spinning;
const spinning = this.props.spinning;
if (currentSpinning === spinning) {
return;
}
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 (shouldDelay(spinning, delay)) {
2017-06-06 20:38:23 +08:00
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
this.delayTimeout = window.setTimeout(this.delayUpdateSpinning, delay);
} else {
this.setState({ spinning });
}
}
}
delayUpdateSpinning = () => {
const { spinning } = this.props;
if (this.state.spinning !== spinning) {
this.setState({ spinning });
}
};
render() {
const { className, size, prefixCls, tip, wrapperClassName, ...restProps } = this.props;
const { spinning } = this.state;
2015-10-27 10:34:05 +08:00
2018-12-07 16:17:45 +08:00
const spinClassName = classNames(
prefixCls,
{
[`${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
2018-12-07 16:17:45 +08:00
const divProps = omit(restProps, ['spinning', 'delay', 'indicator']);
2016-07-08 10:14:45 +08:00
const spinElement = (
2018-12-07 16:17:45 +08:00
<div {...divProps} className={spinClassName}>
{renderIndicator(this.props)}
{tip ? <div className={`${prefixCls}-text`}>{tip}</div> : null}
</div>
);
2015-10-31 02:00:03 +08:00
if (this.isNestedPattern()) {
const containerClassName = classNames(`${prefixCls}-container`, {
2016-11-07 17:53:12 +08:00
[`${prefixCls}-blur`]: spinning,
});
2015-10-31 02:00:03 +08:00
return (
<div {...divProps} className={classNames(`${prefixCls}-nested-loading`, wrapperClassName)}>
{spinning && <div key="loading">{spinElement}</div>}
<div className={containerClassName} key="container">
2015-10-31 02:00:03 +08:00
{this.props.children}
</div>
</div>
2015-10-31 02:00:03 +08:00
);
}
return spinElement;
2015-10-27 10:10:27 +08:00
}
}
export default Spin;