-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathusePagination.ts
145 lines (128 loc) · 3.76 KB
/
usePagination.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import type { ComputedRef, WritableComputedRef } from 'vue-demi';
import { computed, inject } from 'vue-demi';
import { getGlobalOptions, GLOBAL_OPTIONS_PROVIDE_KEY } from './core/config';
import type {
GlobalOptions,
Options,
QueryResult,
Service,
} from './core/types';
import { get } from './core/utils';
import { merge } from './core/utils/lodash';
import useRequest from './useRequest';
interface PaginationType {
currentKey: string;
pageSizeKey: string;
totalKey: string;
totalPageKey: string;
}
export interface PaginationExtendsOption {
pagination?: Partial<PaginationType>;
}
export interface PaginationOptions<R, P extends unknown[]>
extends Options<R, P>,
PaginationExtendsOption {}
export interface PaginationQueryResult<R, P extends unknown[]>
extends QueryResult<R, P> {
current: WritableComputedRef<number>;
pageSize: WritableComputedRef<number>;
total: ComputedRef<number>;
totalPage: ComputedRef<number>;
changeCurrent: (current: number) => void;
changePageSize: (pageSize: number) => void;
changePagination: (current: number, pageSize: number) => void;
}
function usePagination<R, P extends unknown[] = any>(
service: Service<R, P>,
options: PaginationOptions<R, P> = {},
): PaginationQueryResult<R, P> {
const defaultPaginationOptions = {
currentKey: 'current',
pageSizeKey: 'pageSize',
totalKey: 'total',
totalPageKey: 'totalPage',
};
const injectedGlobalOptions = inject<GlobalOptions>(
GLOBAL_OPTIONS_PROVIDE_KEY,
{},
);
const { pagination, ...restOptions } = options;
const { currentKey, pageSizeKey, totalKey, totalPageKey } = merge(
defaultPaginationOptions,
getGlobalOptions().pagination || {},
injectedGlobalOptions?.pagination || {},
pagination || {},
) as PaginationType;
const finallyOptions = merge(
{
defaultParams: [
{
[currentKey]: 1,
[pageSizeKey]: 10,
},
] as any,
},
restOptions,
) as any;
const { data, params, run, ...rest } = useRequest<R, P>(
service,
finallyOptions,
);
const paging = (paginationParams: Record<string, number>) => {
const [oldPaginationParams, ...restParams] = (params.value as P[]) || [];
const newPaginationParams = {
...oldPaginationParams,
...paginationParams,
};
const mergerParams = [newPaginationParams, ...restParams] as any;
run(...mergerParams);
};
// changeCurrent change current page (current: number) => void
const changeCurrent = (current: number) => {
paging({ [currentKey]: current });
};
// changePageSize change pageSize (pageSize: number) => void
const changePageSize = (pageSize: number) => {
paging({ [pageSizeKey]: pageSize });
};
// changePagination change current and pageSize (current: number, pageSize: number) => void
const changePagination = (current: number, pageSize: number) => {
paging({ [currentKey]: current, [pageSizeKey]: pageSize });
};
const total = computed<number>(() => get(data.value!, totalKey, 0));
const current = computed({
get: () =>
// @ts-ignore
params.value?.[0]?.[currentKey] ??
finallyOptions.defaultParams[0][currentKey],
set: (val: number) => {
changeCurrent(val);
},
});
const pageSize = computed<number>({
get: () =>
// @ts-ignore
params.value?.[0]?.[pageSizeKey] ??
finallyOptions.defaultParams[0][pageSizeKey],
set: (val: number) => {
changePageSize(val);
},
});
const totalPage = computed<number>(() =>
get(data.value!, totalPageKey, Math.ceil(total.value / pageSize.value)),
);
return {
data,
params,
current,
pageSize,
total,
totalPage,
run,
changeCurrent,
changePageSize,
changePagination,
...rest,
};
}
export default usePagination;