Notification.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  4. var _excluded = ["name", "getContainer", "appContext", "prefixCls", "rootPrefixCls", "transitionName", "hasTransitionName"];
  5. import { createVNode as _createVNode } from "vue";
  6. import { getTransitionGroupProps } from '../_util/transition';
  7. import { createVNode, computed, defineComponent, ref, TransitionGroup, onMounted, render as vueRender } from 'vue';
  8. import Notice from './Notice';
  9. import ConfigProvider, { globalConfigForApi } from '../config-provider';
  10. var seed = 0;
  11. var now = Date.now();
  12. function getUuid() {
  13. var id = seed;
  14. seed += 1;
  15. return "rcNotification_".concat(now, "_").concat(id);
  16. }
  17. var Notification = defineComponent({
  18. name: 'Notification',
  19. inheritAttrs: false,
  20. props: ['prefixCls', 'transitionName', 'animation', 'maxCount', 'closeIcon'],
  21. setup: function setup(props, _ref) {
  22. var attrs = _ref.attrs,
  23. expose = _ref.expose,
  24. slots = _ref.slots;
  25. var hookRefs = new Map();
  26. var notices = ref([]);
  27. var transitionProps = computed(function () {
  28. var prefixCls = props.prefixCls,
  29. _props$animation = props.animation,
  30. animation = _props$animation === void 0 ? 'fade' : _props$animation;
  31. var name = props.transitionName;
  32. if (!name && animation) {
  33. name = "".concat(prefixCls, "-").concat(animation);
  34. }
  35. return getTransitionGroupProps(name);
  36. });
  37. var add = function add(originNotice, holderCallback) {
  38. var key = originNotice.key || getUuid();
  39. var notice = _objectSpread(_objectSpread({}, originNotice), {}, {
  40. key: key
  41. });
  42. var maxCount = props.maxCount;
  43. var noticeIndex = notices.value.map(function (v) {
  44. return v.notice.key;
  45. }).indexOf(key);
  46. var updatedNotices = notices.value.concat();
  47. if (noticeIndex !== -1) {
  48. updatedNotices.splice(noticeIndex, 1, {
  49. notice: notice,
  50. holderCallback: holderCallback
  51. });
  52. } else {
  53. if (maxCount && notices.value.length >= maxCount) {
  54. // XXX, use key of first item to update new added (let React to move exsiting
  55. // instead of remove and mount). Same key was used before for both a) external
  56. // manual control and b) internal react 'key' prop , which is not that good.
  57. // eslint-disable-next-line no-param-reassign
  58. // zombieJ: Not know why use `updateKey`. This makes Notice infinite loop in jest.
  59. // Change to `updateMark` for compare instead.
  60. // https://github.com/react-component/notification/commit/32299e6be396f94040bfa82517eea940db947ece
  61. notice.key = updatedNotices[0].notice.key;
  62. notice.updateMark = getUuid();
  63. // zombieJ: That's why. User may close by key directly.
  64. // We need record this but not re-render to avoid upper issue
  65. // https://github.com/react-component/notification/issues/129
  66. notice.userPassKey = key;
  67. updatedNotices.shift();
  68. }
  69. updatedNotices.push({
  70. notice: notice,
  71. holderCallback: holderCallback
  72. });
  73. }
  74. notices.value = updatedNotices;
  75. };
  76. var remove = function remove(removeKey) {
  77. notices.value = notices.value.filter(function (_ref2) {
  78. var _ref2$notice = _ref2.notice,
  79. key = _ref2$notice.key,
  80. userPassKey = _ref2$notice.userPassKey;
  81. var mergedKey = userPassKey || key;
  82. return mergedKey !== removeKey;
  83. });
  84. };
  85. expose({
  86. add: add,
  87. remove: remove,
  88. notices: notices
  89. });
  90. return function () {
  91. var _slots$closeIcon, _className;
  92. var prefixCls = props.prefixCls,
  93. _props$closeIcon = props.closeIcon,
  94. closeIcon = _props$closeIcon === void 0 ? (_slots$closeIcon = slots.closeIcon) === null || _slots$closeIcon === void 0 ? void 0 : _slots$closeIcon.call(slots, {
  95. prefixCls: prefixCls
  96. }) : _props$closeIcon;
  97. var noticeNodes = notices.value.map(function (_ref3, index) {
  98. var notice = _ref3.notice,
  99. holderCallback = _ref3.holderCallback;
  100. var updateMark = index === notices.value.length - 1 ? notice.updateMark : undefined;
  101. var key = notice.key,
  102. userPassKey = notice.userPassKey;
  103. var content = notice.content;
  104. var noticeProps = _objectSpread(_objectSpread(_objectSpread({
  105. prefixCls: prefixCls,
  106. closeIcon: typeof closeIcon === 'function' ? closeIcon({
  107. prefixCls: prefixCls
  108. }) : closeIcon
  109. }, notice), notice.props), {}, {
  110. key: key,
  111. noticeKey: userPassKey || key,
  112. updateMark: updateMark,
  113. onClose: function onClose(noticeKey) {
  114. var _notice$onClose;
  115. remove(noticeKey);
  116. (_notice$onClose = notice.onClose) === null || _notice$onClose === void 0 ? void 0 : _notice$onClose.call(notice);
  117. },
  118. onClick: notice.onClick
  119. });
  120. if (holderCallback) {
  121. return _createVNode("div", {
  122. "key": key,
  123. "class": "".concat(prefixCls, "-hook-holder"),
  124. "ref": function ref(div) {
  125. if (typeof key === 'undefined') {
  126. return;
  127. }
  128. if (div) {
  129. hookRefs.set(key, div);
  130. holderCallback(div, noticeProps);
  131. } else {
  132. hookRefs.delete(key);
  133. }
  134. }
  135. }, null);
  136. }
  137. return _createVNode(Notice, noticeProps, {
  138. default: function _default() {
  139. return [typeof content === 'function' ? content({
  140. prefixCls: prefixCls
  141. }) : content];
  142. }
  143. });
  144. });
  145. var className = (_className = {}, _defineProperty(_className, prefixCls, 1), _defineProperty(_className, attrs.class, !!attrs.class), _className);
  146. return _createVNode("div", {
  147. "class": className,
  148. "style": attrs.style || {
  149. top: '65px',
  150. left: '50%'
  151. }
  152. }, [_createVNode(TransitionGroup, _objectSpread({
  153. "tag": "div"
  154. }, transitionProps.value), {
  155. default: function _default() {
  156. return [noticeNodes];
  157. }
  158. })]);
  159. };
  160. }
  161. });
  162. Notification.newInstance = function newNotificationInstance(properties, callback) {
  163. var _ref4 = properties || {},
  164. _ref4$name = _ref4.name,
  165. name = _ref4$name === void 0 ? 'notification' : _ref4$name,
  166. getContainer = _ref4.getContainer,
  167. appContext = _ref4.appContext,
  168. customizePrefixCls = _ref4.prefixCls,
  169. customRootPrefixCls = _ref4.rootPrefixCls,
  170. customTransitionName = _ref4.transitionName,
  171. hasTransitionName = _ref4.hasTransitionName,
  172. props = _objectWithoutProperties(_ref4, _excluded);
  173. var div = document.createElement('div');
  174. if (getContainer) {
  175. var root = getContainer();
  176. root.appendChild(div);
  177. } else {
  178. document.body.appendChild(div);
  179. }
  180. var Wrapper = defineComponent({
  181. compatConfig: {
  182. MODE: 3
  183. },
  184. name: 'NotificationWrapper',
  185. setup: function setup(_props, _ref5) {
  186. var attrs = _ref5.attrs;
  187. var notiRef = ref();
  188. onMounted(function () {
  189. callback({
  190. notice: function notice(noticeProps) {
  191. var _notiRef$value;
  192. (_notiRef$value = notiRef.value) === null || _notiRef$value === void 0 ? void 0 : _notiRef$value.add(noticeProps);
  193. },
  194. removeNotice: function removeNotice(key) {
  195. var _notiRef$value2;
  196. (_notiRef$value2 = notiRef.value) === null || _notiRef$value2 === void 0 ? void 0 : _notiRef$value2.remove(key);
  197. },
  198. destroy: function destroy() {
  199. vueRender(null, div);
  200. if (div.parentNode) {
  201. div.parentNode.removeChild(div);
  202. }
  203. },
  204. component: notiRef
  205. });
  206. });
  207. return function () {
  208. var global = globalConfigForApi;
  209. var prefixCls = global.getPrefixCls(name, customizePrefixCls);
  210. var rootPrefixCls = global.getRootPrefixCls(customRootPrefixCls, prefixCls);
  211. var transitionName = hasTransitionName ? customTransitionName : "".concat(rootPrefixCls, "-").concat(customTransitionName);
  212. return _createVNode(ConfigProvider, _objectSpread(_objectSpread({}, global), {}, {
  213. "notUpdateGlobalConfig": true,
  214. "prefixCls": rootPrefixCls
  215. }), {
  216. default: function _default() {
  217. return [_createVNode(Notification, _objectSpread(_objectSpread({
  218. "ref": notiRef
  219. }, attrs), {}, {
  220. "prefixCls": prefixCls,
  221. "transitionName": transitionName
  222. }), null)];
  223. }
  224. });
  225. };
  226. }
  227. });
  228. var vm = createVNode(Wrapper, props);
  229. vm.appContext = appContext || vm.appContext;
  230. vueRender(vm, div);
  231. };
  232. export default Notification;