fix: 解决同级(same parent node) spinner 的 show 属性不全为 true 时报错 (#5538)

This commit is contained in:
meerkat 2022-10-11 21:24:27 +08:00 committed by GitHub
parent 9067c9f30f
commit 3ebe43c2be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -56,7 +56,9 @@ const SpinnerSharedStore = types
self.spinnerContainers.add(spinnerContainer);
},
remove: (spinnerContainer: HTMLElement) => {
self.spinnerContainers.delete(spinnerContainer);
if (self.spinnerContainers.has(spinnerContainer)) {
self.spinnerContainers.delete(spinnerContainer);
}
},
/**
* Spinner loading
@ -91,7 +93,10 @@ const SpinnerSharedStore = types
const store = SpinnerSharedStore.create({});
export class Spinner extends React.Component<SpinnerProps> {
export class Spinner extends React.Component<
SpinnerProps,
{spinning: boolean; showMark: boolean}
> {
static defaultProps = {
show: true,
className: '',
@ -111,6 +116,14 @@ export class Spinner extends React.Component<SpinnerProps> {
parent: HTMLElement | null = null;
/**
* same parent node spinner show true
* spinning; push: + 1 ; remove: -1;
* > 0 : spinning = checkLoading
* = 0 : spinning = false; cannot remove
*/
count: number = 0;
spinnerRef = (dom: HTMLElement) => {
if (dom) {
this.parent = dom.parentNode as HTMLElement;
@ -123,9 +136,11 @@ export class Spinner extends React.Component<SpinnerProps> {
componentDidUpdate() {
if (this.parent) {
if (this.props.show) {
this.count++;
store.push(this.parent);
} else if (this.state.spinning) {
} else if (this.state.spinning && this.count > 0) {
store.remove(this.parent);
this.count--;
}
}
}
@ -144,7 +159,7 @@ export class Spinner extends React.Component<SpinnerProps> {
() => store.spinnerContainers.size,
() => {
this.setState({
spinning: store.checkLoading(this.parent)
spinning: store.checkLoading(this.parent) && this.count > 0
});
}
);