From 1dded7fd4b27235a81ec8cc6633fecae2c2b122d Mon Sep 17 00:00:00 2001
From: Alan <49978973+3Alan@users.noreply.github.com>
Date: Tue, 7 Feb 2023 23:46:10 +0800
Subject: [PATCH] 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
---
components/spin/__tests__/delay.test.tsx | 11 +++++++++++
components/spin/index.tsx | 18 +++++++++++-------
2 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/components/spin/__tests__/delay.test.tsx b/components/spin/__tests__/delay.test.tsx
index eb67695c4f..154ce8c28c 100644
--- a/components/spin/__tests__/delay.test.tsx
+++ b/components/spin/__tests__/delay.test.tsx
@@ -42,4 +42,15 @@ describe('delay spinning', () => {
unmount();
expect(cancel).toHaveBeenCalled();
});
+
+ it('should close immediately', async () => {
+ jest.useFakeTimers();
+ const { container, rerender } = render();
+
+ await waitFakeTimer();
+ expect(container.querySelector('.ant-spin-spinning')).toBeTruthy();
+
+ rerender();
+ expect(container.querySelector('.ant-spin-spinning')).toBeFalsy();
+ });
});
diff --git a/components/spin/index.tsx b/components/spin/index.tsx
index 6ad1fbc4be..8527a9a9b9 100644
--- a/components/spin/index.tsx
+++ b/components/spin/index.tsx
@@ -93,13 +93,17 @@ const Spin: React.FC = (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(() => typeof children !== 'undefined', [children]);