index.js
5.59 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import * as React from 'react';
import classNames from 'classnames';
import RCNotification 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';
import createUseMessage from './hooks/useMessage';
var messageInstance;
var defaultDuration = 3;
var defaultTop;
var key = 1;
var localPrefixCls = 'ant-message';
var transitionName = 'move-up';
var getContainer;
var maxCount;
var rtl = false;
export function getKeyThenIncreaseKey() {
return key++;
}
function setMessageConfig(options) {
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) {
localPrefixCls = 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;
}
if (options.rtl !== undefined) {
rtl = options.rtl;
}
}
function getRCNotificationInstance(args, callback) {
var prefixCls = args.prefixCls || localPrefixCls;
if (messageInstance) {
callback({
prefixCls: prefixCls,
instance: messageInstance
});
return;
}
RCNotification.newInstance({
prefixCls: prefixCls,
transitionName: transitionName,
style: {
top: defaultTop
},
getContainer: getContainer,
maxCount: maxCount
}, function (instance) {
if (messageInstance) {
callback({
prefixCls: prefixCls,
instance: messageInstance
});
return;
}
messageInstance = instance;
callback({
prefixCls: prefixCls,
instance: instance
});
});
}
var typeToIcon = {
info: InfoCircleFilled,
success: CheckCircleFilled,
error: CloseCircleFilled,
warning: ExclamationCircleFilled,
loading: LoadingOutlined
};
function getRCNoticeProps(args, prefixCls) {
var _classNames;
var duration = args.duration !== undefined ? args.duration : defaultDuration;
var IconComponent = typeToIcon[args.type];
var messageClass = classNames("".concat(prefixCls, "-custom-content"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(args.type), args.type), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), rtl === true), _classNames));
return {
key: args.key,
duration: duration,
style: args.style || {},
className: args.className,
content: /*#__PURE__*/React.createElement("div", {
className: messageClass
}, args.icon || IconComponent && /*#__PURE__*/React.createElement(IconComponent, null), /*#__PURE__*/React.createElement("span", null, args.content)),
onClose: args.onClose
};
}
function notice(args) {
var target = args.key || key++;
var closePromise = new Promise(function (resolve) {
var callback = function callback() {
if (typeof args.onClose === 'function') {
args.onClose();
}
return resolve(true);
};
getRCNotificationInstance(args, function (_ref) {
var prefixCls = _ref.prefixCls,
instance = _ref.instance;
instance.notice(getRCNoticeProps(_extends(_extends({}, args), {
key: target,
onClose: callback
}), prefixCls));
});
});
var result = function result() {
if (messageInstance) {
messageInstance.removeNotice(target);
}
};
result.then = function (filled, rejected) {
return closePromise.then(filled, rejected);
};
result.promise = closePromise;
return result;
}
function isArgsProps(content) {
return Object.prototype.toString.call(content) === '[object Object]' && !!content.content;
}
var api = {
open: notice,
config: setMessageConfig,
destroy: function destroy(messageKey) {
if (messageInstance) {
if (messageKey) {
var _messageInstance = messageInstance,
removeNotice = _messageInstance.removeNotice;
removeNotice(messageKey);
} else {
var _messageInstance2 = messageInstance,
destroy = _messageInstance2.destroy;
destroy();
messageInstance = null;
}
}
}
};
export function attachTypeApi(originalApi, type) {
originalApi[type] = function (content, duration, onClose) {
if (isArgsProps(content)) {
return originalApi.open(_extends(_extends({}, content), {
type: type
}));
}
if (typeof duration === 'function') {
onClose = duration;
duration = undefined;
}
return originalApi.open({
content: content,
duration: duration,
type: type,
onClose: onClose
});
};
}
['success', 'info', 'warning', 'error', 'loading'].forEach(function (type) {
return attachTypeApi(api, type);
});
api.warn = api.warning;
api.useMessage = createUseMessage(getRCNotificationInstance, getRCNoticeProps);
/** @private test only function. Not work on production */
export var getInstance = function getInstance() {
return process.env.NODE_ENV === 'test' ? messageInstance : null;
};
export default api;