-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPagination.tsx
126 lines (112 loc) · 3.72 KB
/
Pagination.tsx
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
import { useMemo } from 'react';
import { FaChevronLeft, FaChevronRight } from 'react-icons/fa6';
type PaginationProps = {
page: number,
maxPage: number,
setPage: (n: number) => void
}
export default function Pagination(props: PaginationProps) {
const { page, maxPage, setPage } = props;
const visiblePages = 9;
function decrementPage() {
void setPage(page - 1);
}
function incrementPage() {
void setPage(page + 1);
}
// Calculate pagination parameters
// Code borrowed and modified from https://github.com/redpwn/rctf/blob/master/client/src/components/pagination.js
const { pages, startPage, endPage } = useMemo(() => {
// Follow the google pagination principle of always showing 10 items
let startPage, endPage;
if (maxPage <= visiblePages) {
// Display all
startPage = 1;
endPage = maxPage;
} else {
// We need to hide some pages
startPage = page - Math.ceil((visiblePages - 1) / 2)
endPage = page + Math.floor((visiblePages - 1) / 2)
if (startPage < 1) {
startPage = 1;
endPage = visiblePages;
} else if (endPage > maxPage) {
endPage = maxPage
startPage = maxPage - visiblePages + 1
}
if (startPage > 1) {
startPage += 2
}
if (endPage < maxPage) {
endPage -= 2
}
}
const pages = [] // ...Array((endPage + 1) - startPage).keys()].map(i => startPage + i)
for (let i = startPage; i <= endPage; i++) {
pages.push(i)
}
return { pages, startPage, endPage }
}, [maxPage, page, visiblePages]);
return (
<div className="flex gap-1.5 justify-center text-secondary mt-2">
<button
className="disabled:text-tertiary enabled:hover:text-primary transition duration-200 mr-2"
onClick={decrementPage}
disabled={page <= 0}
>
<FaChevronLeft />
</button>
{startPage > 1 && (
<>
<PaginationItem
page={page}
setPage={setPage}
i={1}
/>
<p className="text-secondary text-center w-9">...</p>
</>
)}
{pages.map((i) => (
<PaginationItem
page={page}
setPage={setPage}
i={i}
key={i}
/>
))}
{endPage < maxPage && (
<>
<p className="text-secondary text-center w-9">...</p>
<PaginationItem
page={page}
setPage={setPage}
i={maxPage}
/>
</>
)}
<button
className="disabled:text-tertiary enabled:hover:text-primary transition duration-200 ml-2"
onClick={incrementPage}
disabled={page >= maxPage - 1}
>
<FaChevronRight />
</button>
</div>
)
}
type PaginationItemProps = {
page: number,
setPage: (p: number) => void,
i: number
}
function PaginationItem(props: PaginationItemProps) {
const { page, setPage, i } = props;
return (
<button
className={'w-9 py-1.5 rounded transition duration-200 ' + (page === i - 1 ? 'bg-theme text-white' : 'hover:text-white')}
onClick={() => setPage(i - 1)}
>
{i}
</button>
)
}