fix(Spin): not close Spin immediately when using delay (#40475)

* fix(Spin): close Spin immediately when using delay

* test(Spin): add delay close test case
pull/40623/head
Alan 2 years ago committed by GitHub
parent 9d4216e565
commit 1dded7fd4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -42,4 +42,15 @@ describe('delay spinning', () => {
unmount();
expect(cancel).toHaveBeenCalled();
});
it('should close immediately', async () => {
jest.useFakeTimers();
const { container, rerender } = render(<Spin spinning delay={500} />);
await waitFakeTimer();
expect(container.querySelector('.ant-spin-spinning')).toBeTruthy();
rerender(<Spin spinning={false} delay={500} />);
expect(container.querySelector('.ant-spin-spinning')).toBeFalsy();
});
});

@ -93,13 +93,17 @@ const Spin: React.FC<SpinClassProps> = (props) => {
);
React.useEffect(() => {
const updateSpinning = debounce(delay, () => {
setSpinning(customSpinning);
});
updateSpinning();
return () => {
updateSpinning?.cancel?.();
};
if (customSpinning) {
const showSpinning = debounce(delay, () => {
setSpinning(true);
});
showSpinning();
return () => {
showSpinning?.cancel?.();
};
}
setSpinning(false);
}, [delay, customSpinning]);
const isNestedPattern = React.useMemo<boolean>(() => typeof children !== 'undefined', [children]);

Loading…
Cancel
Save