yzsd
Version:
a niubi UI component
97 lines • 3.34 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import React, { useEffect, useMemo, useRef, useState } from 'react';
import NoTifications from './notifications';
var uniqueKey = 0;
function mergeConfig() {
var clone = {};
for (var _len = arguments.length, objList = new Array(_len), _key = 0; _key < _len; _key++) {
objList[_key] = arguments[_key];
}
objList.forEach(function (obj) {
if (obj) {
Object.keys(obj).forEach(function (key) {
var val = obj[key];
if (val !== undefined) {
clone[key] = val;
}
});
}
});
return clone;
}
var useNotification = function useNotification(rootConfig) {
var _a = rootConfig || {},
_a$getContainer = _a.getContainer,
getContainer = _a$getContainer === void 0 ? function () {
return document.body;
} : _a$getContainer,
_a$prefixCls = _a.prefixCls,
prefixCls = _a$prefixCls === void 0 ? 'yzs-notification' : _a$prefixCls,
className = _a.className,
style = _a.style,
onAllRemoved = _a.onAllRemoved,
maxCount = _a.maxCount,
shareConfig = __rest(_a, ["getContainer", "prefixCls", "className", "style", "onAllRemoved", "maxCount"]);
var _useState = useState(),
_useState2 = _slicedToArray(_useState, 2),
container = _useState2[0],
setContainer = _useState2[1];
var notificationsRef = useRef(null);
var _useState3 = useState([]),
_useState4 = _slicedToArray(_useState3, 2),
queueTask = _useState4[0],
setQueueTask = _useState4[1];
var api = useMemo(function () {
return {
open: function open(config) {
var mergedConfig = mergeConfig(shareConfig, config);
if (!mergedConfig.key) {
mergedConfig.key = "".concat(prefixCls, "-").concat(uniqueKey);
uniqueKey += 1;
}
setQueueTask(function (task) {
return [].concat(_toConsumableArray(task), [{
type: 'open',
config: mergedConfig
}]);
});
},
close: function close() {},
destroy: function destroy() {}
};
}, []);
var contextHolder = /*#__PURE__*/React.createElement(NoTifications, {
ref: notificationsRef,
container: container,
prefixCls: prefixCls,
className: className,
style: style,
maxCount: maxCount,
onAllRemoved: onAllRemoved
});
useEffect(function () {
setContainer(getContainer());
});
useEffect(function () {
if (notificationsRef.current && queueTask.length) {
queueTask.forEach(function (task) {
var _a;
if (task.type === 'open') {
(_a = notificationsRef.current) === null || _a === void 0 ? void 0 : _a.open(task.config);
}
});
setQueueTask([]);
}
}, [queueTask]);
return [api, contextHolder];
};
export default useNotification;