🐛 Fix spin[delay] never render

also simplify the code of spin, a lot

close #14100
This commit is contained in:
afc163 2019-01-04 23:35:01 +08:00 committed by 偏右
parent a9c8874854
commit 9ecb3725a1
2 changed files with 15 additions and 40 deletions

View File

@ -1,6 +1,6 @@
---
order: 0
title:
title:
zh-CN: 基本用法
en-US: basic Usage
---

View File

@ -2,6 +2,7 @@ import * as React from 'react';
import * as PropTypes from 'prop-types';
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';
@ -78,9 +79,6 @@ class Spin extends React.Component<SpinProps, SpinState> {
defaultIndicator = indicator;
}
debounceTimeout: number;
delayTimeout: number;
constructor(props: SpinProps) {
super(props);
@ -88,56 +86,33 @@ class Spin extends React.Component<SpinProps, SpinState> {
this.state = {
spinning: spinning && !shouldDelay(spinning, delay),
};
this.updateSpinning = debounce(this.updateSpinning, delay);
}
isNestedPattern() {
return !!(this.props && this.props.children);
}
componentDidMount() {
this.componentDidUpdate();
componentWillUnmount() {
const updateSpinning: any = this.updateSpinning;
if (updateSpinning && updateSpinning.cancel) {
updateSpinning.cancel();
}
}
componentWillUnmount() {
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
}
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
componentDidMount() {
this.updateSpinning();
}
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) {
this.debounceTimeout = window.setTimeout(() => this.setState({ spinning }), 200);
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
} else {
if (shouldDelay(spinning, delay)) {
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
this.delayTimeout = window.setTimeout(this.delayUpdateSpinning, delay);
} else {
this.setState({ spinning });
}
}
this.updateSpinning();
}
delayUpdateSpinning = () => {
updateSpinning = () => {
const { spinning } = this.props;
if (this.state.spinning !== spinning) {
const { spinning: currentSpinning } = this.state;
if (currentSpinning !== spinning) {
this.setState({ spinning });
}
};