2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-07-11 16:40:59 +08:00
|
|
|
import Notification from 'rc-notification';
|
2019-08-13 14:07:17 +08:00
|
|
|
import {
|
2019-11-28 12:34:33 +08:00
|
|
|
LoadingOutlined,
|
2019-08-13 14:07:17 +08:00
|
|
|
ExclamationCircleFilled,
|
|
|
|
CloseCircleFilled,
|
|
|
|
CheckCircleFilled,
|
|
|
|
InfoCircleFilled,
|
|
|
|
} from '@ant-design/icons';
|
2015-07-11 16:40:59 +08:00
|
|
|
|
2017-07-18 12:50:17 +08:00
|
|
|
let defaultDuration = 3;
|
2017-11-21 17:16:48 +08:00
|
|
|
let defaultTop: number;
|
|
|
|
let messageInstance: any;
|
2016-10-26 11:05:00 +08:00
|
|
|
let key = 1;
|
2016-10-27 17:39:28 +08:00
|
|
|
let prefixCls = 'ant-message';
|
2018-03-09 13:30:50 +08:00
|
|
|
let transitionName = 'move-up';
|
2017-11-21 17:16:48 +08:00
|
|
|
let getContainer: () => HTMLElement;
|
2018-04-20 15:40:43 +08:00
|
|
|
let maxCount: number;
|
2016-10-26 11:05:00 +08:00
|
|
|
|
2017-11-21 17:16:48 +08:00
|
|
|
function getMessageInstance(callback: (i: any) => void) {
|
2017-10-11 12:03:19 +08:00
|
|
|
if (messageInstance) {
|
|
|
|
callback(messageInstance);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-07 16:17:45 +08:00
|
|
|
Notification.newInstance(
|
|
|
|
{
|
|
|
|
prefixCls,
|
|
|
|
transitionName,
|
|
|
|
style: { top: defaultTop }, // 覆盖原来的样式
|
|
|
|
getContainer,
|
|
|
|
maxCount,
|
|
|
|
},
|
|
|
|
(instance: any) => {
|
|
|
|
if (messageInstance) {
|
|
|
|
callback(messageInstance);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
messageInstance = instance;
|
|
|
|
callback(instance);
|
|
|
|
},
|
|
|
|
);
|
2015-07-23 11:59:56 +08:00
|
|
|
}
|
|
|
|
|
2016-07-29 16:32:12 +08:00
|
|
|
type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading';
|
|
|
|
|
2018-05-25 20:41:07 +08:00
|
|
|
export interface ThenableArgument {
|
2019-06-24 11:29:58 +08:00
|
|
|
(val: any): void;
|
2018-05-25 20:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface MessageType {
|
|
|
|
(): void;
|
2019-06-16 20:51:47 +08:00
|
|
|
then: (fill: ThenableArgument, reject: ThenableArgument) => Promise<void>;
|
|
|
|
promise: Promise<void>;
|
2018-05-25 20:41:07 +08:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:28:09 +08:00
|
|
|
export interface ArgsProps {
|
|
|
|
content: React.ReactNode;
|
|
|
|
duration: number | null;
|
|
|
|
type: NoticeType;
|
|
|
|
onClose?: () => void;
|
|
|
|
icon?: React.ReactNode;
|
2019-09-05 10:15:14 +08:00
|
|
|
key?: string | number;
|
2018-08-31 12:28:09 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
const iconMap = {
|
|
|
|
info: InfoCircleFilled,
|
|
|
|
success: CheckCircleFilled,
|
|
|
|
error: CloseCircleFilled,
|
|
|
|
warning: ExclamationCircleFilled,
|
2019-11-28 12:34:33 +08:00
|
|
|
loading: LoadingOutlined,
|
2019-08-13 14:07:17 +08:00
|
|
|
};
|
|
|
|
|
2018-08-31 12:28:09 +08:00
|
|
|
function notice(args: ArgsProps): MessageType {
|
|
|
|
const duration = args.duration !== undefined ? args.duration : defaultDuration;
|
2019-08-13 14:07:17 +08:00
|
|
|
const IconComponent = iconMap[args.type];
|
2017-10-14 14:28:57 +08:00
|
|
|
|
2019-11-28 10:01:43 +08:00
|
|
|
const target = args.key || key++;
|
2018-12-07 16:17:45 +08:00
|
|
|
const closePromise = new Promise(resolve => {
|
2018-08-31 12:28:09 +08:00
|
|
|
const callback = () => {
|
|
|
|
if (typeof args.onClose === 'function') {
|
|
|
|
args.onClose();
|
2018-05-25 20:41:07 +08:00
|
|
|
}
|
|
|
|
return resolve(true);
|
|
|
|
};
|
2018-12-07 16:17:45 +08:00
|
|
|
getMessageInstance(instance => {
|
2018-05-25 20:41:07 +08:00
|
|
|
instance.notice({
|
2019-11-28 10:01:43 +08:00
|
|
|
key: target,
|
2018-05-25 20:41:07 +08:00
|
|
|
duration,
|
|
|
|
style: {},
|
|
|
|
content: (
|
2018-12-07 16:17:45 +08:00
|
|
|
<div
|
|
|
|
className={`${prefixCls}-custom-content${
|
|
|
|
args.type ? ` ${prefixCls}-${args.type}` : ''
|
|
|
|
}`}
|
|
|
|
>
|
2019-08-13 14:07:17 +08:00
|
|
|
{args.icon ? args.icon : <IconComponent />}
|
2018-08-31 12:28:09 +08:00
|
|
|
<span>{args.content}</span>
|
2018-05-25 20:41:07 +08:00
|
|
|
</div>
|
|
|
|
),
|
|
|
|
onClose: callback,
|
|
|
|
});
|
2017-10-11 12:03:19 +08:00
|
|
|
});
|
2015-07-23 11:59:56 +08:00
|
|
|
});
|
2018-05-25 20:41:07 +08:00
|
|
|
const result: any = () => {
|
2017-11-18 15:26:02 +08:00
|
|
|
if (messageInstance) {
|
|
|
|
messageInstance.removeNotice(target);
|
|
|
|
}
|
|
|
|
};
|
2018-12-07 16:17:45 +08:00
|
|
|
result.then = (filled: ThenableArgument, rejected: ThenableArgument) =>
|
|
|
|
closePromise.then(filled, rejected);
|
2018-05-25 20:41:07 +08:00
|
|
|
result.promise = closePromise;
|
|
|
|
return result;
|
2015-07-23 11:59:56 +08:00
|
|
|
}
|
2015-07-11 16:40:59 +08:00
|
|
|
|
2016-09-19 10:17:07 +08:00
|
|
|
type ConfigContent = React.ReactNode | string;
|
2017-10-14 14:28:57 +08:00
|
|
|
type ConfigDuration = number | (() => void);
|
2019-09-05 10:15:14 +08:00
|
|
|
type JointContent = ConfigContent | ArgsProps;
|
2016-10-18 11:55:00 +08:00
|
|
|
export type ConfigOnClose = () => void;
|
2016-07-29 16:32:12 +08:00
|
|
|
|
2019-09-05 10:15:14 +08:00
|
|
|
function isArgsProps(content: JointContent): content is ArgsProps {
|
2019-12-30 22:14:17 +08:00
|
|
|
return (
|
|
|
|
Object.prototype.toString.call(content) === '[object Object]' &&
|
|
|
|
!!(content as ArgsProps).content
|
|
|
|
);
|
2019-09-05 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export interface ConfigOptions {
|
2016-07-29 16:32:12 +08:00
|
|
|
top?: number;
|
|
|
|
duration?: number;
|
|
|
|
prefixCls?: string;
|
2017-02-27 13:39:37 +08:00
|
|
|
getContainer?: () => HTMLElement;
|
2018-03-09 13:30:50 +08:00
|
|
|
transitionName?: string;
|
2018-04-20 15:40:43 +08:00
|
|
|
maxCount?: number;
|
2016-07-29 16:32:12 +08:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:28:09 +08:00
|
|
|
const api: any = {
|
|
|
|
open: notice,
|
2016-07-29 16:32:12 +08:00
|
|
|
config(options: ConfigOptions) {
|
2016-10-24 12:04:26 +08:00
|
|
|
if (options.top !== undefined) {
|
2016-03-09 21:55:46 +08:00
|
|
|
defaultTop = options.top;
|
2016-12-19 16:46:39 +08:00
|
|
|
messageInstance = null; // delete messageInstance for new defaultTop
|
2016-03-09 21:55:46 +08:00
|
|
|
}
|
2016-10-24 12:04:26 +08:00
|
|
|
if (options.duration !== undefined) {
|
2016-03-09 21:55:46 +08:00
|
|
|
defaultDuration = options.duration;
|
2015-07-30 11:47:46 +08:00
|
|
|
}
|
2016-10-24 12:04:26 +08:00
|
|
|
if (options.prefixCls !== undefined) {
|
2016-04-16 17:38:30 +08:00
|
|
|
prefixCls = options.prefixCls;
|
|
|
|
}
|
2017-03-19 02:07:39 +08:00
|
|
|
if (options.getContainer !== undefined) {
|
|
|
|
getContainer = options.getContainer;
|
|
|
|
}
|
2018-03-09 13:30:50 +08:00
|
|
|
if (options.transitionName !== undefined) {
|
|
|
|
transitionName = options.transitionName;
|
|
|
|
messageInstance = null; // delete messageInstance for new transitionName
|
|
|
|
}
|
2018-04-20 15:40:43 +08:00
|
|
|
if (options.maxCount !== undefined) {
|
|
|
|
maxCount = options.maxCount;
|
|
|
|
messageInstance = null;
|
|
|
|
}
|
2016-01-21 14:13:28 +08:00
|
|
|
},
|
|
|
|
destroy() {
|
2016-10-27 17:39:28 +08:00
|
|
|
if (messageInstance) {
|
|
|
|
messageInstance.destroy();
|
|
|
|
messageInstance = null;
|
2016-01-21 14:13:28 +08:00
|
|
|
}
|
|
|
|
},
|
2015-07-11 16:40:59 +08:00
|
|
|
};
|
2018-08-31 12:28:09 +08:00
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
['success', 'info', 'warning', 'error', 'loading'].forEach(type => {
|
2019-09-05 10:15:14 +08:00
|
|
|
api[type] = (content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose) => {
|
|
|
|
if (isArgsProps(content)) {
|
|
|
|
return api.open({ ...content, type });
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:28:09 +08:00
|
|
|
if (typeof duration === 'function') {
|
|
|
|
onClose = duration;
|
|
|
|
duration = undefined;
|
|
|
|
}
|
2019-09-05 10:15:14 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
return api.open({ content, duration, type, onClose });
|
2018-08-31 12:28:09 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
api.warn = api.warning;
|
|
|
|
|
|
|
|
export interface MessageApi {
|
2019-09-05 10:15:14 +08:00
|
|
|
info(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
|
|
|
success(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
|
|
|
error(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
|
|
|
warn(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
|
|
|
warning(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
|
|
|
loading(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
2018-09-09 22:50:12 +08:00
|
|
|
open(args: ArgsProps): MessageType;
|
2018-08-31 12:28:09 +08:00
|
|
|
config(options: ConfigOptions): void;
|
|
|
|
destroy(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default api as MessageApi;
|