Notification.js
9.5 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var React = _interopRequireWildcard(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _classnames = _interopRequireDefault(require("classnames"));
var _rcMotion = require("rc-motion");
var _Notice = _interopRequireDefault(require("./Notice"));
var _useNotification2 = _interopRequireDefault(require("./useNotification"));
var seed = 0;
var now = Date.now();
function getUuid() {
var id = seed;
seed += 1;
return "rcNotification_".concat(now, "_").concat(id);
}
var Notification = /*#__PURE__*/function (_Component) {
(0, _inherits2.default)(Notification, _Component);
var _super = (0, _createSuper2.default)(Notification);
function Notification() {
var _this;
(0, _classCallCheck2.default)(this, Notification);
_this = _super.apply(this, arguments);
_this.state = {
notices: []
};
_this.hookRefs = new Map();
_this.add = function (originNotice, holderCallback) {
var key = originNotice.key || getUuid();
var notice = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originNotice), {}, {
key: key
});
var maxCount = _this.props.maxCount;
_this.setState(function (previousState) {
var notices = previousState.notices;
var noticeIndex = notices.map(function (v) {
return v.notice.key;
}).indexOf(key);
var updatedNotices = notices.concat();
if (noticeIndex !== -1) {
updatedNotices.splice(noticeIndex, 1, {
notice: notice,
holderCallback: holderCallback
});
} else {
if (maxCount && notices.length >= maxCount) {
// XXX, use key of first item to update new added (let React to move exsiting
// instead of remove and mount). Same key was used before for both a) external
// manual control and b) internal react 'key' prop , which is not that good.
// eslint-disable-next-line no-param-reassign
// zombieJ: Not know why use `updateKey`. This makes Notice infinite loop in jest.
// Change to `updateMark` for compare instead.
// https://github.com/react-component/notification/commit/32299e6be396f94040bfa82517eea940db947ece
notice.key = updatedNotices[0].notice.key;
notice.updateMark = getUuid(); // zombieJ: That's why. User may close by key directly.
// We need record this but not re-render to avoid upper issue
// https://github.com/react-component/notification/issues/129
notice.userPassKey = key;
updatedNotices.shift();
}
updatedNotices.push({
notice: notice,
holderCallback: holderCallback
});
}
return {
notices: updatedNotices
};
});
};
_this.remove = function (removeKey) {
_this.setState(function (_ref) {
var notices = _ref.notices;
return {
notices: notices.filter(function (_ref2) {
var _ref2$notice = _ref2.notice,
key = _ref2$notice.key,
userPassKey = _ref2$notice.userPassKey;
var mergedKey = userPassKey || key;
return mergedKey !== removeKey;
})
};
});
};
_this.noticePropsMap = {};
return _this;
}
(0, _createClass2.default)(Notification, [{
key: "getTransitionName",
value: function getTransitionName() {
var _this$props = this.props,
prefixCls = _this$props.prefixCls,
animation = _this$props.animation;
var transitionName = this.props.transitionName;
if (!transitionName && animation) {
transitionName = "".concat(prefixCls, "-").concat(animation);
}
return transitionName;
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var notices = this.state.notices;
var _this$props2 = this.props,
prefixCls = _this$props2.prefixCls,
className = _this$props2.className,
closeIcon = _this$props2.closeIcon,
style = _this$props2.style;
var noticeKeys = [];
notices.forEach(function (_ref3, index) {
var notice = _ref3.notice,
holderCallback = _ref3.holderCallback;
var updateMark = index === notices.length - 1 ? notice.updateMark : undefined;
var key = notice.key,
userPassKey = notice.userPassKey;
var noticeProps = (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({
prefixCls: prefixCls,
closeIcon: closeIcon
}, notice), notice.props), {}, {
key: key,
noticeKey: userPassKey || key,
updateMark: updateMark,
onClose: function onClose(noticeKey) {
var _notice$onClose;
_this2.remove(noticeKey);
(_notice$onClose = notice.onClose) === null || _notice$onClose === void 0 ? void 0 : _notice$onClose.call(notice);
},
onClick: notice.onClick,
children: notice.content
}); // Give to motion
noticeKeys.push(key);
_this2.noticePropsMap[key] = {
props: noticeProps,
holderCallback: holderCallback
};
});
return React.createElement("div", {
className: (0, _classnames.default)(prefixCls, className),
style: style
}, React.createElement(_rcMotion.CSSMotionList, {
keys: noticeKeys,
motionName: this.getTransitionName(),
onVisibleChanged: function onVisibleChanged(changedVisible, _ref4) {
var key = _ref4.key;
if (!changedVisible) {
delete _this2.noticePropsMap[key];
}
}
}, function (_ref5) {
var key = _ref5.key,
motionClassName = _ref5.className,
motionStyle = _ref5.style;
var _this2$noticePropsMap = _this2.noticePropsMap[key],
noticeProps = _this2$noticePropsMap.props,
holderCallback = _this2$noticePropsMap.holderCallback;
if (holderCallback) {
return React.createElement("div", {
key: key,
className: (0, _classnames.default)(motionClassName, "".concat(prefixCls, "-hook-holder")),
style: (0, _objectSpread2.default)({}, motionStyle),
ref: function ref(div) {
if (typeof key === 'undefined') {
return;
}
if (div) {
_this2.hookRefs.set(key, div);
holderCallback(div, noticeProps);
} else {
_this2.hookRefs.delete(key);
}
}
});
}
return React.createElement(_Notice.default, Object.assign({}, noticeProps, {
className: (0, _classnames.default)(motionClassName, noticeProps === null || noticeProps === void 0 ? void 0 : noticeProps.className),
style: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, motionStyle), noticeProps === null || noticeProps === void 0 ? void 0 : noticeProps.style)
}));
}));
}
}]);
return Notification;
}(React.Component);
Notification.defaultProps = {
prefixCls: 'rc-notification',
animation: 'fade',
style: {
top: 65,
left: '50%'
}
};
Notification.newInstance = function newNotificationInstance(properties, callback) {
var _ref6 = properties || {},
getContainer = _ref6.getContainer,
props = (0, _objectWithoutProperties2.default)(_ref6, ["getContainer"]);
var div = document.createElement('div');
if (getContainer) {
var root = getContainer();
root.appendChild(div);
} else {
document.body.appendChild(div);
}
var called = false;
function ref(notification) {
if (called) {
return;
}
called = true;
callback({
notice: function notice(noticeProps) {
notification.add(noticeProps);
},
removeNotice: function removeNotice(key) {
notification.remove(key);
},
component: notification,
destroy: function destroy() {
_reactDom.default.unmountComponentAtNode(div);
if (div.parentNode) {
div.parentNode.removeChild(div);
}
},
// Hooks
useNotification: function useNotification() {
return (0, _useNotification2.default)(notification);
}
});
} // Only used for test case usage
if (process.env.NODE_ENV === 'test' && properties.TEST_RENDER) {
properties.TEST_RENDER(React.createElement(Notification, Object.assign({}, props, {
ref: ref
})));
return;
}
_reactDom.default.render(React.createElement(Notification, Object.assign({}, props, {
ref: ref
})), div);
};
var _default = Notification;
exports.default = _default;