UNPKG

acud

Version:

acg react 组件库

231 lines (229 loc) • 9.07 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.actWrapper = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var React = _interopRequireWildcard(require("react")); var _configProvider = _interopRequireWildcard(require("../config-provider")); var _PurePanel = _interopRequireDefault(require("./PurePanel")); var _useNotification = _interopRequireWildcard(require("./useNotification")); var _UnstableContext = require("../config-provider/UnstableContext"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } // import {render} from 'rc-util/lib/React/render'; var notification = null; var act = function act(callback) { return callback(); }; var taskQueue = []; var defaultGlobalConfig = {}; function getGlobalContext() { var _defaultGlobalConfig = defaultGlobalConfig, globalPrefixCls = _defaultGlobalConfig.prefixCls, globalGetContainer = _defaultGlobalConfig.getContainer, rtl = _defaultGlobalConfig.rtl, maxCount = _defaultGlobalConfig.maxCount, top = _defaultGlobalConfig.top, bottom = _defaultGlobalConfig.bottom; var mergedPrefixCls = globalPrefixCls ? globalPrefixCls + '-notification' : (0, _configProvider.globalConfig)().getPrefixCls('') + '-notification'; var mergedContainer = (globalGetContainer === null || globalGetContainer === void 0 ? void 0 : globalGetContainer()) || document.body; return { prefixCls: mergedPrefixCls, container: mergedContainer, rtl: rtl, maxCount: maxCount, top: top, bottom: bottom }; } var GlobalHolder = /*#__PURE__*/React.forwardRef(function (_, ref) { var _React$useState = React.useState(), _React$useState2 = (0, _slicedToArray2["default"])(_React$useState, 2), prefixCls = _React$useState2[0], setPrefixCls = _React$useState2[1]; var _React$useState3 = React.useState(), _React$useState4 = (0, _slicedToArray2["default"])(_React$useState3, 2), container = _React$useState4[0], setContainer = _React$useState4[1]; var _React$useState5 = React.useState(), _React$useState6 = (0, _slicedToArray2["default"])(_React$useState5, 2), maxCount = _React$useState6[0], setMaxCount = _React$useState6[1]; var _React$useState7 = React.useState(), _React$useState8 = (0, _slicedToArray2["default"])(_React$useState7, 2), rtl = _React$useState8[0], setRTL = _React$useState8[1]; var _React$useState9 = React.useState(), _React$useState0 = (0, _slicedToArray2["default"])(_React$useState9, 2), top = _React$useState0[0], setTop = _React$useState0[1]; var _React$useState1 = React.useState(), _React$useState10 = (0, _slicedToArray2["default"])(_React$useState1, 2), bottom = _React$useState10[0], setBottom = _React$useState10[1]; var _useInternalNotificat = (0, _useNotification.useInternalNotification)({ prefixCls: prefixCls, getContainer: function getContainer() { return container; }, maxCount: maxCount, rtl: rtl, top: top, bottom: bottom }), _useInternalNotificat2 = (0, _slicedToArray2["default"])(_useInternalNotificat, 2), api = _useInternalNotificat2[0], holder = _useInternalNotificat2[1]; var global = (0, _configProvider.globalConfig)(); var _globalConfig = (0, _configProvider.globalConfig)(), getPrefixCls = _globalConfig.getPrefixCls; var rootPrefixCls = getPrefixCls('') + '-notification'; // const rootPrefixCls = global.getRootPrefixCls();· var rootIconPrefixCls = global.getIconPrefixCls(); var sync = function sync() { var _getGlobalContext = getGlobalContext(), nextGlobalPrefixCls = _getGlobalContext.prefixCls, nextGlobalContainer = _getGlobalContext.container, nextGlobalMaxCount = _getGlobalContext.maxCount, nextGlobalRTL = _getGlobalContext.rtl, nextTop = _getGlobalContext.top, nextBottom = _getGlobalContext.bottom; setPrefixCls(nextGlobalPrefixCls); setContainer(nextGlobalContainer); setMaxCount(nextGlobalMaxCount); setRTL(nextGlobalRTL); setTop(nextTop); setBottom(nextBottom); }; React.useEffect(sync, []); React.useImperativeHandle(ref, function () { var instance = (0, _extends2["default"])({}, api); Object.keys(instance).forEach(function (method) { instance[method] = function () { sync(); return api[method].apply(api, arguments); }; }); return { instance: instance, sync: sync }; }); return /*#__PURE__*/React.createElement(_configProvider["default"], { prefixCls: rootPrefixCls, iconPrefixCls: rootIconPrefixCls }, holder); }); function flushNotice() { if (!notification) { var holderFragment = document.createDocumentFragment(); var newNotification = { fragment: holderFragment }; notification = newNotification; // Delay render to avoid sync issue act(function () { var render = (0, _UnstableContext.unstableSetRender)(); render(/*#__PURE__*/React.createElement(GlobalHolder, { ref: function ref(node) { var _ref = node || {}, instance = _ref.instance, sync = _ref.sync; Promise.resolve().then(function () { if (!newNotification.instance && instance) { newNotification.instance = instance; newNotification.sync = sync; flushNotice(); } }); } }), holderFragment); }); return; } // Notification not ready if (!notification.instance) { return; } // >>> Execute task taskQueue.forEach(function (task) { // eslint-disable-next-line default-case switch (task.type) { case 'open': { act(function () { notification.instance.open((0, _extends2["default"])((0, _extends2["default"])({}, defaultGlobalConfig), task.config)); }); break; } case 'destroy': act(function () { notification === null || notification === void 0 ? void 0 : notification.instance.destroy(task.key); }); break; } }); // Clean up taskQueue = []; } // ============================================================================== // == Export == // ============================================================================== function setNotificationGlobalConfig(config) { defaultGlobalConfig = (0, _extends2["default"])((0, _extends2["default"])({}, defaultGlobalConfig), config); // Trigger sync for it act(function () { var _a; (_a = notification === null || notification === void 0 ? void 0 : notification.sync) === null || _a === void 0 ? void 0 : _a.call(notification); }); } function open(config) { // Warning if exist theme if (process.env.NODE_ENV !== 'production') { // warnContext('notification'); } taskQueue.push({ type: 'open', config: config }); flushNotice(); } function destroy(key) { taskQueue.push({ type: 'destroy', key: key }); flushNotice(); } var methods = ['success', 'info', 'warning', 'error']; var baseStaticMethods = { open: open, destroy: destroy, config: setNotificationGlobalConfig, useNotification: _useNotification["default"], _InternalPanelDoNotUseOrYouWillBeFired: _PurePanel["default"] }; var staticMethods = baseStaticMethods; methods.forEach(function (type) { staticMethods[type] = function (config) { return open((0, _extends2["default"])((0, _extends2["default"])({}, config), { type: type })); }; }); // ============================================================================== // == Test == // ============================================================================== var noop = function noop() {}; /** @private Only Work in test env */ // eslint-disable-next-line import/no-mutable-exports var actWrapper = exports.actWrapper = noop; if (process.env.NODE_ENV === 'test') { exports.actWrapper = actWrapper = function actWrapper(wrapper) { act = wrapper; }; } var _default = exports["default"] = staticMethods;