List.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. listProps: () => listProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_vue2 = require("vue");
  26. var import_utils = require("../utils");
  27. var import_use = require("@vant/use");
  28. var import_use_expose = require("../composables/use-expose");
  29. var import_use_tab_status = require("../composables/use-tab-status");
  30. var import_loading = require("../loading");
  31. const [name, bem, t] = (0, import_utils.createNamespace)("list");
  32. const listProps = {
  33. error: Boolean,
  34. offset: (0, import_utils.makeNumericProp)(300),
  35. loading: Boolean,
  36. disabled: Boolean,
  37. finished: Boolean,
  38. errorText: String,
  39. direction: (0, import_utils.makeStringProp)("down"),
  40. loadingText: String,
  41. finishedText: String,
  42. immediateCheck: import_utils.truthProp
  43. };
  44. var stdin_default = (0, import_vue2.defineComponent)({
  45. name,
  46. props: listProps,
  47. emits: ["load", "update:error", "update:loading"],
  48. setup(props, {
  49. emit,
  50. slots
  51. }) {
  52. const loading = (0, import_vue2.ref)(props.loading);
  53. const root = (0, import_vue2.ref)();
  54. const placeholder = (0, import_vue2.ref)();
  55. const tabStatus = (0, import_use_tab_status.useTabStatus)();
  56. const scrollParent = (0, import_use.useScrollParent)(root);
  57. const check = () => {
  58. (0, import_vue2.nextTick)(() => {
  59. if (loading.value || props.finished || props.disabled || props.error || (tabStatus == null ? void 0 : tabStatus.value) === false) {
  60. return;
  61. }
  62. const {
  63. offset,
  64. direction
  65. } = props;
  66. const scrollParentRect = (0, import_use.useRect)(scrollParent);
  67. if (!scrollParentRect.height || (0, import_utils.isHidden)(root)) {
  68. return;
  69. }
  70. let isReachEdge = false;
  71. const placeholderRect = (0, import_use.useRect)(placeholder);
  72. if (direction === "up") {
  73. isReachEdge = scrollParentRect.top - placeholderRect.top <= offset;
  74. } else {
  75. isReachEdge = placeholderRect.bottom - scrollParentRect.bottom <= offset;
  76. }
  77. if (isReachEdge) {
  78. loading.value = true;
  79. emit("update:loading", true);
  80. emit("load");
  81. }
  82. });
  83. };
  84. const renderFinishedText = () => {
  85. if (props.finished) {
  86. const text = slots.finished ? slots.finished() : props.finishedText;
  87. if (text) {
  88. return (0, import_vue.createVNode)("div", {
  89. "class": bem("finished-text")
  90. }, [text]);
  91. }
  92. }
  93. };
  94. const clickErrorText = () => {
  95. emit("update:error", false);
  96. check();
  97. };
  98. const renderErrorText = () => {
  99. if (props.error) {
  100. const text = slots.error ? slots.error() : props.errorText;
  101. if (text) {
  102. return (0, import_vue.createVNode)("div", {
  103. "role": "button",
  104. "class": bem("error-text"),
  105. "tabindex": 0,
  106. "onClick": clickErrorText
  107. }, [text]);
  108. }
  109. }
  110. };
  111. const renderLoading = () => {
  112. if (loading.value && !props.finished && !props.disabled) {
  113. return (0, import_vue.createVNode)("div", {
  114. "class": bem("loading")
  115. }, [slots.loading ? slots.loading() : (0, import_vue.createVNode)(import_loading.Loading, {
  116. "class": bem("loading-icon")
  117. }, {
  118. default: () => [props.loadingText || t("loading")]
  119. })]);
  120. }
  121. };
  122. (0, import_vue2.watch)(() => [props.loading, props.finished, props.error], check);
  123. if (tabStatus) {
  124. (0, import_vue2.watch)(tabStatus, (tabActive) => {
  125. if (tabActive) {
  126. check();
  127. }
  128. });
  129. }
  130. (0, import_vue2.onUpdated)(() => {
  131. loading.value = props.loading;
  132. });
  133. (0, import_vue2.onMounted)(() => {
  134. if (props.immediateCheck) {
  135. check();
  136. }
  137. });
  138. (0, import_use_expose.useExpose)({
  139. check
  140. });
  141. (0, import_use.useEventListener)("scroll", check, {
  142. target: scrollParent,
  143. passive: true
  144. });
  145. return () => {
  146. var _a;
  147. const Content = (_a = slots.default) == null ? void 0 : _a.call(slots);
  148. const Placeholder = (0, import_vue.createVNode)("div", {
  149. "ref": placeholder,
  150. "class": bem("placeholder")
  151. }, null);
  152. return (0, import_vue.createVNode)("div", {
  153. "ref": root,
  154. "role": "feed",
  155. "class": bem(),
  156. "aria-busy": loading.value
  157. }, [props.direction === "down" ? Content : Placeholder, renderLoading(), renderFinishedText(), renderErrorText(), props.direction === "up" ? Content : Placeholder]);
  158. };
  159. }
  160. });