Skip to content

Commit

Permalink
refactor: Use updateMark instead of update in updated
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Sep 27, 2020
1 parent cb5355c commit 1c8e09a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
3 changes: 2 additions & 1 deletion examples/simple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Notification.newInstance(

function simpleFn() {
notification.notice({
content: <span>simple show {Date.now()}</span>,
duration: 3,
content: <span>simple show {String(Date.now()).slice(-5)}</span>,
onClose() {
console.log('simple close');
},
Expand Down
7 changes: 5 additions & 2 deletions src/Notice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface NoticeProps {
className?: string;
duration?: number | null;
children?: React.ReactNode;
update?: boolean;
updateMark?: string;
closeIcon?: React.ReactNode;
closable?: boolean;
props?: DivProps;
Expand All @@ -37,7 +37,10 @@ export default class Notice extends Component<NoticeProps> {
}

componentDidUpdate(prevProps: NoticeProps) {
if (this.props.duration !== prevProps.duration || this.props.update) {
if (
this.props.duration !== prevProps.duration ||
this.props.updateMark !== prevProps.updateMark
) {
this.restartCloseTimer();
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function getUuid() {
export interface NoticeContent extends Omit<NoticeProps, 'prefixCls' | 'children'> {
prefixCls?: string;
key?: React.Key;
updateKey?: React.Key;
updateMark?: string;
content?: React.ReactNode;
}

Expand Down Expand Up @@ -84,10 +84,9 @@ class Notification extends Component<NotificationProps, NotificationState> {
return transitionName;
}

add = (notice: NoticeContent, holderCallback?: HolderReadyCallback) => {
// eslint-disable-next-line no-param-reassign
notice.key = notice.key || getUuid();
const { key } = notice;
add = (originNotice: NoticeContent, holderCallback?: HolderReadyCallback) => {
const key = originNotice.key || getUuid();
const notice = { ...originNotice, key };
const { maxCount } = this.props;
this.setState(previousState => {
const { notices } = previousState;
Expand All @@ -101,7 +100,12 @@ class Notification extends Component<NotificationProps, NotificationState> {
// instead of remove and mount). Same key was used before for both a) external
// manual control and b) internal react 'key' prop , which is not that good.
// eslint-disable-next-line no-param-reassign
notice.updateKey = updatedNotices[0].notice.updateKey || updatedNotices[0].notice.key;

// zombieJ: Not know why use `updateKey`. This makes Notice infinite loop in jest.
// Change to `updateMark` for compare instead.
// https://github.com/react-component/notification/commit/32299e6be396f94040bfa82517eea940db947ece
notice.key = updatedNotices[0].notice.key;
notice.updateMark = getUuid();
updatedNotices.shift();
}
updatedNotices.push({ notice, holderCallback });
Expand Down Expand Up @@ -135,21 +139,18 @@ class Notification extends Component<NotificationProps, NotificationState> {
const noticeKeys: React.Key[] = [];

notices.forEach(({ notice, holderCallback }, index) => {
const update = Boolean(index === notices.length - 1 && notice.updateKey);
const key = notice.updateKey ? notice.updateKey : notice.key;
const updateMark = index === notices.length - 1 ? notice.updateMark : undefined;
const { key } = notice;

const onClose = createChainedFunction(
this.remove.bind(this, notice.key!),
notice.onClose,
) as any;
const onClose = createChainedFunction(this.remove.bind(this, key), notice.onClose) as any;

const noticeProps = {
prefixCls,
closeIcon,
...notice,
...notice.props,
key,
update,
updateMark,
onClose,
onClick: notice.onClick,
children: notice.content,
Expand Down

0 comments on commit 1c8e09a

Please sign in to comment.