Toast.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_vue = require("vue");
  24. var import_vue2 = 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"];
  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. className: import_utils.unknownProp,
  43. iconPrefix: String,
  44. transition: (0, import_utils.makeStringProp)("van-fade"),
  45. loadingType: String,
  46. forbidClick: Boolean,
  47. overlayClass: import_utils.unknownProp,
  48. overlayStyle: Object,
  49. closeOnClick: Boolean,
  50. closeOnClickOverlay: Boolean
  51. };
  52. var stdin_default = (0, import_vue2.defineComponent)({
  53. name,
  54. props: toastProps,
  55. emits: ["update:show"],
  56. setup(props, {
  57. emit
  58. }) {
  59. let timer;
  60. let clickable = false;
  61. const toggleClickable = () => {
  62. const newValue = props.show && props.forbidClick;
  63. if (clickable !== newValue) {
  64. clickable = newValue;
  65. (0, import_lock_click.lockClick)(clickable);
  66. }
  67. };
  68. const updateShow = (show) => emit("update:show", show);
  69. const onClick = () => {
  70. if (props.closeOnClick) {
  71. updateShow(false);
  72. }
  73. };
  74. const clearTimer = () => clearTimeout(timer);
  75. const renderIcon = () => {
  76. const {
  77. icon,
  78. type,
  79. iconSize,
  80. iconPrefix,
  81. loadingType
  82. } = props;
  83. const hasIcon = icon || type === "success" || type === "fail";
  84. if (hasIcon) {
  85. return (0, import_vue.createVNode)(import_icon.Icon, {
  86. "name": icon || type,
  87. "size": iconSize,
  88. "class": bem("icon"),
  89. "classPrefix": iconPrefix
  90. }, null);
  91. }
  92. if (type === "loading") {
  93. return (0, import_vue.createVNode)(import_loading.Loading, {
  94. "class": bem("loading"),
  95. "size": iconSize,
  96. "type": loadingType
  97. }, null);
  98. }
  99. };
  100. const renderMessage = () => {
  101. const {
  102. type,
  103. message
  104. } = props;
  105. if ((0, import_utils.isDef)(message) && message !== "") {
  106. return type === "html" ? (0, import_vue.createVNode)("div", {
  107. "key": 0,
  108. "class": bem("text"),
  109. "innerHTML": String(message)
  110. }, null) : (0, import_vue.createVNode)("div", {
  111. "class": bem("text")
  112. }, [message]);
  113. }
  114. };
  115. (0, import_vue2.watch)(() => [props.show, props.forbidClick], toggleClickable);
  116. (0, import_vue2.watch)(() => [props.show, props.type, props.message, props.duration], () => {
  117. clearTimer();
  118. if (props.show && props.duration > 0) {
  119. timer = setTimeout(() => {
  120. updateShow(false);
  121. }, props.duration);
  122. }
  123. });
  124. (0, import_vue2.onMounted)(toggleClickable);
  125. (0, import_vue2.onUnmounted)(toggleClickable);
  126. return () => (0, import_vue.createVNode)(import_popup.Popup, (0, import_vue.mergeProps)({
  127. "class": [bem([props.position, {
  128. [props.type]: !props.icon
  129. }]), props.className],
  130. "lockScroll": false,
  131. "onClick": onClick,
  132. "onClosed": clearTimer,
  133. "onUpdate:show": updateShow
  134. }, (0, import_utils.pick)(props, popupInheritProps)), {
  135. default: () => [renderIcon(), renderMessage()]
  136. });
  137. }
  138. });