index.js
8.3 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function (resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import * as React from 'react';
import Notification from 'rc-notification';
import CloseOutlined from '@ant-design/icons/CloseOutlined';
import classNames from 'classnames';
import CheckCircleOutlined from '@ant-design/icons/CheckCircleOutlined';
import CloseCircleOutlined from '@ant-design/icons/CloseCircleOutlined';
import ExclamationCircleOutlined from '@ant-design/icons/ExclamationCircleOutlined';
import InfoCircleOutlined from '@ant-design/icons/InfoCircleOutlined';
import createUseNotification from './hooks/useNotification';
var notificationInstance = {};
var defaultDuration = 4.5;
var defaultTop = 24;
var defaultBottom = 24;
var defaultPrefixCls = 'ant-notification';
var defaultPlacement = 'topRight';
var defaultGetContainer;
var defaultCloseIcon;
var rtl = false;
function setNotificationConfig(options) {
var duration = options.duration,
placement = options.placement,
bottom = options.bottom,
top = options.top,
getContainer = options.getContainer,
closeIcon = options.closeIcon,
prefixCls = options.prefixCls;
if (prefixCls !== undefined) {
defaultPrefixCls = prefixCls;
}
if (duration !== undefined) {
defaultDuration = duration;
}
if (placement !== undefined) {
defaultPlacement = placement;
} else if (options.rtl) {
defaultPlacement = 'topLeft';
}
if (bottom !== undefined) {
defaultBottom = bottom;
}
if (top !== undefined) {
defaultTop = top;
}
if (getContainer !== undefined) {
defaultGetContainer = getContainer;
}
if (closeIcon !== undefined) {
defaultCloseIcon = closeIcon;
}
if (options.rtl !== undefined) {
rtl = options.rtl;
}
}
function getPlacementStyle(placement) {
var top = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultTop;
var bottom = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultBottom;
var style;
switch (placement) {
case 'topLeft':
style = {
left: 0,
top: top,
bottom: 'auto'
};
break;
case 'topRight':
style = {
right: 0,
top: top,
bottom: 'auto'
};
break;
case 'bottomLeft':
style = {
left: 0,
top: 'auto',
bottom: bottom
};
break;
default:
style = {
right: 0,
top: 'auto',
bottom: bottom
};
break;
}
return style;
}
function getNotificationInstance(args, callback) {
var _args$placement = args.placement,
placement = _args$placement === void 0 ? defaultPlacement : _args$placement,
top = args.top,
bottom = args.bottom,
_args$getContainer = args.getContainer,
getContainer = _args$getContainer === void 0 ? defaultGetContainer : _args$getContainer,
_args$closeIcon = args.closeIcon,
closeIcon = _args$closeIcon === void 0 ? defaultCloseIcon : _args$closeIcon;
var outerPrefixCls = args.prefixCls || defaultPrefixCls;
var prefixCls = "".concat(outerPrefixCls, "-notice");
var cacheKey = "".concat(outerPrefixCls, "-").concat(placement);
var cacheInstance = notificationInstance[cacheKey];
if (cacheInstance) {
Promise.resolve(cacheInstance).then(function (instance) {
callback({
prefixCls: prefixCls,
instance: instance
});
});
return;
}
var closeIconToRender = /*#__PURE__*/React.createElement("span", {
className: "".concat(outerPrefixCls, "-close-x")
}, closeIcon || /*#__PURE__*/React.createElement(CloseOutlined, {
className: "".concat(outerPrefixCls, "-close-icon")
}));
var notificationClass = classNames("".concat(outerPrefixCls, "-").concat(placement), _defineProperty({}, "".concat(outerPrefixCls, "-rtl"), rtl === true));
notificationInstance[cacheKey] = new Promise(function (resolve) {
Notification.newInstance({
prefixCls: outerPrefixCls,
className: notificationClass,
style: getPlacementStyle(placement, top, bottom),
getContainer: getContainer,
closeIcon: closeIconToRender
}, function (notification) {
resolve(notification);
callback({
prefixCls: prefixCls,
instance: notification
});
});
});
}
var typeToIcon = {
success: CheckCircleOutlined,
info: InfoCircleOutlined,
error: CloseCircleOutlined,
warning: ExclamationCircleOutlined
};
function getRCNoticeProps(args, prefixCls) {
var duration = args.duration === undefined ? defaultDuration : args.duration;
var iconNode = null;
if (args.icon) {
iconNode = /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-icon")
}, args.icon);
} else if (args.type) {
iconNode = /*#__PURE__*/React.createElement(typeToIcon[args.type] || null, {
className: "".concat(prefixCls, "-icon ").concat(prefixCls, "-icon-").concat(args.type)
});
}
var autoMarginTag = !args.description && iconNode ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-message-single-line-auto-margin")
}) : null;
return {
content: /*#__PURE__*/React.createElement("div", {
className: iconNode ? "".concat(prefixCls, "-with-icon") : '',
role: "alert"
}, iconNode, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-message")
}, autoMarginTag, args.message), /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-description")
}, args.description), args.btn ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-btn")
}, args.btn) : null),
duration: duration,
closable: true,
onClose: args.onClose,
onClick: args.onClick,
key: args.key,
style: args.style || {},
className: args.className
};
}
function notice(args) {
getNotificationInstance(args, function (_ref) {
var prefixCls = _ref.prefixCls,
instance = _ref.instance;
instance.notice(getRCNoticeProps(args, prefixCls));
});
}
var api = {
open: notice,
close: function close(key) {
Object.keys(notificationInstance).forEach(function (cacheKey) {
return Promise.resolve(notificationInstance[cacheKey]).then(function (instance) {
instance.removeNotice(key);
});
});
},
config: setNotificationConfig,
destroy: function destroy() {
Object.keys(notificationInstance).forEach(function (cacheKey) {
Promise.resolve(notificationInstance[cacheKey]).then(function (instance) {
instance.destroy();
});
delete notificationInstance[cacheKey]; // lgtm[js/missing-await]
});
}
};
['success', 'info', 'warning', 'error'].forEach(function (type) {
api[type] = function (args) {
return api.open(_extends(_extends({}, args), {
type: type
}));
};
});
api.warn = api.warning;
api.useNotification = createUseNotification(getNotificationInstance, getRCNoticeProps);
/** @private test only function. Not work on production */
export var getInstance = function getInstance(cacheKey) {
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", process.env.NODE_ENV === 'test' ? notificationInstance[cacheKey] : null);
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
}));
};
export default api;