useNotification.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = useNotification;
  7. exports.useInternalNotification = useInternalNotification;
  8. var _vue = require("vue");
  9. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  10. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  11. var _vcNotification = require("../vc-notification");
  12. var _style = _interopRequireDefault(require("./style"));
  13. var _PurePanel = require("./PurePanel");
  14. var _util = require("./util");
  15. var _useConfigInject = _interopRequireDefault(require("../config-provider/hooks/useConfigInject"));
  16. var _classNames = _interopRequireDefault(require("../_util/classNames"));
  17. var __rest = void 0 && (void 0).__rest || function (s, e) {
  18. var t = {};
  19. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  20. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  21. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  22. }
  23. return t;
  24. };
  25. const DEFAULT_OFFSET = 24;
  26. const DEFAULT_DURATION = 4.5;
  27. const Holder = (0, _vue.defineComponent)({
  28. name: 'Holder',
  29. inheritAttrs: false,
  30. props: ['prefixCls', 'class', 'type', 'icon', 'content', 'onAllRemoved'],
  31. setup(props, _ref) {
  32. let {
  33. expose
  34. } = _ref;
  35. const {
  36. getPrefixCls,
  37. getPopupContainer
  38. } = (0, _useConfigInject.default)('notification', props);
  39. const prefixCls = (0, _vue.computed)(() => props.prefixCls || getPrefixCls('notification'));
  40. // =============================== Style ===============================
  41. const getStyles = placement => {
  42. var _a, _b;
  43. return (0, _util.getPlacementStyle)(placement, (_a = props.top) !== null && _a !== void 0 ? _a : DEFAULT_OFFSET, (_b = props.bottom) !== null && _b !== void 0 ? _b : DEFAULT_OFFSET);
  44. };
  45. // Style
  46. const [, hashId] = (0, _style.default)(prefixCls);
  47. const getClassName = () => (0, _classNames.default)(hashId.value, {
  48. [`${prefixCls.value}-rtl`]: props.rtl
  49. });
  50. // ============================== Motion ===============================
  51. const getNotificationMotion = () => (0, _util.getMotion)(prefixCls.value);
  52. // ============================== Origin ===============================
  53. const [api, holder] = (0, _vcNotification.useNotification)({
  54. prefixCls: prefixCls.value,
  55. getStyles,
  56. getClassName,
  57. motion: getNotificationMotion,
  58. closable: true,
  59. closeIcon: (0, _PurePanel.getCloseIcon)(prefixCls.value),
  60. duration: DEFAULT_DURATION,
  61. getContainer: () => {
  62. var _a, _b;
  63. return ((_a = props.getPopupContainer) === null || _a === void 0 ? void 0 : _a.call(props)) || ((_b = getPopupContainer.value) === null || _b === void 0 ? void 0 : _b.call(getPopupContainer)) || document.body;
  64. },
  65. maxCount: props.maxCount,
  66. hashId: hashId.value,
  67. onAllRemoved: props.onAllRemoved
  68. });
  69. // ================================ Ref ================================
  70. expose((0, _extends2.default)((0, _extends2.default)({}, api), {
  71. prefixCls: prefixCls.value,
  72. hashId
  73. }));
  74. return holder;
  75. }
  76. });
  77. // ==============================================================================
  78. // == Hook ==
  79. // ==============================================================================
  80. function useInternalNotification(notificationConfig) {
  81. const holderRef = (0, _vue.shallowRef)(null);
  82. const holderKey = Symbol('notificationHolderKey');
  83. // ================================ API ================================
  84. // Wrap with notification content
  85. // >>> Open
  86. const open = config => {
  87. if (!holderRef.value) {
  88. return;
  89. }
  90. const {
  91. open: originOpen,
  92. prefixCls,
  93. hashId
  94. } = holderRef.value;
  95. const noticePrefixCls = `${prefixCls}-notice`;
  96. const {
  97. message,
  98. description,
  99. icon,
  100. type,
  101. btn,
  102. class: className
  103. } = config,
  104. restConfig = __rest(config, ["message", "description", "icon", "type", "btn", "class"]);
  105. return originOpen((0, _extends2.default)((0, _extends2.default)({
  106. placement: 'topRight'
  107. }, restConfig), {
  108. content: () => (0, _vue.createVNode)(_PurePanel.PureContent, {
  109. "prefixCls": noticePrefixCls,
  110. "icon": typeof icon === 'function' ? icon() : icon,
  111. "type": type,
  112. "message": typeof message === 'function' ? message() : message,
  113. "description": typeof description === 'function' ? description() : description,
  114. "btn": typeof btn === 'function' ? btn() : btn
  115. }, null),
  116. // @ts-ignore
  117. class: (0, _classNames.default)(type && `${noticePrefixCls}-${type}`, hashId, className)
  118. }));
  119. };
  120. // >>> destroy
  121. const destroy = key => {
  122. var _a, _b;
  123. if (key !== undefined) {
  124. (_a = holderRef.value) === null || _a === void 0 ? void 0 : _a.close(key);
  125. } else {
  126. (_b = holderRef.value) === null || _b === void 0 ? void 0 : _b.destroy();
  127. }
  128. };
  129. const wrapAPI = {
  130. open,
  131. destroy
  132. };
  133. const keys = ['success', 'info', 'warning', 'error'];
  134. keys.forEach(type => {
  135. wrapAPI[type] = config => open((0, _extends2.default)((0, _extends2.default)({}, config), {
  136. type
  137. }));
  138. });
  139. // ============================== Return ===============================
  140. return [wrapAPI, () => (0, _vue.createVNode)(Holder, (0, _objectSpread2.default)((0, _objectSpread2.default)({
  141. "key": holderKey
  142. }, notificationConfig), {}, {
  143. "ref": holderRef
  144. }), null)];
  145. }
  146. function useNotification(notificationConfig) {
  147. return useInternalNotification(notificationConfig);
  148. }