-
Notifications
You must be signed in to change notification settings - Fork 22
/
Pagination.tsx
118 lines (107 loc) · 2.56 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
import {
ArrowBackRounded,
ArrowForwardRounded,
Remove,
} from '@mui/icons-material'
import clsx from 'clsx'
import { useEffect } from 'react'
import { Button } from './buttons'
import { IconButton } from './icon_buttons'
import { Loader } from './logo'
export const PAGINATION_MIN_PAGE = 1
export type PaginationProps = {
total: number
page: number
setPage: (page: number) => void
pageSize: number
className?: string
/**
* Show loading indicator over current page.
*/
loading?: boolean
}
export const Pagination = ({
total,
page: _page,
setPage,
pageSize,
className,
loading,
}: PaginationProps) => {
const maxPage = Math.ceil(total / pageSize)
const page = Math.min(Math.max(PAGINATION_MIN_PAGE, _page), maxPage)
// If page is out of bounds, correct it.
useEffect(() => {
if (_page !== page) {
setPage(maxPage)
}
}, [_page, maxPage, page, setPage])
if (maxPage <= PAGINATION_MIN_PAGE) {
return null
}
return (
<div
className={clsx(
'flex max-w-sm flex-row items-center justify-between gap-4',
className
)}
>
<IconButton
Icon={ArrowBackRounded}
circular
disabled={page === PAGINATION_MIN_PAGE}
onClick={() => setPage(page - 1)}
size="sm"
variant="ghost"
/>
<Button
circular
className="text-lg"
disabled={page === PAGINATION_MIN_PAGE}
onClick={() => setPage(PAGINATION_MIN_PAGE)}
pressed={page === PAGINATION_MIN_PAGE}
size="sm"
variant="ghost"
>
{PAGINATION_MIN_PAGE}
</Button>
<div className="flex h-6 w-6 items-center justify-center">
{loading ? (
<Loader fill={false} size={22} />
) : // Show current page if not first or last.
page > PAGINATION_MIN_PAGE && page < maxPage ? (
<Button
className="text-lg"
disabled
pressed
size="sm"
variant="ghost"
>
{page}
</Button>
) : (
<Remove className="!h-5 !w-5" />
)}
</div>
<Button
circular
className="text-lg"
disabled={page === maxPage}
onClick={() => setPage(maxPage)}
pressed={page === maxPage}
size="sm"
variant="ghost"
>
{maxPage}
</Button>
<IconButton
Icon={ArrowForwardRounded}
circular
disabled={page === maxPage}
onClick={() => setPage(page + 1)}
size="sm"
variant="ghost"
/>
</div>
)
}