Skip to content

Commit 1c8e09a

Browse files
committed
refactor: Use updateMark instead of update in updated
1 parent cb5355c commit 1c8e09a

3 files changed

Lines changed: 21 additions & 16 deletions

File tree

examples/simple.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Notification.newInstance(
1515

1616
function simpleFn() {
1717
notification.notice({
18-
content: <span>simple show {Date.now()}</span>,
18+
duration: 3,
19+
content: <span>simple show {String(Date.now()).slice(-5)}</span>,
1920
onClose() {
2021
console.log('simple close');
2122
},

src/Notice.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface NoticeProps {
1313
className?: string;
1414
duration?: number | null;
1515
children?: React.ReactNode;
16-
update?: boolean;
16+
updateMark?: string;
1717
closeIcon?: React.ReactNode;
1818
closable?: boolean;
1919
props?: DivProps;
@@ -37,7 +37,10 @@ export default class Notice extends Component<NoticeProps> {
3737
}
3838

3939
componentDidUpdate(prevProps: NoticeProps) {
40-
if (this.props.duration !== prevProps.duration || this.props.update) {
40+
if (
41+
this.props.duration !== prevProps.duration ||
42+
this.props.updateMark !== prevProps.updateMark
43+
) {
4144
this.restartCloseTimer();
4245
}
4346
}

src/Notification.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function getUuid() {
1818
export interface NoticeContent extends Omit<NoticeProps, 'prefixCls' | 'children'> {
1919
prefixCls?: string;
2020
key?: React.Key;
21-
updateKey?: React.Key;
21+
updateMark?: string;
2222
content?: React.ReactNode;
2323
}
2424

@@ -84,10 +84,9 @@ class Notification extends Component<NotificationProps, NotificationState> {
8484
return transitionName;
8585
}
8686

87-
add = (notice: NoticeContent, holderCallback?: HolderReadyCallback) => {
88-
// eslint-disable-next-line no-param-reassign
89-
notice.key = notice.key || getUuid();
90-
const { key } = notice;
87+
add = (originNotice: NoticeContent, holderCallback?: HolderReadyCallback) => {
88+
const key = originNotice.key || getUuid();
89+
const notice = { ...originNotice, key };
9190
const { maxCount } = this.props;
9291
this.setState(previousState => {
9392
const { notices } = previousState;
@@ -101,7 +100,12 @@ class Notification extends Component<NotificationProps, NotificationState> {
101100
// instead of remove and mount). Same key was used before for both a) external
102101
// manual control and b) internal react 'key' prop , which is not that good.
103102
// eslint-disable-next-line no-param-reassign
104-
notice.updateKey = updatedNotices[0].notice.updateKey || updatedNotices[0].notice.key;
103+
104+
// zombieJ: Not know why use `updateKey`. This makes Notice infinite loop in jest.
105+
// Change to `updateMark` for compare instead.
106+
// https://github.com/react-component/notification/commit/32299e6be396f94040bfa82517eea940db947ece
107+
notice.key = updatedNotices[0].notice.key;
108+
notice.updateMark = getUuid();
105109
updatedNotices.shift();
106110
}
107111
updatedNotices.push({ notice, holderCallback });
@@ -135,21 +139,18 @@ class Notification extends Component<NotificationProps, NotificationState> {
135139
const noticeKeys: React.Key[] = [];
136140

137141
notices.forEach(({ notice, holderCallback }, index) => {
138-
const update = Boolean(index === notices.length - 1 && notice.updateKey);
139-
const key = notice.updateKey ? notice.updateKey : notice.key;
142+
const updateMark = index === notices.length - 1 ? notice.updateMark : undefined;
143+
const { key } = notice;
140144

141-
const onClose = createChainedFunction(
142-
this.remove.bind(this, notice.key!),
143-
notice.onClose,
144-
) as any;
145+
const onClose = createChainedFunction(this.remove.bind(this, key), notice.onClose) as any;
145146

146147
const noticeProps = {
147148
prefixCls,
148149
closeIcon,
149150
...notice,
150151
...notice.props,
151152
key,
152-
update,
153+
updateMark,
153154
onClose,
154155
onClick: notice.onClick,
155156
children: notice.content,

0 commit comments

Comments
 (0)