添加列表刷新组件

This commit is contained in:
icssoa
2025-08-11 09:54:23 +08:00
parent 30147ed73a
commit 4f2d437ef8
13 changed files with 482 additions and 108 deletions

View File

@@ -1,5 +1,6 @@
export * from "./refs";
export * from "./page";
export * from "./pager";
export * from "./long-press";
export * from "./cache";
export * from "./parent";

94
cool/hooks/pager.ts Normal file
View File

@@ -0,0 +1,94 @@
import { ref } from "vue";
import { assign, parse } from "../utils";
import type { Response } from "../service";
type Pagination = {
page: number;
size: number;
total: number;
};
type PagerResponse = {
list: UTSJSONObject[];
pagination: Pagination;
};
type PagerCallback = (params: UTSJSONObject) => Promise<UTSJSONObject>;
export class Pager {
public page = 1;
public size = 20;
public total = 0;
public list = ref<UTSJSONObject[]>([]);
public loading = ref(false);
public refreshing = ref(false);
public finished = ref(false);
public params = {} as UTSJSONObject;
public cb: PagerCallback | null = null;
constructor(cb: PagerCallback) {
this.cb = cb;
}
done() {
this.loading.value = false;
}
clear() {
this.list.value = [];
this.finished.value = false;
this.refreshing.value = false;
this.loading.value = false;
}
public refresh = async (params: UTSJSONObject): Promise<UTSJSONObject> => {
return new Promise((resolve, reject) => {
assign(this.params, params);
const data = {
page: this.page,
size: this.size,
...this.params
};
this.loading.value = true;
this.cb!(data)
.then((res) => {
const { list, pagination } = parse<PagerResponse>(res)!;
this.page = pagination.page;
this.size = pagination.size;
this.total = pagination.total;
this.finished.value = this.list.value.length >= this.total;
if (data.page == 1) {
this.list.value = list;
} else {
this.list.value.push(...list);
}
resolve(res);
})
.catch((err) => {
reject(err);
})
.finally(() => {
this.loading.value = false;
});
});
};
public loadMore = () => {
if (this.loading.value || this.finished.value) {
return;
}
this.page += 1;
this.refresh({});
};
}
export function usePager(cb: PagerCallback) {
return new Pager(cb);
}