|
|
|
import * as React from 'react';
|
|
|
|
import Notification from 'rc-notification';
|
|
|
|
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
|
|
|
|
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
|
|
|
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
|
|
|
|
import InfoCircleFilled from '@ant-design/icons/InfoCircleFilled';
|
|
|
|
|
|
|
|
let defaultDuration = 3;
|
|
|
|
let defaultTop: number;
|
|
|
|
let messageInstance: any;
|
|
|
|
let key = 1;
|
|
|
|
let prefixCls = 'ant-message';
|
|
|
|
let transitionName = 'move-up';
|
|
|
|
let getContainer: () => HTMLElement;
|
|
|
|
let maxCount: number;
|
|
|
|
|
|
|
|
function getMessageInstance(callback: (i: any) => void) {
|
|
|
|
if (messageInstance) {
|
|
|
|
callback(messageInstance);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Notification.newInstance(
|
|
|
|
{
|
|
|
|
prefixCls,
|
|
|
|
transitionName,
|
|
|
|
style: { top: defaultTop }, // 覆盖原来的样式
|
|
|
|
getContainer,
|
|
|
|
maxCount,
|
|
|
|
},
|
|
|
|
(instance: any) => {
|
|
|
|
if (messageInstance) {
|
|
|
|
callback(messageInstance);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
messageInstance = instance;
|
|
|
|
callback(instance);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading';
|
|
|
|
|
|
|
|
export interface ThenableArgument {
|
|
|
|
(val: any): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MessageType {
|
|
|
|
(): void;
|
|
|
|
then: (fill: ThenableArgument, reject: ThenableArgument) => Promise<void>;
|
|
|
|
promise: Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ArgsProps {
|
|
|
|
content: React.ReactNode;
|
|
|
|
duration: number | null;
|
|
|
|
type: NoticeType;
|
|
|
|
onClose?: () => void;
|
|
|
|
icon?: React.ReactNode;
|
|
|
|
key?: string | number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const iconMap = {
|
|
|
|
info: InfoCircleFilled,
|
|
|
|
success: CheckCircleFilled,
|
|
|
|
error: CloseCircleFilled,
|
|
|
|
warning: ExclamationCircleFilled,
|
|
|
|
loading: LoadingOutlined,
|
|
|
|
};
|
|
|
|
|
|
|
|
function notice(args: ArgsProps): MessageType {
|
|
|
|
const duration = args.duration !== undefined ? args.duration : defaultDuration;
|
|
|
|
const IconComponent = iconMap[args.type];
|
|
|
|
|
|
|
|
const target = args.key || key++;
|
|
|
|
const closePromise = new Promise(resolve => {
|
|
|
|
const callback = () => {
|
|
|
|
if (typeof args.onClose === 'function') {
|
|
|
|
args.onClose();
|
|
|
|
}
|
|
|
|
return resolve(true);
|
|
|
|
};
|
|
|
|
getMessageInstance(instance => {
|
|
|
|
instance.notice({
|
|
|
|
key: target,
|
|
|
|
duration,
|
|
|
|
style: {},
|
|
|
|
content: (
|
|
|
|
<div
|
|
|
|
className={`${prefixCls}-custom-content${
|
|
|
|
args.type ? ` ${prefixCls}-${args.type}` : ''
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{args.icon || (IconComponent && <IconComponent />)}
|
|
|