123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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", "stack", "renderNotifications"];
- import * as React from 'react';
- import Notifications from "../Notifications";
- import { useEvent } from 'rc-util';
- 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,
- stack = rootConfig.stack,
- 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,
- stack: stack,
- renderNotifications: renderNotifications
- });
- var _React$useState3 = React.useState([]),
- _React$useState4 = _slicedToArray(_React$useState3, 2),
- taskQueue = _React$useState4[0],
- setTaskQueue = _React$useState4[1];
- var open = useEvent(function (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
- }]);
- });
- });
- // ========================= Refs =========================
- var api = React.useMemo(function () {
- return {
- open: open,
- 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;
- }
- });
- // https://github.com/ant-design/ant-design/issues/52590
- // React `startTransition` will run once `useEffect` but many times `setState`,
- // So `setTaskQueue` with filtered array will cause infinite loop.
- // We cache the first match queue instead.
- var oriTaskQueue;
- var tgtTaskQueue;
- // 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) {
- if (oriTaskQueue !== oriQueue || !tgtTaskQueue) {
- oriTaskQueue = oriQueue;
- tgtTaskQueue = oriQueue.filter(function (task) {
- return !taskQueue.includes(task);
- });
- }
- return tgtTaskQueue;
- });
- }
- }, [taskQueue]);
- // ======================== Return ========================
- return [api, contextHolder];
- }
|