List.js 5.5 KB

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