-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-pagination.ts
44 lines (35 loc) · 1.35 KB
/
use-pagination.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
import { useCallback, useMemo, useState } from 'react';
export type PaginationConfigType = {
pageIndex?: number;
itemsPerPage?: number;
};
/**
* Simplify the pagination service in react
* @param {Array<DataType>} data The array data that is needed to paginate
* @param {PaginationConfigType | undefined} config Define page index and items per page
*/
export function usePagination<DataType>(data: Array<DataType>, config?: PaginationConfigType) {
const pageIndex = config?.pageIndex ?? 1;
const itemsPerPage = config?.itemsPerPage ?? 10;
const [currentPage, setCurrentPage] = useState(pageIndex);
const maxPage = useMemo(() => {
return Math.ceil(data.length / itemsPerPage);
}, [data.length, itemsPerPage]);
const _data = useMemo(() => {
return data.slice(currentPage * itemsPerPage - itemsPerPage, currentPage * itemsPerPage);
}, [currentPage, data, itemsPerPage]);
const next = useCallback(() => {
setCurrentPage((currentPage) => Math.min(currentPage + 1, maxPage));
}, [maxPage]);
const prev = useCallback(() => {
setCurrentPage((currentPage) => Math.max(currentPage - 1, 1));
}, []);
const jump = useCallback(
(page: number) => {
const pageNumber = Math.max(1, page);
setCurrentPage(Math.min(pageNumber, maxPage));
},
[maxPage],
);
return { next, prev, jump, data: _data, currentPage, maxPage };
}