amis-rpc-design/node_modules/rc-notification/es/useNotification.js
2023-10-07 19:42:30 +08:00

135 lines
4.5 KiB
JavaScript

import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["getContainer", "motion", "prefixCls", "maxCount", "className", "style", "onAllRemoved", "renderNotifications"];
import * as React from 'react';
import Notifications from "./Notifications";
var defaultGetContainer = function defaultGetContainer() {
return document.body;
};
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;
}
export default function useNotification() {
var rootConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _rootConfig$getContai = rootConfig.getContainer,
getContainer = _rootConfig$getContai === void 0 ? defaultGetContainer : _rootConfig$getContai,
motion = rootConfig.motion,
prefixCls = rootConfig.prefixCls,
maxCount = rootConfig.maxCount,
className = rootConfig.className,
style = rootConfig.style,
onAllRemoved = rootConfig.onAllRemoved,
renderNotifications = rootConfig.renderNotifications,
shareConfig = _objectWithoutProperties(rootConfig, _excluded);
var _React$useState = React.useState(),
_React$useState2 = _slicedToArray(_React$useState, 2),
container = _React$useState2[0],
setContainer = _React$useState2[1];
var notificationsRef = React.useRef();
var contextHolder = /*#__PURE__*/React.createElement(Notifications, {
container: container,
ref: notificationsRef,
prefixCls: prefixCls,
motion: motion,
maxCount: maxCount,
className: className,
style: style,
onAllRemoved: onAllRemoved,
renderNotifications: renderNotifications
});
var _React$useState3 = React.useState([]),
_React$useState4 = _slicedToArray(_React$useState3, 2),
taskQueue = _React$useState4[0],
setTaskQueue = _React$useState4[1];
// ========================= Refs =========================
var api = React.useMemo(function () {
return {
open: function open(config) {
var mergedConfig = mergeConfig(shareConfig, config);
if (mergedConfig.key === null || mergedConfig.key === undefined) {
mergedConfig.key = "rc-notification-".concat(uniqueKey);
uniqueKey += 1;
}
setTaskQueue(function (queue) {
return [].concat(_toConsumableArray(queue), [{
type: 'open',
config: mergedConfig
}]);
});
},
close: function close(key) {
setTaskQueue(function (queue) {
return [].concat(_toConsumableArray(queue), [{
type: 'close',
key: key
}]);
});
},
destroy: function destroy() {
setTaskQueue(function (queue) {
return [].concat(_toConsumableArray(queue), [{
type: 'destroy'
}]);
});
}
};
}, []);
// ======================= Container ======================
// React 18 should all in effect that we will check container in each render
// Which means getContainer should be stable.
React.useEffect(function () {
setContainer(getContainer());
});
// ======================== Effect ========================
React.useEffect(function () {
// Flush task when node ready
if (notificationsRef.current && taskQueue.length) {
taskQueue.forEach(function (task) {
switch (task.type) {
case 'open':
notificationsRef.current.open(task.config);
break;
case 'close':
notificationsRef.current.close(task.key);
break;
case 'destroy':
notificationsRef.current.destroy();
break;
}
});
// React 17 will mix order of effect & setState in async
// - open: setState[0]
// - effect[0]
// - open: setState[1]
// - effect setState([]) * here will clean up [0, 1] in React 17
setTaskQueue(function (oriQueue) {
return oriQueue.filter(function (task) {
return !taskQueue.includes(task);
});
});
}
}, [taskQueue]);
// ======================== Return ========================
return [api, contextHolder];
}