Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add closeTypes('manual', 'auto') for onClose to distinguish between manual-close by click and auto-close by duration timer. #168

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@types/enzyme": "^3.10.7",
"@types/jest": "^26.0.4",
"@types/react": "^16.9.34",
"@types/react-dom": "^16.9.7",
"@types/react-dom": "^17.0.2",
"cross-env": "^7.0.0",
"dumi": "^1.1.7",
"enzyme": "^3.3.0",
Expand Down
23 changes: 17 additions & 6 deletions src/Notice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface DivProps extends React.HTMLProps<HTMLDivElement> {
'data-testid'?: string;
}

export type CloseTypes = 'manual' | 'auto'

export interface NoticeProps {
prefixCls: string;
style?: React.CSSProperties;
Expand All @@ -21,7 +23,7 @@ export interface NoticeProps {
closable?: boolean;
props?: DivProps;
onClick?: React.MouseEventHandler<HTMLDivElement>;
onClose?: (key: React.Key) => void;
onClose?: (key: React.Key, closeType: CloseTypes) => void;

/** @private Only for internal usage. We don't promise that we will refactor this */
holder?: HTMLDivElement;
Expand Down Expand Up @@ -52,21 +54,30 @@ export default class Notice extends Component<NoticeProps> {
this.clearCloseTimer();
}

onCloseHandler = (closeType: CloseTypes) => {
const { onClose, noticeKey } = this.props;
if (onClose) {
onClose(noticeKey, closeType);
}
}

close = (e?: React.MouseEvent<HTMLAnchorElement>) => {
if (e) {
e.stopPropagation();
}
this.clearCloseTimer();
const { onClose, noticeKey } = this.props;
if (onClose) {
onClose(noticeKey);
}
this.onCloseHandler('manual');
};

autoClose = () => {
this.clearCloseTimer();
this.onCloseHandler('auto');
}

startCloseTimer = () => {
if (this.props.duration) {
this.closeTimer = window.setTimeout(() => {
this.close();
this.autoClose();
}, this.props.duration * 1000);
}
};
Expand Down
8 changes: 4 additions & 4 deletions src/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ReactText } from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import { CSSMotionList } from 'rc-motion';
import type { NoticeProps } from './Notice';
import type { NoticeProps, CloseTypes } from './Notice';
import Notice from './Notice';
import useNotification from './useNotification';

Expand All @@ -23,7 +23,7 @@ export interface NoticeContent
key?: React.Key;
updateMark?: string;
content?: React.ReactNode;
onClose?: () => void;
onClose?: (closeType: CloseTypes) => void;
}

export type NoticeFunc = (noticeProps: NoticeContent) => void;
Expand Down Expand Up @@ -168,9 +168,9 @@ class Notification extends Component<NotificationProps, NotificationState> {
key,
noticeKey: userPassKey || key,
updateMark,
onClose: (noticeKey: React.Key) => {
onClose: (noticeKey: React.Key, closeType: CloseTypes) => {
this.remove(noticeKey);
notice.onClose?.();
notice.onClose?.(closeType);
},
onClick: notice.onClick,
children: notice.content,
Expand Down