ant-design/components/spin/index.tsx

187 lines
4.9 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import classNames from 'classnames';
import omit from 'omit.js';
import debounce from 'lodash/debounce';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import { tuple } from '../_util/type';
2015-10-31 02:00:03 +08:00
const SpinSizes = tuple('small', 'default', 'large');
export type SpinSize = typeof SpinSizes[number];
export type SpinIndicator = React.ReactElement<HTMLElement>;
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(prefixCls: string, props: SpinProps): React.ReactNode {
const { indicator } = props;
const dotClassName = `${prefixCls}-dot`;
// should not be render default indicator when indicator value is null
if (indicator === null) {
return null;
}
if (React.isValidElement(indicator)) {
return React.cloneElement(indicator, {
className: classNames(indicator.props.className, dotClassName),
});
}
if (React.isValidElement(defaultIndicator)) {
2018-12-07 20:02:01 +08:00
return React.cloneElement(defaultIndicator as SpinIndicator, {
className: classNames((defaultIndicator as SpinIndicator).props.className, dotClassName),
});
}
return (
<span className={classNames(dotClassName, `${prefixCls}-dot-spin`)}>
<i className={`${prefixCls}-dot-item`} />
<i className={`${prefixCls}-dot-item`} />
<i className={`${prefixCls}-dot-item`} />
<i className={`${prefixCls}-dot-item`} />
2018-12-07 20:02:01 +08:00
</span>
);
}
function shouldDelay(spinning?: boolean, delay?: number): boolean {
return !!spinning && !!delay && !isNaN(Number(delay));
}
class Spin extends React.Component<SpinProps, SpinState> {
static defaultProps = {
spinning: true,
size: 'default' as SpinSize,
2017-03-21 17:12:43 +08:00
wrapperClassName: '',
2016-07-13 11:14:24 +08:00
};
static setDefaultIndicator(indicator: React.ReactNode) {
defaultIndicator = indicator;
}
originalUpdateSpinning: () => void;
constructor(props: SpinProps) {
super(props);
const { spinning, delay } = props;
2019-01-04 23:51:07 +08:00
const shouldBeDelayed = shouldDelay(spinning, delay);
this.state = {
2019-01-04 23:51:07 +08:00
spinning: spinning && !shouldBeDelayed,
};
this.originalUpdateSpinning = this.updateSpinning;
this.debouncifyUpdateSpinning(props);
}
2016-05-20 14:26:44 +08:00
componentDidMount() {
this.updateSpinning();
}
componentDidUpdate() {
this.debouncifyUpdateSpinning();
this.updateSpinning();
}
2019-08-05 18:38:10 +08:00
componentWillUnmount() {
this.cancelExistingSpin();
}
debouncifyUpdateSpinning = (props?: SpinProps) => {
const { delay } = props || this.props;
if (delay) {
this.cancelExistingSpin();
this.updateSpinning = debounce(this.originalUpdateSpinning, delay);
}
};
updateSpinning = () => {
const { spinning } = this.props;
const { spinning: currentSpinning } = this.state;
if (currentSpinning !== spinning) {
this.setState({ spinning });
}
};
2019-08-05 18:38:10 +08:00
cancelExistingSpin() {
const { updateSpinning } = this;
if (updateSpinning && (updateSpinning as any).cancel) {
(updateSpinning as any).cancel();
}
}
isNestedPattern() {
return !!(this.props && this.props.children);
}
renderSpin = ({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
2018-12-07 20:02:01 +08:00
className,
size,
tip,
wrapperClassName,
2018-12-18 19:18:08 +08:00
style,
...restProps
} = this.props;
const { spinning } = this.state;
2015-10-27 10:34:05 +08:00
const prefixCls = getPrefixCls('spin', customizePrefixCls);
2018-12-07 20:02:01 +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 20:02:01 +08:00
const divProps = omit(restProps, ['spinning', 'delay', 'indicator']);
2016-07-08 10:14:45 +08:00
const spinElement = (
2018-12-18 11:02:09 +08:00
<div {...divProps} style={style} className={spinClassName}>
{renderIndicator(prefixCls, 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;
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderSpin}</ConfigConsumer>;
}
}
export default Spin;