Skip to content

Commit 5cc00b4

Browse files
kiner-tangtangwenhui
and
tangwenhui
authored
fix: should work properly when the key is zero (#253)
Co-authored-by: tangwenhui <[email protected]>
1 parent 3c1dab4 commit 5cc00b4

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

docs/examples/simple.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,29 @@ function customCloseIconFn() {
120120
});
121121
}
122122

123+
124+
function callWithKey0() {
125+
const key = 0;
126+
notification.notice({
127+
duration: 3,
128+
content: <span>first call with key 0</span>,
129+
onClose() {
130+
console.log('simple close');
131+
},
132+
key
133+
});
134+
setTimeout(() => {
135+
notification.notice({
136+
duration: 3,
137+
content: <span>second call with key 0</span>,
138+
onClose() {
139+
console.log('simple close');
140+
},
141+
key
142+
});
143+
}, 2000);
144+
}
145+
123146
const Demo = () => (
124147
<div>
125148
<button type="button" onClick={simpleFn}>
@@ -140,6 +163,9 @@ const Demo = () => (
140163
<button type="button" onClick={customCloseIconFn}>
141164
custom close icon
142165
</button>
166+
<button type="button" onClick={callWithKey0}>
167+
call with key 0
168+
</button>
143169
</div>
144170
);
145171

src/Notification.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class Notification extends Component<NotificationProps, NotificationState> {
9696
}
9797

9898
add = (originNotice: NoticeContent, holderCallback?: HolderReadyCallback) => {
99-
const key = originNotice.key || getUuid();
99+
const key = originNotice.key ?? getUuid();
100100
const notice: NoticeContent & { key: React.Key; userPassKey?: React.Key } = {
101101
...originNotice,
102102
key,
@@ -139,7 +139,7 @@ class Notification extends Component<NotificationProps, NotificationState> {
139139
remove = (removeKey: React.Key) => {
140140
this.setState(({ notices }: NotificationState) => ({
141141
notices: notices.filter(({ notice: { key, userPassKey } }) => {
142-
const mergedKey = userPassKey || key;
142+
const mergedKey = userPassKey ?? key;
143143
return mergedKey !== removeKey;
144144
}),
145145
}));

tests/index.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -573,4 +573,42 @@ describe('Notification.Basic', () => {
573573
}, 10);
574574
});
575575
});
576+
577+
it('should work properly when the key is zero', (done) => {
578+
let container;
579+
580+
let notificationInstance;
581+
const key = 0;
582+
Notification.newInstance(
583+
{
584+
TEST_RENDER: (node) => {
585+
({ container } = render(<div>{node}</div>));
586+
},
587+
},
588+
(notification) => {
589+
notificationInstance = notification;
590+
591+
notificationInstance.notice({
592+
content: <span className="content first">bamboo</span>,
593+
duration: 0.3,
594+
key,
595+
});
596+
597+
setTimeout(() => {
598+
notificationInstance.notice({
599+
content: <span className="content second">bamboo</span>,
600+
duration: 0.3,
601+
key,
602+
});
603+
setTimeout(() => {
604+
expect(container.querySelectorAll('.content')).toHaveLength(1);
605+
expect(container.querySelectorAll('.first')).toHaveLength(0);
606+
expect(container.querySelectorAll('.second')).toHaveLength(1);
607+
notification.destroy();
608+
done();
609+
}, 10);
610+
}, 200);
611+
},
612+
);
613+
});
576614
});

0 commit comments

Comments
 (0)