Skip to content

Commit 00dc849

Browse files
Wxh16144yellowryan
andauthored
同步 master 分支的一些 bugfix 到 antd-v5 分支,以修复 antd@v5 的 bug (#371)
Co-authored-by: Ryan <[email protected]> fix: global duration not work(Also includes some other attributes) (#365)
1 parent f4c10c1 commit 00dc849

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

src/hooks/useNotification.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as React from 'react';
33
import type { NotificationsProps, NotificationsRef } from '../Notifications';
44
import Notifications from '../Notifications';
55
import type { OpenConfig, Placement, StackConfig } from '../interface';
6+
import { useEvent } from 'rc-util';
67

78
const defaultGetContainer = () => document.body;
89

@@ -107,26 +108,29 @@ export default function useNotification(
107108

108109
const [taskQueue, setTaskQueue] = React.useState<Task[]>([]);
109110

110-
// ========================= Refs =========================
111-
const api = React.useMemo<NotificationAPI>(() => {
112-
return {
113-
open: (config) => {
114-
const mergedConfig = mergeConfig(shareConfig, config);
115-
if (mergedConfig.key === null || mergedConfig.key === undefined) {
116-
mergedConfig.key = `rc-notification-${uniqueKey}`;
117-
uniqueKey += 1;
118-
}
111+
const open = useEvent<NotificationAPI['open']>((config) => {
112+
const mergedConfig = mergeConfig(shareConfig, config);
113+
if (mergedConfig.key === null || mergedConfig.key === undefined) {
114+
mergedConfig.key = `rc-notification-${uniqueKey}`;
115+
uniqueKey += 1;
116+
}
119117

120-
setTaskQueue((queue) => [...queue, { type: 'open', config: mergedConfig }]);
121-
},
118+
setTaskQueue((queue) => [...queue, { type: 'open', config: mergedConfig }]);
119+
});
120+
121+
// ========================= Refs =========================
122+
const api = React.useMemo<NotificationAPI>(
123+
() => ({
124+
open: open,
122125
close: (key) => {
123126
setTaskQueue((queue) => [...queue, { type: 'close', key }]);
124127
},
125128
destroy: () => {
126129
setTaskQueue((queue) => [...queue, { type: 'destroy' }]);
127130
},
128-
};
129-
}, []);
131+
}),
132+
[],
133+
);
130134

131135
// ======================= Container ======================
132136
// React 18 should all in effect that we will check container in each render

tests/index.test.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,4 +849,47 @@ describe('Notification.Basic', () => {
849849
expect(document.querySelector('.rc-notification-notice-progress')).toBeFalsy();
850850
});
851851
});
852+
853+
describe('Modifying properties through useState can take effect', () => {
854+
it('should show notification and disappear after 5 seconds', async () => {
855+
const Demo: React.FC = () => {
856+
const [duration, setDuration] = React.useState(0);
857+
const [api, holder] = useNotification({ duration });
858+
859+
return (
860+
<>
861+
<button data-testid="change-duration" onClick={() => setDuration(5)}>
862+
change duration
863+
</button>
864+
<button
865+
data-testid="show-notification"
866+
onClick={() => {
867+
api.open({
868+
content: `Test Notification`,
869+
});
870+
}}
871+
>
872+
show notification
873+
</button>
874+
{holder}
875+
</>
876+
);
877+
};
878+
879+
const { getByTestId } = render(<Demo />);
880+
881+
fireEvent.click(getByTestId('show-notification'));
882+
883+
expect(document.querySelectorAll('.rc-notification-notice').length).toBe(1);
884+
fireEvent.click(getByTestId('change-duration'));
885+
fireEvent.click(getByTestId('show-notification'));
886+
expect(document.querySelectorAll('.rc-notification-notice').length).toBe(2);
887+
888+
act(() => {
889+
vi.advanceTimersByTime(5000);
890+
});
891+
892+
expect(document.querySelectorAll('.rc-notification-notice').length).toBe(1);
893+
});
894+
});
852895
});

0 commit comments

Comments
 (0)