2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-07-28 15:40:28 +08:00
|
|
|
import Notification from 'rc-notification';
|
2019-08-13 14:07:17 +08:00
|
|
|
import {
|
2019-11-28 12:34:33 +08:00
|
|
|
CloseOutlined,
|
|
|
|
CheckCircleOutlined,
|
|
|
|
CloseCircleOutlined,
|
|
|
|
ExclamationCircleOutlined,
|
|
|
|
InfoCircleOutlined,
|
2019-08-13 14:07:17 +08:00
|
|
|
} from '@ant-design/icons';
|
2017-08-17 15:23:47 +08:00
|
|
|
|
|
|
|
export type NotificationPlacement = 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
export type IconType = 'success' | 'info' | 'error' | 'warning';
|
2017-11-21 15:48:53 +08:00
|
|
|
|
|
|
|
const notificationInstance: { [key: string]: any } = {};
|
2017-04-24 16:12:50 +08:00
|
|
|
let defaultDuration = 4.5;
|
2017-01-27 17:12:25 +08:00
|
|
|
let defaultTop = 24;
|
|
|
|
let defaultBottom = 24;
|
2017-08-17 15:23:47 +08:00
|
|
|
let defaultPlacement: NotificationPlacement = 'topRight';
|
2017-11-21 15:48:53 +08:00
|
|
|
let defaultGetContainer: () => HTMLElement;
|
2019-11-08 20:03:27 +08:00
|
|
|
let defaultCloseIcon: React.ReactNode;
|
2017-01-27 17:12:25 +08:00
|
|
|
|
2017-08-17 15:23:47 +08:00
|
|
|
export interface ConfigProps {
|
|
|
|
top?: number;
|
|
|
|
bottom?: number;
|
|
|
|
duration?: number;
|
|
|
|
placement?: NotificationPlacement;
|
|
|
|
getContainer?: () => HTMLElement;
|
2019-11-08 20:03:27 +08:00
|
|
|
closeIcon?: React.ReactNode;
|
2017-08-17 15:23:47 +08:00
|
|
|
}
|
2019-01-05 21:33:07 +08:00
|
|
|
|
2017-08-17 15:23:47 +08:00
|
|
|
function setNotificationConfig(options: ConfigProps) {
|
2019-11-08 20:03:27 +08:00
|
|
|
const { duration, placement, bottom, top, getContainer, closeIcon } = options;
|
2017-08-17 15:23:47 +08:00
|
|
|
if (duration !== undefined) {
|
|
|
|
defaultDuration = duration;
|
|
|
|
}
|
|
|
|
if (placement !== undefined) {
|
|
|
|
defaultPlacement = placement;
|
|
|
|
}
|
|
|
|
if (bottom !== undefined) {
|
|
|
|
defaultBottom = bottom;
|
|
|
|
}
|
|
|
|
if (top !== undefined) {
|
|
|
|
defaultTop = top;
|
|
|
|
}
|
|
|
|
if (getContainer !== undefined) {
|
|
|
|
defaultGetContainer = getContainer;
|
|
|
|
}
|
2019-11-08 20:03:27 +08:00
|
|
|
if (closeIcon !== undefined) {
|
|
|
|
defaultCloseIcon = closeIcon;
|
|
|
|
}
|
2017-08-17 15:23:47 +08:00
|
|
|
}
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
function getPlacementStyle(
|
|
|
|
placement: NotificationPlacement,
|
|
|
|
top: number = defaultTop,
|
|
|
|
bottom: number = defaultBottom,
|
|
|
|
) {
|
2017-01-27 17:12:25 +08:00
|
|
|
let style;
|
|
|
|
switch (placement) {
|
|
|
|
case 'topLeft':
|
|
|
|
style = {
|
|
|
|
left: 0,
|
2019-05-06 12:04:39 +08:00
|
|
|
top,
|
2017-01-27 17:12:25 +08:00
|
|
|
bottom: 'auto',
|
|
|
|
};
|
|
|
|
break;
|
2017-08-17 15:23:47 +08:00
|
|
|
case 'topRight':
|
|
|
|
style = {
|
|
|
|
right: 0,
|
2019-05-06 12:04:39 +08:00
|
|
|
top,
|
2017-08-17 15:23:47 +08:00
|
|
|
bottom: 'auto',
|
|
|
|
};
|
|
|
|
break;
|
2017-01-27 17:12:25 +08:00
|
|
|
case 'bottomLeft':
|
|
|
|
style = {
|
|
|
|
left: 0,
|
|
|
|
top: 'auto',
|
2019-05-06 12:04:39 +08:00
|
|
|
bottom,
|
2017-01-27 17:12:25 +08:00
|
|
|
};
|
|
|
|
break;
|
2017-08-17 15:23:47 +08:00
|
|
|
default:
|
2017-01-27 17:12:25 +08:00
|
|
|
style = {
|
|
|
|
right: 0,
|
|
|
|
top: 'auto',
|
2019-05-06 12:04:39 +08:00
|
|
|
bottom,
|
2017-01-27 17:12:25 +08:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
}
|
2015-08-03 18:00:22 +08:00
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
type NotificationInstanceProps = {
|
|
|
|
prefixCls: string;
|
|
|
|
placement?: NotificationPlacement;
|
|
|
|
getContainer?: () => HTMLElement;
|
|
|
|
top?: number;
|
|
|
|
bottom?: number;
|
2019-11-08 20:03:27 +08:00
|
|
|
closeIcon?: React.ReactNode;
|
2019-05-06 12:04:39 +08:00
|
|
|
};
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
function getNotificationInstance(
|
2019-05-06 12:04:39 +08:00
|
|
|
{
|
|
|
|
prefixCls,
|
|
|
|
placement = defaultPlacement,
|
|
|
|
getContainer = defaultGetContainer,
|
|
|
|
top,
|
|
|
|
bottom,
|
2019-11-08 20:03:27 +08:00
|
|
|
closeIcon = defaultCloseIcon,
|
2019-05-06 12:04:39 +08:00
|
|
|
}: NotificationInstanceProps,
|
2018-12-07 16:17:45 +08:00
|
|
|
callback: (n: any) => void,
|
|
|
|
) {
|
2017-08-17 15:23:47 +08:00
|
|
|
const cacheKey = `${prefixCls}-${placement}`;
|
2017-10-11 12:03:19 +08:00
|
|
|
if (notificationInstance[cacheKey]) {
|
|
|
|
callback(notificationInstance[cacheKey]);
|
|
|
|
return;
|
2017-08-17 15:23:47 +08:00
|
|
|
}
|
2019-11-08 20:03:27 +08:00
|
|
|
|
|
|
|
const closeIconToRender = (
|
|
|
|
<span className={`${prefixCls}-close-x`}>
|
2019-12-01 21:46:17 +08:00
|
|
|
{closeIcon || <CloseOutlined className={`${prefixCls}-close-icon`} />}
|
2019-11-08 20:03:27 +08:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
(Notification as any).newInstance(
|
|
|
|
{
|
|
|
|
prefixCls,
|
|
|
|
className: `${prefixCls}-${placement}`,
|
2019-05-06 12:04:39 +08:00
|
|
|
style: getPlacementStyle(placement, top, bottom),
|
|
|
|
getContainer,
|
2019-11-08 20:03:27 +08:00
|
|
|
closeIcon: closeIconToRender,
|
2018-12-07 16:17:45 +08:00
|
|
|
},
|
|
|
|
(notification: any) => {
|
|
|
|
notificationInstance[cacheKey] = notification;
|
|
|
|
callback(notification);
|
|
|
|
},
|
|
|
|
);
|
2017-08-17 15:23:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const typeToIcon = {
|
2019-11-28 12:34:33 +08:00
|
|
|
success: CheckCircleOutlined,
|
|
|
|
info: InfoCircleOutlined,
|
|
|
|
error: CloseCircleOutlined,
|
|
|
|
warning: ExclamationCircleOutlined,
|
2017-08-17 15:23:47 +08:00
|
|
|
};
|
|
|
|
|
2016-08-15 12:00:05 +08:00
|
|
|
export interface ArgsProps {
|
2017-01-20 20:10:50 +08:00
|
|
|
message: React.ReactNode;
|
2018-11-15 11:32:53 +08:00
|
|
|
description?: React.ReactNode;
|
2016-08-15 12:00:05 +08:00
|
|
|
btn?: React.ReactNode;
|
|
|
|
key?: string;
|
|
|
|
onClose?: () => void;
|
2018-01-25 12:29:03 +08:00
|
|
|
duration?: number | null;
|
2016-08-28 17:45:27 +08:00
|
|
|
icon?: React.ReactNode;
|
2017-08-17 15:23:47 +08:00
|
|
|
placement?: NotificationPlacement;
|
2017-10-09 19:58:30 +08:00
|
|
|
style?: React.CSSProperties;
|
2017-08-17 15:23:47 +08:00
|
|
|
prefixCls?: string;
|
2017-05-05 14:26:55 +08:00
|
|
|
className?: string;
|
2017-11-21 15:48:53 +08:00
|
|
|
readonly type?: IconType;
|
2018-10-18 10:16:56 +08:00
|
|
|
onClick?: () => void;
|
2019-05-06 12:04:39 +08:00
|
|
|
top?: number;
|
|
|
|
bottom?: number;
|
|
|
|
getContainer?: () => HTMLElement;
|
2019-12-01 15:29:53 +08:00
|
|
|
closeIcon?: React.ReactNode;
|
2016-08-15 12:00:05 +08:00
|
|
|
}
|
2019-01-05 21:33:07 +08:00
|
|
|
|
2017-08-17 15:23:47 +08:00
|
|
|
function notice(args: ArgsProps) {
|
2016-09-14 16:18:33 +08:00
|
|
|
const outerPrefixCls = args.prefixCls || 'ant-notification';
|
|
|
|
const prefixCls = `${outerPrefixCls}-notice`;
|
2017-08-17 15:23:47 +08:00
|
|
|
const duration = args.duration === undefined ? defaultDuration : args.duration;
|
2016-04-10 13:08:56 +08:00
|
|
|
|
2017-08-17 15:23:47 +08:00
|
|
|
let iconNode: React.ReactNode = null;
|
2016-08-28 17:45:27 +08:00
|
|
|
if (args.icon) {
|
2018-12-07 16:17:45 +08:00
|
|
|
iconNode = <span className={`${prefixCls}-icon`}>{args.icon}</span>;
|
2016-08-28 17:45:27 +08:00
|
|
|
} else if (args.type) {
|
2019-08-13 14:07:17 +08:00
|
|
|
iconNode = React.createElement(typeToIcon[args.type] || null, {
|
|
|
|
className: `${prefixCls}-icon ${prefixCls}-icon-${args.type}`,
|
|
|
|
});
|
2016-08-28 17:45:27 +08:00
|
|
|
}
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
const autoMarginTag =
|
|
|
|
!args.description && iconNode ? (
|
|
|
|
<span className={`${prefixCls}-message-single-line-auto-margin`} />
|
|
|
|
) : null;
|
2017-04-22 01:28:54 +08:00
|
|
|
|
2019-11-08 20:03:27 +08:00
|
|
|
const { placement, top, bottom, getContainer, closeIcon } = args;
|
2019-05-06 12:04:39 +08:00
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
getNotificationInstance(
|
2019-05-06 12:04:39 +08:00
|
|
|
{
|
|
|
|
prefixCls: outerPrefixCls,
|
|
|
|
placement,
|
|
|
|
top,
|
|
|
|
bottom,
|
|
|
|
getContainer,
|
2019-11-08 20:03:27 +08:00
|
|
|
closeIcon,
|
2019-05-06 12:04:39 +08:00
|
|
|
},
|
2018-12-07 16:17:45 +08:00
|
|
|
(notification: any) => {
|
|
|
|
notification.notice({
|
|
|
|
content: (
|
|
|
|
<div className={iconNode ? `${prefixCls}-with-icon` : ''}>
|
|
|
|
{iconNode}
|
|
|
|
<div className={`${prefixCls}-message`}>
|
|
|
|
{autoMarginTag}
|
|
|
|
{args.message}
|
|
|
|
</div>
|
|
|
|
<div className={`${prefixCls}-description`}>{args.description}</div>
|
|
|
|
{args.btn ? <span className={`${prefixCls}-btn`}>{args.btn}</span> : null}
|
2017-10-11 12:03:19 +08:00
|
|
|
</div>
|
2018-12-07 16:17:45 +08:00
|
|
|
),
|
|
|
|
duration,
|
|
|
|
closable: true,
|
|
|
|
onClose: args.onClose,
|
|
|
|
onClick: args.onClick,
|
|
|
|
key: args.key,
|
|
|
|
style: args.style || {},
|
|
|
|
className: args.className,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2015-07-31 18:13:32 +08:00
|
|
|
}
|
2015-07-28 15:40:28 +08:00
|
|
|
|
2017-08-17 15:23:47 +08:00
|
|
|
const api: any = {
|
|
|
|
open: notice,
|
2017-11-21 15:48:53 +08:00
|
|
|
close(key: string) {
|
2018-12-07 16:17:45 +08:00
|
|
|
Object.keys(notificationInstance).forEach(cacheKey =>
|
|
|
|
notificationInstance[cacheKey].removeNotice(key),
|
|
|
|
);
|
2016-01-21 14:13:28 +08:00
|
|
|
},
|
2017-08-17 15:23:47 +08:00
|
|
|
config: setNotificationConfig,
|
2016-01-21 14:13:28 +08:00
|
|
|
destroy() {
|
2017-08-17 15:23:47 +08:00
|
|
|
Object.keys(notificationInstance).forEach(cacheKey => {
|
|
|
|
notificationInstance[cacheKey].destroy();
|
|
|
|
delete notificationInstance[cacheKey];
|
2017-05-08 16:57:43 +08:00
|
|
|
});
|
2016-01-21 14:13:28 +08:00
|
|
|
},
|
2015-07-31 18:13:32 +08:00
|
|
|
};
|
2015-08-04 15:16:10 +08:00
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
['success', 'info', 'warning', 'error'].forEach(type => {
|
|
|
|
api[type] = (args: ArgsProps) =>
|
|
|
|
api.open({
|
|
|
|
...args,
|
|
|
|
type,
|
|
|
|
});
|
2015-08-04 15:16:10 +08:00
|
|
|
});
|
|
|
|
|
2017-08-17 15:23:47 +08:00
|
|
|
api.warn = api.warning;
|
2016-03-28 18:03:00 +08:00
|
|
|
|
2017-08-17 15:23:47 +08:00
|
|
|
export interface NotificationApi {
|
|
|
|
success(args: ArgsProps): void;
|
|
|
|
error(args: ArgsProps): void;
|
|
|
|
info(args: ArgsProps): void;
|
|
|
|
warn(args: ArgsProps): void;
|
|
|
|
warning(args: ArgsProps): void;
|
|
|
|
open(args: ArgsProps): void;
|
|
|
|
close(key: string): void;
|
|
|
|
config(options: ConfigProps): void;
|
|
|
|
destroy(): void;
|
|
|
|
}
|
2019-01-05 21:33:07 +08:00
|
|
|
|
2017-06-13 14:43:19 +08:00
|
|
|
export default api as NotificationApi;
|