UNPKG

5.1 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports["default"] = void 0;
9
10var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
12var _react = _interopRequireDefault(require("react"));
13
14var _icon = _interopRequireDefault(require("../icon"));
15
16var _notification = _interopRequireDefault(require("../rc-components/notification"));
17
18var _configure = require("../configure");
19
20var notificationInstance = {};
21var defaultDuration = 4.5;
22var defaultTop = 24;
23var defaultBottom = 24;
24var defaultPlacement = 'topRight';
25var defaultGetContainer;
26
27function setNotificationConfig(options) {
28 var duration = options.duration,
29 placement = options.placement,
30 bottom = options.bottom,
31 top = options.top,
32 getContainer = options.getContainer;
33
34 if (duration !== undefined) {
35 defaultDuration = duration;
36 }
37
38 if (placement !== undefined) {
39 defaultPlacement = placement;
40 }
41
42 if (bottom !== undefined) {
43 defaultBottom = bottom;
44 }
45
46 if (top !== undefined) {
47 defaultTop = top;
48 }
49
50 if (getContainer !== undefined) {
51 defaultGetContainer = getContainer;
52 }
53}
54
55function getPlacementStyle(placement) {
56 var style;
57
58 switch (placement) {
59 case 'topLeft':
60 style = {
61 left: 0,
62 top: defaultTop,
63 bottom: 'auto'
64 };
65 break;
66
67 case 'topRight':
68 style = {
69 right: 0,
70 top: defaultTop,
71 bottom: 'auto'
72 };
73 break;
74
75 case 'bottomLeft':
76 style = {
77 left: 0,
78 top: 'auto',
79 bottom: defaultBottom
80 };
81 break;
82
83 default:
84 style = {
85 right: 0,
86 top: 'auto',
87 bottom: defaultBottom
88 };
89 break;
90 }
91
92 return style;
93}
94
95function getNotificationInstance(prefixCls, placement, callback) {
96 var cacheKey = "".concat(prefixCls, "-").concat(placement);
97
98 if (notificationInstance[cacheKey]) {
99 callback(notificationInstance[cacheKey]);
100 return;
101 }
102
103 _notification["default"].newInstance({
104 prefixCls: prefixCls,
105 className: "".concat(prefixCls, "-").concat(placement),
106 style: getPlacementStyle(placement),
107 getContainer: defaultGetContainer,
108 closeIcon: _react["default"].createElement(_icon["default"], {
109 className: "".concat(prefixCls, "-close-icon"),
110 type: "close"
111 })
112 }, function (notification) {
113 notificationInstance[cacheKey] = notification;
114 callback(notification);
115 });
116}
117
118var typeToIcon = {
119 success: 'check',
120 info: 'info',
121 error: 'error',
122 warning: 'warning'
123};
124
125function notice(args) {
126 var outerPrefixCls = (0, _configure.getPrefixCls)('notification', args.prefixCls);
127 var prefixCls = "".concat(outerPrefixCls, "-notice");
128 var duration = args.duration === undefined ? defaultDuration : args.duration;
129 var iconNode = null;
130
131 if (args.icon) {
132 iconNode = _react["default"].createElement("span", {
133 className: "".concat(prefixCls, "-icon")
134 }, args.icon);
135 } else if (args.type) {
136 var iconType = typeToIcon[args.type];
137 iconNode = _react["default"].createElement(_icon["default"], {
138 className: "".concat(prefixCls, "-icon ").concat(prefixCls, "-icon-").concat(args.type),
139 type: iconType
140 });
141 }
142
143 var autoMarginTag = !args.description && iconNode ? _react["default"].createElement("span", {
144 className: "".concat(prefixCls, "-message-single-line-auto-margin")
145 }) : null;
146 getNotificationInstance(outerPrefixCls, args.placement || defaultPlacement, function (notification) {
147 notification.notice({
148 content: _react["default"].createElement("div", {
149 className: iconNode ? "".concat(prefixCls, "-with-icon") : ''
150 }, iconNode, _react["default"].createElement("div", {
151 className: "".concat(prefixCls, "-message")
152 }, autoMarginTag, args.message), _react["default"].createElement("div", {
153 className: "".concat(prefixCls, "-description")
154 }, args.description), args.btn ? _react["default"].createElement("span", {
155 className: "".concat(prefixCls, "-btn")
156 }, args.btn) : null),
157 duration: duration,
158 closable: true,
159 onClose: args.onClose,
160 key: args.key,
161 style: args.style || {},
162 className: args.className
163 });
164 });
165}
166
167var api = {
168 open: notice,
169 close: function close(key) {
170 Object.keys(notificationInstance).forEach(function (cacheKey) {
171 return notificationInstance[cacheKey].removeNotice(key);
172 });
173 },
174 config: setNotificationConfig,
175 destroy: function destroy() {
176 Object.keys(notificationInstance).forEach(function (cacheKey) {
177 notificationInstance[cacheKey].destroy();
178 delete notificationInstance[cacheKey];
179 });
180 }
181};
182['success', 'info', 'warning', 'error'].forEach(function (type) {
183 api[type] = function (args) {
184 return api.open((0, _objectSpread2["default"])({}, args, {
185 type: type
186 }));
187 };
188});
189api.warn = api.warning;
190var _default = api;
191exports["default"] = _default;
192//# sourceMappingURL=index.js.map