UNPKG

frc-ui

Version:

React Web UI

171 lines (144 loc) 5.63 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var React = _interopRequireWildcard(require("react")); var _rcNotification = _interopRequireDefault(require("rc-notification")); var _icon = _interopRequireDefault(require("../icon")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } // @ts-ignore var defaultDuration = 3; var defaultTop; var messageInstance; var key = 1; var prefixCls = 'swc-message'; var transitionName = 'move-up'; var getContainer; var maxCount; function getMessageInstance(callback) { if (messageInstance) { callback(messageInstance); return; } _rcNotification["default"].newInstance({ prefixCls: prefixCls, transitionName: transitionName, style: { top: defaultTop }, getContainer: getContainer, maxCount: maxCount }, function (instance) { if (messageInstance) { callback(messageInstance); return; } messageInstance = instance; callback(instance); }); } function notice(args) { var duration = args.duration !== undefined ? args.duration : defaultDuration; var iconType = { info: 'info-circle', success: 'check-circle', error: 'close-circle', warning: 'exclamation-circle', loading: 'loading' }[args.type]; var target = args.key || key++; var closePromise = new Promise(function (resolve) { var callback = function callback() { if (typeof args.onClose === 'function') { args.onClose(); } return resolve(true); }; getMessageInstance(function (instance) { var iconNode = /*#__PURE__*/React.createElement(_icon["default"], { type: iconType }); var switchIconNode = iconType ? iconNode : ''; instance.notice({ key: target, duration: duration, style: {}, content: /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-custom-content").concat(args.type ? " ".concat(prefixCls, "-").concat(args.type) : '') }, args.icon ? args.icon : switchIconNode, /*#__PURE__*/React.createElement("span", null, args.content)), onClose: callback }); }); }); var result = function result() { if (messageInstance) { messageInstance.removeNotice(target); } }; result.then = function (filled, rejected) { return closePromise.then(filled, rejected); }; result.promise = closePromise; return result; } function isArgsProps(content) { return Object.prototype.toString.call(content) === '[object Object]' && !!content.content; } var api = { open: notice, config: function config(options) { if (options.top !== undefined) { defaultTop = options.top; messageInstance = null; // delete messageInstance for new defaultTop } if (options.duration !== undefined) { defaultDuration = options.duration; } if (options.prefixCls !== undefined) { prefixCls = options.prefixCls; } if (options.getContainer !== undefined) { getContainer = options.getContainer; } if (options.transitionName !== undefined) { transitionName = options.transitionName; messageInstance = null; // delete messageInstance for new transitionName } if (options.maxCount !== undefined) { maxCount = options.maxCount; messageInstance = null; } }, destroy: function destroy() { if (messageInstance) { messageInstance.destroy(); messageInstance = null; } } }; ['success', 'info', 'warning', 'error', 'loading'].forEach(function (type) { api[type] = function (content, duration, onClose) { if (isArgsProps(content)) { return api.open(Object.assign(Object.assign({}, content), { type: type })); } if (typeof duration === 'function') { onClose = duration; duration = undefined; } return api.open({ content: content, duration: duration, type: type, onClose: onClose }); }; }); api.warn = api.warning; var _default = api; exports["default"] = _default;