Toast.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default,
  21. toastProps: () => toastProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_utils = require("../utils");
  26. var import_lock_click = require("./lock-click");
  27. var import_icon = require("../icon");
  28. var import_popup = require("../popup");
  29. var import_loading = require("../loading");
  30. const [name, bem] = (0, import_utils.createNamespace)("toast");
  31. const popupInheritProps = ["show", "overlay", "teleport", "transition", "overlayClass", "overlayStyle", "closeOnClickOverlay", "zIndex"];
  32. const toastProps = {
  33. icon: String,
  34. show: Boolean,
  35. type: (0, import_utils.makeStringProp)("text"),
  36. overlay: Boolean,
  37. message: import_utils.numericProp,
  38. iconSize: import_utils.numericProp,
  39. duration: (0, import_utils.makeNumberProp)(2e3),
  40. position: (0, import_utils.makeStringProp)("middle"),
  41. teleport: [String, Object],
  42. wordBreak: String,
  43. className: import_utils.unknownProp,
  44. iconPrefix: String,
  45. transition: (0, import_utils.makeStringProp)("van-fade"),
  46. loadingType: String,
  47. forbidClick: Boolean,
  48. overlayClass: import_utils.unknownProp,
  49. overlayStyle: Object,
  50. closeOnClick: Boolean,
  51. closeOnClickOverlay: Boolean,
  52. zIndex: import_utils.numericProp
  53. };
  54. var stdin_default = (0, import_vue.defineComponent)({
  55. name,
  56. props: toastProps,
  57. emits: ["update:show"],
  58. setup(props, {
  59. emit,
  60. slots
  61. }) {
  62. let timer;
  63. let clickable = false;
  64. const toggleClickable = () => {
  65. const newValue = props.show && props.forbidClick;
  66. if (clickable !== newValue) {
  67. clickable = newValue;
  68. (0, import_lock_click.lockClick)(clickable);
  69. }
  70. };
  71. const updateShow = (show) => emit("update:show", show);
  72. const onClick = () => {
  73. if (props.closeOnClick) {
  74. updateShow(false);
  75. }
  76. };
  77. const clearTimer = () => clearTimeout(timer);
  78. const renderIcon = () => {
  79. const {
  80. icon,
  81. type,
  82. iconSize,
  83. iconPrefix,
  84. loadingType
  85. } = props;
  86. const hasIcon = icon || type === "success" || type === "fail";
  87. if (hasIcon) {
  88. return (0, import_vue.createVNode)(import_icon.Icon, {
  89. "name": icon || type,
  90. "size": iconSize,
  91. "class": bem("icon"),
  92. "classPrefix": iconPrefix
  93. }, null);
  94. }
  95. if (type === "loading") {
  96. return (0, import_vue.createVNode)(import_loading.Loading, {
  97. "class": bem("loading"),
  98. "size": iconSize,
  99. "type": loadingType
  100. }, null);
  101. }
  102. };
  103. const renderMessage = () => {
  104. const {
  105. type,
  106. message
  107. } = props;
  108. if (slots.message) {
  109. return (0, import_vue.createVNode)("div", {
  110. "class": bem("text")
  111. }, [slots.message()]);
  112. }
  113. if ((0, import_utils.isDef)(message) && message !== "") {
  114. return type === "html" ? (0, import_vue.createVNode)("div", {
  115. "key": 0,
  116. "class": bem("text"),
  117. "innerHTML": String(message)
  118. }, null) : (0, import_vue.createVNode)("div", {
  119. "class": bem("text")
  120. }, [message]);
  121. }
  122. };
  123. (0, import_vue.watch)(() => [props.show, props.forbidClick], toggleClickable);
  124. (0, import_vue.watch)(() => [props.show, props.type, props.message, props.duration], () => {
  125. clearTimer();
  126. if (props.show && props.duration > 0) {
  127. timer = setTimeout(() => {
  128. updateShow(false);
  129. }, props.duration);
  130. }
  131. });
  132. (0, import_vue.onMounted)(toggleClickable);
  133. (0, import_vue.onUnmounted)(toggleClickable);
  134. return () => (0, import_vue.createVNode)(import_popup.Popup, (0, import_vue.mergeProps)({
  135. "class": [bem([props.position, props.wordBreak === "normal" ? "break-normal" : props.wordBreak, {
  136. [props.type]: !props.icon
  137. }]), props.className],
  138. "lockScroll": false,
  139. "onClick": onClick,
  140. "onClosed": clearTimer,
  141. "onUpdate:show": updateShow
  142. }, (0, import_utils.pick)(props, popupInheritProps)), {
  143. default: () => [renderIcon(), renderMessage()]
  144. });
  145. }
  146. });