Optimize pagnation exp (#297)
This commit is contained in:
1 parent
3f8941312d
commit
b103bee710
3 files changed
+24
-6
No files matched your search
@@ -29,7 +29,7 @@ export default defineComponent({
|
||||
},
|
||||
reachThreshold: {
|
||||
type: Number,
|
||||
default: 10
|
||||
default: 40
|
||||
}
|
||||
},
|
||||
emits: ['reach-top', 'scroll'],
|
||||
|
||||
+16
-5
@@ -9,15 +9,26 @@ export async function loadPreviousPage(options: {
|
||||
tasks?: TasksLike;
|
||||
getTasks?: () => TasksLike | undefined;
|
||||
loading: boolean;
|
||||
ignoreLoadingGuard?: boolean;
|
||||
setLoading: (value: boolean) => void;
|
||||
isBlocked?: () => boolean;
|
||||
fetch: (createdAtMax: number) => Promise<any>;
|
||||
getScrollElement?: () => HTMLElement | undefined;
|
||||
reachThreshold?: number;
|
||||
}) {
|
||||
const { tasks, getTasks, loading, setLoading, isBlocked, fetch, getScrollElement, reachThreshold = 10 } = options;
|
||||
const {
|
||||
tasks,
|
||||
getTasks,
|
||||
loading,
|
||||
ignoreLoadingGuard,
|
||||
setLoading,
|
||||
isBlocked,
|
||||
fetch,
|
||||
getScrollElement,
|
||||
reachThreshold = 40
|
||||
} = options;
|
||||
const currentTasks = getTasks ? getTasks() : tasks;
|
||||
if (loading || isBlocked?.()) {
|
||||
if ((loading && !ignoreLoadingGuard) || isBlocked?.()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -43,18 +54,18 @@ export async function loadPreviousPage(options: {
|
||||
await nextTick();
|
||||
const latest = getTasks ? getTasks() : tasks;
|
||||
const newLength = latest?.items?.length || 0;
|
||||
const hasMore = latest?.total !== undefined ? newLength < (latest.total || 0) : false;
|
||||
const hasMore = latest?.total !== undefined ? newLength < (latest.total || 0) : newLength > previousLength;
|
||||
const scrollEl = getScrollElement?.();
|
||||
if (scrollEl) {
|
||||
const newHeight = scrollEl.scrollHeight;
|
||||
scrollEl.scrollTop = newHeight - previousHeight + previousScrollTop;
|
||||
}
|
||||
// If仍在顶部附近且还有更多数据,自动继续拉取,避免需要人为下滑再上滑。
|
||||
if (hasMore && newLength > previousLength && scrollEl && scrollEl.scrollTop <= reachThreshold && !isBlocked?.()) {
|
||||
await loadPreviousPage({
|
||||
...options,
|
||||
tasks: latest,
|
||||
loading: false
|
||||
loading: false,
|
||||
ignoreLoadingGuard: true
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
|
||||
Reference in new issue
Block a user