Skip to content

Commit

Permalink
fix: should work properly when the key is zero (#253)
Browse files Browse the repository at this point in the history
Co-authored-by: tangwenhui <[email protected]>
  • Loading branch information
kiner-tang and tangwenhui authored Nov 1, 2022
1 parent 3c1dab4 commit 5cc00b4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
26 changes: 26 additions & 0 deletions docs/examples/simple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ function customCloseIconFn() {
});
}


function callWithKey0() {
const key = 0;
notification.notice({
duration: 3,
content: <span>first call with key 0</span>,
onClose() {
console.log('simple close');
},
key
});
setTimeout(() => {
notification.notice({
duration: 3,
content: <span>second call with key 0</span>,
onClose() {
console.log('simple close');
},
key
});
}, 2000);
}

const Demo = () => (
<div>
<button type="button" onClick={simpleFn}>
Expand All @@ -140,6 +163,9 @@ const Demo = () => (
<button type="button" onClick={customCloseIconFn}>
custom close icon
</button>
<button type="button" onClick={callWithKey0}>
call with key 0
</button>
</div>
);

Expand Down
4 changes: 2 additions & 2 deletions src/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Notification extends Component<NotificationProps, NotificationState> {
}

add = (originNotice: NoticeContent, holderCallback?: HolderReadyCallback) => {
const key = originNotice.key || getUuid();
const key = originNotice.key ?? getUuid();
const notice: NoticeContent & { key: React.Key; userPassKey?: React.Key } = {
...originNotice,
key,
Expand Down Expand Up @@ -139,7 +139,7 @@ class Notification extends Component<NotificationProps, NotificationState> {
remove = (removeKey: React.Key) => {
this.setState(({ notices }: NotificationState) => ({
notices: notices.filter(({ notice: { key, userPassKey } }) => {
const mergedKey = userPassKey || key;
const mergedKey = userPassKey ?? key;
return mergedKey !== removeKey;
}),
}));
Expand Down
38 changes: 38 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,4 +573,42 @@ describe('Notification.Basic', () => {
}, 10);
});
});

it('should work properly when the key is zero', (done) => {
let container;

let notificationInstance;
const key = 0;
Notification.newInstance(
{
TEST_RENDER: (node) => {
({ container } = render(<div>{node}</div>));
},
},
(notification) => {
notificationInstance = notification;

notificationInstance.notice({
content: <span className="content first">bamboo</span>,
duration: 0.3,
key,
});

setTimeout(() => {
notificationInstance.notice({
content: <span className="content second">bamboo</span>,
duration: 0.3,
key,
});
setTimeout(() => {
expect(container.querySelectorAll('.content')).toHaveLength(1);
expect(container.querySelectorAll('.first')).toHaveLength(0);
expect(container.querySelectorAll('.second')).toHaveLength(1);
notification.destroy();
done();
}, 10);
}, 200);
},
);
});
});

0 comments on commit 5cc00b4

Please sign in to comment.