index.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = exports.alertProps = void 0;
  7. var _vue = require("vue");
  8. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  9. var _CloseOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/CloseOutlined"));
  10. var _CheckCircleOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/CheckCircleOutlined"));
  11. var _ExclamationCircleOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/ExclamationCircleOutlined"));
  12. var _InfoCircleOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/InfoCircleOutlined"));
  13. var _CloseCircleOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/CloseCircleOutlined"));
  14. var _CheckCircleFilled = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/CheckCircleFilled"));
  15. var _ExclamationCircleFilled = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/ExclamationCircleFilled"));
  16. var _InfoCircleFilled = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/InfoCircleFilled"));
  17. var _CloseCircleFilled = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/CloseCircleFilled"));
  18. var _classNames = _interopRequireDefault(require("../_util/classNames"));
  19. var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));
  20. var _transition = require("../_util/transition");
  21. var _propsUtil = require("../_util/props-util");
  22. var _type = require("../_util/type");
  23. var _vnode = require("../_util/vnode");
  24. var _useConfigInject = _interopRequireDefault(require("../config-provider/hooks/useConfigInject"));
  25. var _style = _interopRequireDefault(require("./style"));
  26. const iconMapFilled = {
  27. success: _CheckCircleFilled.default,
  28. info: _InfoCircleFilled.default,
  29. error: _CloseCircleFilled.default,
  30. warning: _ExclamationCircleFilled.default
  31. };
  32. const iconMapOutlined = {
  33. success: _CheckCircleOutlined.default,
  34. info: _InfoCircleOutlined.default,
  35. error: _CloseCircleOutlined.default,
  36. warning: _ExclamationCircleOutlined.default
  37. };
  38. const AlertTypes = (0, _type.tuple)('success', 'info', 'warning', 'error');
  39. const alertProps = () => ({
  40. /**
  41. * Type of Alert styles, options: `success`, `info`, `warning`, `error`
  42. */
  43. type: _vueTypes.default.oneOf(AlertTypes),
  44. /** Whether Alert can be closed */
  45. closable: {
  46. type: Boolean,
  47. default: undefined
  48. },
  49. /** Close text to show */
  50. closeText: _vueTypes.default.any,
  51. /** Content of Alert */
  52. message: _vueTypes.default.any,
  53. /** Additional content of Alert */
  54. description: _vueTypes.default.any,
  55. /** Trigger when animation ending of Alert */
  56. afterClose: Function,
  57. /** Whether to show icon */
  58. showIcon: {
  59. type: Boolean,
  60. default: undefined
  61. },
  62. prefixCls: String,
  63. banner: {
  64. type: Boolean,
  65. default: undefined
  66. },
  67. icon: _vueTypes.default.any,
  68. closeIcon: _vueTypes.default.any,
  69. onClose: Function
  70. });
  71. exports.alertProps = alertProps;
  72. const Alert = (0, _vue.defineComponent)({
  73. compatConfig: {
  74. MODE: 3
  75. },
  76. name: 'AAlert',
  77. inheritAttrs: false,
  78. props: alertProps(),
  79. setup(props, _ref) {
  80. let {
  81. slots,
  82. emit,
  83. attrs,
  84. expose
  85. } = _ref;
  86. const {
  87. prefixCls,
  88. direction
  89. } = (0, _useConfigInject.default)('alert', props);
  90. const [wrapSSR, hashId] = (0, _style.default)(prefixCls);
  91. const closing = (0, _vue.shallowRef)(false);
  92. const closed = (0, _vue.shallowRef)(false);
  93. const alertNode = (0, _vue.shallowRef)();
  94. const handleClose = e => {
  95. e.preventDefault();
  96. const dom = alertNode.value;
  97. dom.style.height = `${dom.offsetHeight}px`;
  98. // Magic code
  99. // 重复一次后才能正确设置 height
  100. dom.style.height = `${dom.offsetHeight}px`;
  101. closing.value = true;
  102. emit('close', e);
  103. };
  104. const animationEnd = () => {
  105. var _a;
  106. closing.value = false;
  107. closed.value = true;
  108. (_a = props.afterClose) === null || _a === void 0 ? void 0 : _a.call(props);
  109. };
  110. const mergedType = (0, _vue.computed)(() => {
  111. const {
  112. type
  113. } = props;
  114. if (type !== undefined) {
  115. return type;
  116. }
  117. // banner 模式默认为警告
  118. return props.banner ? 'warning' : 'info';
  119. });
  120. expose({
  121. animationEnd
  122. });
  123. const motionStyle = (0, _vue.shallowRef)({});
  124. return () => {
  125. var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
  126. const {
  127. banner,
  128. closeIcon: customCloseIcon = (_a = slots.closeIcon) === null || _a === void 0 ? void 0 : _a.call(slots)
  129. } = props;
  130. let {
  131. closable,
  132. showIcon
  133. } = props;
  134. const closeText = (_b = props.closeText) !== null && _b !== void 0 ? _b : (_c = slots.closeText) === null || _c === void 0 ? void 0 : _c.call(slots);
  135. const description = (_d = props.description) !== null && _d !== void 0 ? _d : (_e = slots.description) === null || _e === void 0 ? void 0 : _e.call(slots);
  136. const message = (_f = props.message) !== null && _f !== void 0 ? _f : (_g = slots.message) === null || _g === void 0 ? void 0 : _g.call(slots);
  137. const icon = (_h = props.icon) !== null && _h !== void 0 ? _h : (_j = slots.icon) === null || _j === void 0 ? void 0 : _j.call(slots);
  138. const action = (_k = slots.action) === null || _k === void 0 ? void 0 : _k.call(slots);
  139. // banner模式默认有 Icon
  140. showIcon = banner && showIcon === undefined ? true : showIcon;
  141. const IconType = (description ? iconMapOutlined : iconMapFilled)[mergedType.value] || null;
  142. // closeable when closeText is assigned
  143. if (closeText) {
  144. closable = true;
  145. }
  146. const prefixClsValue = prefixCls.value;
  147. const alertCls = (0, _classNames.default)(prefixClsValue, {
  148. [`${prefixClsValue}-${mergedType.value}`]: true,
  149. [`${prefixClsValue}-closing`]: closing.value,
  150. [`${prefixClsValue}-with-description`]: !!description,
  151. [`${prefixClsValue}-no-icon`]: !showIcon,
  152. [`${prefixClsValue}-banner`]: !!banner,
  153. [`${prefixClsValue}-closable`]: closable,
  154. [`${prefixClsValue}-rtl`]: direction.value === 'rtl',
  155. [hashId.value]: true
  156. });
  157. const closeIcon = closable ? (0, _vue.createVNode)("button", {
  158. "type": "button",
  159. "onClick": handleClose,
  160. "class": `${prefixClsValue}-close-icon`,
  161. "tabindex": 0
  162. }, [closeText ? (0, _vue.createVNode)("span", {
  163. "class": `${prefixClsValue}-close-text`
  164. }, [closeText]) : customCloseIcon === undefined ? (0, _vue.createVNode)(_CloseOutlined.default, null, null) : customCloseIcon]) : null;
  165. const iconNode = icon && ((0, _propsUtil.isValidElement)(icon) ? (0, _vnode.cloneElement)(icon, {
  166. class: `${prefixClsValue}-icon`
  167. }) : (0, _vue.createVNode)("span", {
  168. "class": `${prefixClsValue}-icon`
  169. }, [icon])) || (0, _vue.createVNode)(IconType, {
  170. "class": `${prefixClsValue}-icon`
  171. }, null);
  172. const transitionProps = (0, _transition.getTransitionProps)(`${prefixClsValue}-motion`, {
  173. appear: false,
  174. css: true,
  175. onAfterLeave: animationEnd,
  176. onBeforeLeave: node => {
  177. node.style.maxHeight = `${node.offsetHeight}px`;
  178. },
  179. onLeave: node => {
  180. node.style.maxHeight = '0px';
  181. }
  182. });
  183. return wrapSSR(closed.value ? null : (0, _vue.createVNode)(_transition.Transition, transitionProps, {
  184. default: () => [(0, _vue.withDirectives)((0, _vue.createVNode)("div", (0, _objectSpread2.default)((0, _objectSpread2.default)({
  185. "role": "alert"
  186. }, attrs), {}, {
  187. "style": [attrs.style, motionStyle.value],
  188. "class": [attrs.class, alertCls],
  189. "data-show": !closing.value,
  190. "ref": alertNode
  191. }), [showIcon ? iconNode : null, (0, _vue.createVNode)("div", {
  192. "class": `${prefixClsValue}-content`
  193. }, [message ? (0, _vue.createVNode)("div", {
  194. "class": `${prefixClsValue}-message`
  195. }, [message]) : null, description ? (0, _vue.createVNode)("div", {
  196. "class": `${prefixClsValue}-description`
  197. }, [description]) : null]), action ? (0, _vue.createVNode)("div", {
  198. "class": `${prefixClsValue}-action`
  199. }, [action]) : null, closeIcon]), [[_vue.vShow, !closing.value]])]
  200. }));
  201. };
  202. }
  203. });
  204. var _default = exports.default = (0, _type.withInstall)(Alert);