useNotification.js
1.44 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
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useNotification;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var React = _interopRequireWildcard(require("react"));
var _Notice = _interopRequireDefault(require("./Notice"));
function useNotification(notificationInstance) {
var createdRef = React.useRef({});
var _React$useState = React.useState([]),
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
elements = _React$useState2[0],
setElements = _React$useState2[1];
function notify(noticeProps) {
notificationInstance.add(noticeProps, function (div, props) {
var key = props.key;
if (div && !createdRef.current[key]) {
var noticeEle = React.createElement(_Notice.default, Object.assign({}, props, {
holder: div
}));
createdRef.current[key] = noticeEle;
setElements(function (originElements) {
return [].concat((0, _toConsumableArray2.default)(originElements), [noticeEle]);
});
}
});
}
return [notify, React.createElement(React.Fragment, null, elements)];
}