Skip to content

Commit 446bbc2

Browse files
authored
fix: Remove by key not work on maxCount (#134)
1 parent 64bedf2 commit 446bbc2

2 files changed

Lines changed: 61 additions & 7 deletions

File tree

src/Notification.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export interface NotificationProps {
5050

5151
interface NotificationState {
5252
notices: {
53-
notice: NoticeContent;
53+
notice: NoticeContent & {
54+
userPassKey?: React.Key;
55+
};
5456
holderCallback?: HolderReadyCallback;
5557
}[];
5658
}
@@ -87,11 +89,14 @@ class Notification extends Component<NotificationProps, NotificationState> {
8789

8890
add = (originNotice: NoticeContent, holderCallback?: HolderReadyCallback) => {
8991
const key = originNotice.key || getUuid();
90-
const notice = { ...originNotice, key };
92+
const notice: NoticeContent & { key: React.Key; userPassKey?: React.Key } = {
93+
...originNotice,
94+
key,
95+
};
9196
const { maxCount } = this.props;
92-
this.setState(previousState => {
97+
this.setState((previousState) => {
9398
const { notices } = previousState;
94-
const noticeIndex = notices.map(v => v.notice.key).indexOf(key);
99+
const noticeIndex = notices.map((v) => v.notice.key).indexOf(key);
95100
const updatedNotices = notices.concat();
96101
if (noticeIndex !== -1) {
97102
updatedNotices.splice(noticeIndex, 1, { notice, holderCallback });
@@ -107,6 +112,12 @@ class Notification extends Component<NotificationProps, NotificationState> {
107112
// https://github.com/react-component/notification/commit/32299e6be396f94040bfa82517eea940db947ece
108113
notice.key = updatedNotices[0].notice.key;
109114
notice.updateMark = getUuid();
115+
116+
// zombieJ: That's why. User may close by key directly.
117+
// We need record this but not re-render to avoid upper issue
118+
// https://github.com/react-component/notification/issues/129
119+
notice.userPassKey = key;
120+
110121
updatedNotices.shift();
111122
}
112123
updatedNotices.push({ notice, holderCallback });
@@ -117,9 +128,12 @@ class Notification extends Component<NotificationProps, NotificationState> {
117128
});
118129
};
119130

120-
remove = (key: React.Key) => {
131+
remove = (removeKey: React.Key) => {
121132
this.setState(({ notices }) => ({
122-
notices: notices.filter(({ notice }) => notice.key !== key),
133+
notices: notices.filter(({ notice: { key, userPassKey } }) => {
134+
const mergedKey = userPassKey || key;
135+
return mergedKey !== removeKey;
136+
}),
123137
}));
124138
};
125139

@@ -181,7 +195,7 @@ class Notification extends Component<NotificationProps, NotificationState> {
181195
key={key}
182196
className={classNames(motionClassName, `${prefixCls}-hook-holder`)}
183197
style={{ ...motionStyle }}
184-
ref={div => {
198+
ref={(div) => {
185199
if (typeof key === 'undefined') {
186200
return;
187201
}

tests/index.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,46 @@ describe('Notification.Basic', () => {
176176
);
177177
});
178178

179+
it('remove work when maxCount set', done => {
180+
let wrapper;
181+
182+
Notification.newInstance(
183+
{
184+
TEST_RENDER: node => {
185+
wrapper = mount(<div>{node}</div>);
186+
},
187+
maxCount: 1,
188+
},
189+
notification => {
190+
// First
191+
notification.notice({
192+
content: <div className="max-count">bamboo</div>,
193+
key: 'bamboo',
194+
duration: 0,
195+
});
196+
197+
// Next
198+
notification.notice({
199+
content: <div className="max-count">bamboo</div>,
200+
key: 'bamboo',
201+
duration: 0,
202+
});
203+
204+
setTimeout(() => {
205+
expect(wrapper.find('.max-count')).toHaveLength(1);
206+
notification.removeNotice('bamboo');
207+
208+
setTimeout(() => {
209+
wrapper.update();
210+
expect(wrapper.find('.max-count')).toHaveLength(0);
211+
notification.destroy();
212+
done();
213+
}, 500);
214+
}, 10);
215+
},
216+
);
217+
});
218+
179219
it('update notification by key with multi instance', done => {
180220
let wrapper;
181221

0 commit comments

Comments
 (0)