ant-design/components/message/index.tsx

181 lines
4.9 KiB
TypeScript
Raw Normal View History

/* global Promise */
import * as React from 'react';
import Notification from 'rc-notification';
2015-11-20 11:05:34 +08:00
import Icon from '../icon';
2017-07-18 12:50:17 +08:00
let defaultDuration = 3;
let defaultTop: number;
let messageInstance: any;
2016-10-26 11:05:00 +08:00
let key = 1;
let prefixCls = 'ant-message';
let transitionName = 'move-up';
let getContainer: () => HTMLElement;
let maxCount: number;
2016-10-26 11:05:00 +08:00
function getMessageInstance(callback: (i: any) => void) {
if (messageInstance) {
callback(messageInstance);
return;
}
2018-12-07 20:02:01 +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';
export interface ThenableArgument {
(_: any): any;
}
export interface MessageType {
(): void;
then: (fill: ThenableArgument, reject: ThenableArgument) => Promise<any>;
promise: Promise<any>;
}
2018-08-31 12:28:09 +08:00
export interface ArgsProps {
content: React.ReactNode;
duration: number | null;
type: NoticeType;
onClose?: () => void;
icon?: React.ReactNode;
}
function notice(args: ArgsProps): MessageType {
const duration = args.duration !== undefined ? args.duration : defaultDuration;
2018-12-07 20:02:01 +08:00
const iconType = {
info: 'info-circle',
success: 'check-circle',
2018-08-30 20:43:03 +08:00
error: 'close-circle',
warning: 'exclamation-circle',
loading: 'loading',
2018-12-07 20:02:01 +08:00
}[args.type];
const target = key++;
2018-12-07 20:02:01 +08:00
const closePromise = new Promise(resolve => {
2018-08-31 12:28:09 +08:00
const callback = () => {
if (typeof args.onClose === 'function') {
args.onClose();
}
return resolve(true);
};
2018-12-07 20:02:01 +08:00
getMessageInstance(instance => {
const iconNode = (
<Icon type={iconType} theme={iconType === 'loading' ? 'outlined' : 'filled'} />
);
instance.notice({
key: target,
duration,
style: {},
content: (
2018-12-07 20:02:01 +08:00
<div
className={`${prefixCls}-custom-content${
args.type ? ` ${prefixCls}-${args.type}` : ''
}`}
>
{args.icon ? args.icon : iconType ? iconNode : ''}
2018-08-31 12:28:09 +08:00
<span>{args.content}</span>
</div>
),
onClose: callback,
});
});
2015-07-23 11:59:56 +08:00
});
const result: any = () => {
if (messageInstance) {
messageInstance.removeNotice(target);
}
};
2018-12-07 20:02:01 +08:00
result.then = (filled: ThenableArgument, rejected: ThenableArgument) =>
closePromise.then(filled, rejected);
result.promise = closePromise;
return result;
2015-07-23 11:59:56 +08:00
}
type ConfigContent = React.ReactNode | string;
type ConfigDuration = number | (() => void);
export type ConfigOnClose = () => void;
2016-07-29 16:32:12 +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;
getContainer?: () => HTMLElement;
transitionName?: string;
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) {
defaultTop = options.top;
2016-12-19 16:46:39 +08:00
messageInstance = null; // delete messageInstance for new defaultTop
}
2016-10-24 12:04:26 +08:00
if (options.duration !== undefined) {
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;
}
if (options.getContainer !== undefined) {
getContainer = options.getContainer;
}
if (options.transitionName !== undefined) {
transitionName = options.transitionName;
messageInstance = null; // delete messageInstance for new transitionName
}
if (options.maxCount !== undefined) {
maxCount = options.maxCount;
messageInstance = null;
}
},
destroy() {
if (messageInstance) {
messageInstance.destroy();
messageInstance = null;
}
},
};
2018-08-31 12:28:09 +08:00
2018-12-07 20:02:01 +08:00
['success', 'info', 'warning', 'error', 'loading'].forEach(type => {
2018-08-31 12:28:09 +08:00
api[type] = (content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) => {
if (typeof duration === 'function') {
onClose = duration;
duration = undefined;
}
return api.open({ content, duration: duration, type, onClose });
};
});
api.warn = api.warning;
export interface MessageApi {
info(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
success(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
error(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
warn(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
warning(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
loading(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
open(args: ArgsProps): MessageType;
2018-08-31 12:28:09 +08:00
config(options: ConfigOptions): void;
destroy(): void;
}
export default api as MessageApi;