Browse Source

🐛 Fix spin[delay] never render

also simplify the code of spin, a lot

close #14100
pull/14264/head
afc163 6 years ago
committed by 偏右
parent
commit
9ecb3725a1
  1. 2
      components/spin/demo/basic.md
  2. 53
      components/spin/index.tsx

2
components/spin/demo/basic.md

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

53
components/spin/index.tsx

@ -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() {
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
}
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
const updateSpinning: any = this.updateSpinning;
if (updateSpinning && updateSpinning.cancel) {
updateSpinning.cancel();
}
}
componentDidUpdate() {
const currentSpinning = this.state.spinning;
const spinning = this.props.spinning;
if (currentSpinning === spinning) {
return;
}
const { delay } = this.props;
componentDidMount() {
this.updateSpinning();
}
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 });
}
}
componentDidUpdate() {
this.updateSpinning();
}
delayUpdateSpinning = () => {
updateSpinning = () => {
const { spinning } = this.props;
if (this.state.spinning !== spinning) {
const { spinning: currentSpinning } = this.state;
if (currentSpinning !== spinning) {
this.setState({ spinning });
}
};

Loading…
Cancel
Save