antd message源码分析

github地址
调用了 rc-notification 模块。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* global Promise */
import * as React from 'react';
import Notification from 'rc-notification';
import Icon from '../icon';

let defaultDuration = 3; // 持续时间 s
let defaultTop: number; // css top值
let messageInstance: any; // message实例
let key = 1; // 自增key
let prefixCls = 'ant-message'; // prefix cls
let transitionName = 'move-up'; // transition
let getContainer: () => HTMLElement;// container
let maxCount: number; // message个数阈值

/**
* 如果有 messageInstance 则 callback(messageInstance)
* 否则创建一个 messageInstance 再 callback(messageInstance)
* @param {any} callback
*/
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 {
(_: any): any;
}

export interface MessageType {
(): void;
then: (fill: ThenableArgument, reject: ThenableArgument) => Promise<any>;
promise: Promise<any>;
}

/**
* @param {React.ReactNode} content message组件的content
* @param {func | number} duration 持续时间
* @param {NoticeType} type 类型
*/
function notice(
content: React.ReactNode,
duration: (() => void) | number = defaultDuration,
type: NoticeType,
onClose?: () => void,
): MessageType {
const iconType = ({
info: 'info-circle',
success: 'check-circle',
error: 'cross-circle',
warning: 'exclamation-circle',
loading: 'loading',
})[type];

if (typeof duration === 'function') {
onClose = duration;
duration = defaultDuration;
}

const target = key++;

// 调用 messageInstance,并将 onClose 作为 callback 传进去
const closePromise = new Promise((resolve) => {
const callback = () => {
if (typeof onClose === 'function') {
onClose();
}
return resolve(true);
};
getMessageInstance((instance) => {
instance.notice({
key: target,
duration,
style: {},
content: (
<div className={`${prefixCls}-custom-content ${prefixCls}-${type}`}>
<Icon type={iconType} />
<span>{content}</span>
</div>
),
onClose: callback,
});
});
});
const result: any = () => {
if (messageInstance) {
messageInstance.removeNotice(target);
}
};
result.then = (filled: ThenableArgument, rejected: ThenableArgument) => closePromise.then(filled, rejected);
result.promise = closePromise;

// 返回remove notice的函数
return result;
}

type ConfigContent = React.ReactNode | string;
type ConfigDuration = number | (() => void);
export type ConfigOnClose = () => void;

export interface ConfigOptions {
top?: number;
duration?: number;
prefixCls?: string;
getContainer?: () => HTMLElement;
transitionName?: string;
maxCount?: number;
}

// 入口
export default {
info(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) {
return notice(content, duration, 'info', onClose);
},
success(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) {
return notice(content, duration, 'success', onClose);
},
error(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) {
return notice(content, duration, 'error', onClose);
},
// Departed usage, please use warning()
warn(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) {
return notice(content, duration, 'warning', onClose);
},
warning(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) {
return notice(content, duration, 'warning', onClose);
},
loading(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) {
return notice(content, duration, 'loading', onClose);
},
config(options: ConfigOptions) {
if (options.top !== undefined) {
defaultTop = options.top;
messageInstance = null; // delete messageInstance for new defaultTop
}
if (options.duration !== undefined) {
defaultDuration = options.duration;
}
if (options.prefixCls !== undefined) {
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;
}
},
};