Skip to content

Commit e61fd19

Browse files
authored
fix: correct latest notice when removed (#316)
* fix: correct latest notice when removed * chore: code clean * chore: code clean
1 parent 526990d commit e61fd19

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

docs/examples/maxCount.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useNotification } from '../../src';
55
import motion from './motion';
66

77
export default () => {
8-
const [notice, contextHolder] = useNotification({ motion, maxCount: 3, top: 100 });
8+
const [notice, contextHolder] = useNotification({ motion, maxCount: 3 });
99

1010
return (
1111
<>

src/NoticeList.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,19 @@ const NoticeList: FC<NoticeListProps> = (props) => {
6262

6363
// Clean hover key
6464
useEffect(() => {
65-
if (hoverKeys.length > 1) {
65+
if (stack && hoverKeys.length > 1) {
6666
setHoverKeys((prev) =>
6767
prev.filter((key) => keys.some(({ key: dataKey }) => key === dataKey)),
6868
);
6969
}
70-
}, [hoverKeys, keys]);
70+
}, [hoverKeys, keys, stack]);
71+
72+
// Force update latest notice
73+
useEffect(() => {
74+
if (stack && dictRef.current[keys[keys.length - 1]?.key]) {
75+
setLatestNotice(dictRef.current[keys[keys.length - 1]?.key]);
76+
}
77+
}, [keys, stack]);
7178

7279
return (
7380
<CSSMotionList
@@ -83,11 +90,6 @@ const NoticeList: FC<NoticeListProps> = (props) => {
8390
onAllRemoved={() => {
8491
onAllNoticeRemoved(placement);
8592
}}
86-
onAppearPrepare={(element) => {
87-
if (element.parentNode.lastElementChild === element) {
88-
setLatestNotice(element as HTMLDivElement);
89-
}
90-
}}
9193
>
9294
{(
9395
{ config, className: motionClassName, style: motionStyle, index: motionIndex },
@@ -99,10 +101,10 @@ const NoticeList: FC<NoticeListProps> = (props) => {
99101

100102
// If dataIndex is -1, that means this notice has been removed in data, but still in dom
101103
// Should minus (motionIndex - 1) to get the correct index because keys.length is not the same as dom length
102-
const index = keys.length - 1 - (dataIndex > -1 ? dataIndex : motionIndex - 1);
103-
const transformX = placement === 'top' || placement === 'bottom' ? '-50%' : '0';
104104
const stackStyle: CSSProperties = {};
105105
if (stack) {
106+
const index = keys.length - 1 - (dataIndex > -1 ? dataIndex : motionIndex - 1);
107+
const transformX = placement === 'top' || placement === 'bottom' ? '-50%' : '0';
106108
if (index > 0) {
107109
stackStyle.height = expanded
108110
? dictRef.current[key]?.offsetHeight
@@ -156,7 +158,7 @@ const NoticeList: FC<NoticeListProps> = (props) => {
156158
key={key}
157159
eventKey={key}
158160
onNoticeClose={onNoticeClose}
159-
hovering={hoverKeys.length > 0}
161+
hovering={stack && hoverKeys.length > 0}
160162
/>
161163
</div>
162164
);

0 commit comments

Comments
 (0)