2019-01-12 03:33:27 +00:00
|
|
|
import Notification from '../vc-notification';
|
|
|
|
import Icon from '../icon';
|
2018-02-06 07:21:13 +00:00
|
|
|
|
|
|
|
// export type NotificationPlacement = 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';
|
|
|
|
|
|
|
|
// export type IconType = 'success' | 'info' | 'error' | 'warning';
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const notificationInstance = {};
|
|
|
|
let defaultDuration = 4.5;
|
|
|
|
let defaultTop = '24px';
|
|
|
|
let defaultBottom = '24px';
|
|
|
|
let defaultPlacement = 'topRight';
|
|
|
|
let defaultGetContainer = () => document.body;
|
2018-02-06 07:21:13 +00:00
|
|
|
|
|
|
|
// export interface ConfigProps {
|
|
|
|
// top?: number;
|
|
|
|
// bottom?: number;
|
|
|
|
// duration?: number;
|
|
|
|
// placement?: NotificationPlacement;
|
|
|
|
// getContainer?: () => HTMLElement;
|
|
|
|
// }
|
2019-01-12 03:33:27 +00:00
|
|
|
function setNotificationConfig(options) {
|
|
|
|
const { duration, placement, bottom, top, getContainer } = options;
|
2018-02-06 07:21:13 +00:00
|
|
|
if (duration !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultDuration = duration;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
if (placement !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultPlacement = placement;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
if (bottom !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultBottom = typeof bottom === 'number' ? `${bottom}px` : bottom;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
if (top !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultTop = typeof top === 'number' ? `${top}px` : top;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
if (getContainer !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultGetContainer = getContainer;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function getPlacementStyle(placement) {
|
|
|
|
let style;
|
2018-02-06 07:21:13 +00:00
|
|
|
switch (placement) {
|
|
|
|
case 'topLeft':
|
|
|
|
style = {
|
|
|
|
left: 0,
|
|
|
|
top: defaultTop,
|
|
|
|
bottom: 'auto',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
break;
|
2018-02-06 07:21:13 +00:00
|
|
|
case 'topRight':
|
|
|
|
style = {
|
|
|
|
right: 0,
|
|
|
|
top: defaultTop,
|
|
|
|
bottom: 'auto',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
break;
|
2018-02-06 07:21:13 +00:00
|
|
|
case 'bottomLeft':
|
|
|
|
style = {
|
|
|
|
left: 0,
|
|
|
|
top: 'auto',
|
|
|
|
bottom: defaultBottom,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
break;
|
2018-02-06 07:21:13 +00:00
|
|
|
default:
|
|
|
|
style = {
|
|
|
|
right: 0,
|
|
|
|
top: 'auto',
|
|
|
|
bottom: defaultBottom,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
break;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return style;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function getNotificationInstance(prefixCls, placement, callback) {
|
|
|
|
const cacheKey = `${prefixCls}-${placement}`;
|
2018-02-06 07:21:13 +00:00
|
|
|
if (notificationInstance[cacheKey]) {
|
2019-01-12 03:33:27 +00:00
|
|
|
callback(notificationInstance[cacheKey]);
|
|
|
|
return;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
Notification.newInstance(
|
|
|
|
{
|
|
|
|
prefixCls,
|
|
|
|
class: `${prefixCls}-${placement}`,
|
|
|
|
style: getPlacementStyle(placement),
|
|
|
|
getContainer: defaultGetContainer,
|
2019-02-01 09:23:00 +00:00
|
|
|
closeIcon: h => <Icon class={`${prefixCls}-close-icon`} type={'close'} />, // eslint-disable-line
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
notification => {
|
|
|
|
notificationInstance[cacheKey] = notification;
|
|
|
|
callback(notification);
|
|
|
|
},
|
|
|
|
);
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const typeToIcon = {
|
|
|
|
success: 'check-circle-o',
|
|
|
|
info: 'info-circle-o',
|
2018-12-09 10:08:47 +00:00
|
|
|
error: 'close-circle-o',
|
2018-02-06 07:21:13 +00:00
|
|
|
warning: 'exclamation-circle-o',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-02-06 07:21:13 +00:00
|
|
|
|
|
|
|
// export interface ArgsProps {
|
|
|
|
// message: React.ReactNode;
|
|
|
|
// description: React.ReactNode;
|
|
|
|
// btn?: React.ReactNode;
|
|
|
|
// key?: string;
|
|
|
|
// onClose?: () => void;
|
|
|
|
// duration?: number | null;
|
|
|
|
// icon?: React.ReactNode;
|
|
|
|
// placement?: NotificationPlacement;
|
|
|
|
// style?: React.CSSProperties;
|
|
|
|
// prefixCls?: string;
|
|
|
|
// className?: string;
|
|
|
|
// readonly type?: IconType;
|
|
|
|
// }
|
2019-01-12 03:33:27 +00:00
|
|
|
function notice(args) {
|
|
|
|
const { icon, type, description, placement, message, btn } = args;
|
|
|
|
const outerPrefixCls = args.prefixCls || 'ant-notification';
|
|
|
|
const prefixCls = `${outerPrefixCls}-notice`;
|
|
|
|
const duration = args.duration === undefined ? defaultDuration : args.duration;
|
2018-02-06 07:21:13 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
let iconNode = null;
|
2018-02-06 07:21:13 +00:00
|
|
|
if (icon) {
|
2019-01-12 03:33:27 +00:00
|
|
|
iconNode = h => (
|
|
|
|
<span class={`${prefixCls}-icon`}>{typeof icon === 'function' ? icon(h) : icon}</span>
|
|
|
|
);
|
2018-02-06 07:21:13 +00:00
|
|
|
} else if (type) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const iconType = typeToIcon[type];
|
2019-02-01 09:23:00 +00:00
|
|
|
iconNode = h => <Icon class={`${prefixCls}-icon ${prefixCls}-icon-${type}`} type={iconType} />; // eslint-disable-line
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getNotificationInstance(outerPrefixCls, placement || defaultPlacement, notification => {
|
2018-02-06 07:21:13 +00:00
|
|
|
notification.notice({
|
2019-01-12 03:33:27 +00:00
|
|
|
content: h => (
|
2018-02-06 07:21:13 +00:00
|
|
|
<div class={iconNode ? `${prefixCls}-with-icon` : ''}>
|
|
|
|
{iconNode && iconNode(h)}
|
|
|
|
<div class={`${prefixCls}-message`}>
|
2019-01-12 03:33:27 +00:00
|
|
|
{!description && iconNode ? (
|
|
|
|
<span class={`${prefixCls}-message-single-line-auto-margin`} />
|
|
|
|
) : null}
|
2018-02-06 07:21:13 +00:00
|
|
|
{typeof message === 'function' ? message(h) : message}
|
|
|
|
</div>
|
|
|
|
<div class={`${prefixCls}-description`}>
|
|
|
|
{typeof description === 'function' ? description(h) : description}
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
{btn ? (
|
|
|
|
<span class={`${prefixCls}-btn`}>{typeof btn === 'function' ? btn(h) : btn}</span>
|
|
|
|
) : null}
|
2018-02-06 07:21:13 +00:00
|
|
|
</div>
|
|
|
|
),
|
|
|
|
duration,
|
|
|
|
closable: true,
|
|
|
|
onClose: args.onClose,
|
2019-01-05 02:55:03 +00:00
|
|
|
onClick: args.onClick,
|
2018-02-06 07:21:13 +00:00
|
|
|
key: args.key,
|
|
|
|
style: args.style || {},
|
|
|
|
class: args.class,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const api = {
|
|
|
|
open: notice,
|
2019-01-12 03:33:27 +00:00
|
|
|
close(key) {
|
|
|
|
Object.keys(notificationInstance).forEach(cacheKey =>
|
|
|
|
notificationInstance[cacheKey].removeNotice(key),
|
|
|
|
);
|
2018-02-06 07:21:13 +00:00
|
|
|
},
|
|
|
|
config: setNotificationConfig,
|
2019-01-12 03:33:27 +00:00
|
|
|
destroy() {
|
2018-02-06 07:21:13 +00:00
|
|
|
Object.keys(notificationInstance).forEach(cacheKey => {
|
2019-01-12 03:33:27 +00:00
|
|
|
notificationInstance[cacheKey].destroy();
|
|
|
|
delete notificationInstance[cacheKey];
|
|
|
|
});
|
2018-02-06 07:21:13 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
['success', 'info', 'warning', 'error'].forEach(type => {
|
|
|
|
api[type] = args =>
|
|
|
|
api.open({
|
|
|
|
...args,
|
|
|
|
type,
|
|
|
|
});
|
|
|
|
});
|
2018-02-06 07:21:13 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
api.warn = api.warning;
|
2018-02-06 07:21:13 +00: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-12 03:33:27 +00:00
|
|
|
export default api;
|