-
Notifications
You must be signed in to change notification settings - Fork 150
/
Pagination.tsx
216 lines (192 loc) 路 5.93 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import React, { useContext, useEffect, useState } from 'react'
import Button, { ButtonAsButtonProps } from './Button'
import { ThemeContext } from './context/ThemeContext'
const PrevIcon: React.FC = function PrevIcon(props) {
return (
<svg {...props} aria-hidden="true" fill="currentColor" viewBox="0 0 20 20">
<path
d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z"
clipRule="evenodd"
fillRule="evenodd"
></path>
</svg>
)
}
const NextIcon: React.FC = function NextIcon(props) {
return (
<svg {...props} aria-hidden="true" fill="currentColor" viewBox="0 0 20 20">
<path
d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z"
clipRule="evenodd"
fillRule="evenodd"
></path>
</svg>
)
}
interface NavigationButtonProps extends ButtonAsButtonProps {
directionIcon: 'prev' | 'next'
}
export const NavigationButton: React.FC<NavigationButtonProps> = function NavigationButton({
onClick,
disabled,
directionIcon,
}) {
const ariaLabel = directionIcon === 'prev' ? 'Previous' : 'Next'
const icon = directionIcon === 'prev' ? PrevIcon : NextIcon
return (
<Button
size="small"
layout="link"
icon={icon}
onClick={onClick}
disabled={disabled}
aria-label={ariaLabel}
/>
)
}
interface PageButtonProps extends ButtonAsButtonProps {
page: string | number
isActive?: boolean
}
export const PageButton: React.FC<PageButtonProps> = function PageButton({
page,
isActive,
onClick,
}) {
return (
<Button size="pagination" layout={isActive ? 'primary' : 'link'} onClick={onClick}>
{page}
</Button>
)
}
export const EmptyPageButton = () => <span className="px-2 py-1">...</span>
export interface CurrentPageTextParams {
pageStart: number,
pageEnd: number,
totalResults: number
}
const defaultCurrentPageText = ({ pageStart, pageEnd, totalResults }: CurrentPageTextParams): string => {
return [
'Showing ',
pageStart,
'-',
pageEnd,
' of ',
totalResults
].join('')
}
interface PaginationProps {
totalResults: number
resultsPerPage?: number
label: string
currentPageText?: (params: CurrentPageTextParams) => string,
onChange: (activePage: number) => void
}
type Ref = HTMLDivElement
const Pagination = React.forwardRef<Ref, PaginationProps>(function Pagination(props, ref) {
const { totalResults, resultsPerPage = 10, label, onChange, currentPageText = defaultCurrentPageText, ...other } = props
const [pages, setPages] = useState<(number | string)[]>([])
const [activePage, setActivePage] = useState(1)
const TOTAL_PAGES = Math.ceil(totalResults / resultsPerPage)
const FIRST_PAGE = 1
const LAST_PAGE = TOTAL_PAGES
const MAX_VISIBLE_PAGES = 7
function handlePreviousClick() {
setActivePage(activePage - 1)
}
function handleNextClick() {
setActivePage(activePage + 1)
}
useEffect(() => {
// [1], 2, 3, 4, 5, ..., 12 case #1
// 1, [2], 3, 4, 5, ..., 12
// 1, 2, [3], 4, 5, ..., 12
// 1, 2, 3, [4], 5, ..., 12
// 1, ..., 4, [5], 6, ..., 12 case #2
// 1, ..., 5, [6], 7, ..., 12
// 1, ..., 6, [7], 8, ..., 12
// 1, ..., 7, [8], 9, ..., 12
// 1, ..., 8, [9], 10, 11, 12 case #3
// 1, ..., 8, 9, [10], 11, 12
// 1, ..., 8, 9, 10, [11], 12
// 1, ..., 8, 9, 10, 11, [12]
// [1], 2, 3, 4, 5, ..., 8
// always show first and last
// max of 7 pages shown (incl. [...])
if (TOTAL_PAGES <= MAX_VISIBLE_PAGES) {
setPages(Array.from({ length: TOTAL_PAGES }).map((_, i) => i + 1))
} else if (activePage < 5) {
// #1 active page < 5 -> show first 5
setPages([1, 2, 3, 4, 5, '...', TOTAL_PAGES])
} else if (activePage >= 5 && activePage < TOTAL_PAGES - 3) {
// #2 active page >= 5 && < TOTAL_PAGES - 3
setPages([1, '...', activePage - 1, activePage, activePage + 1, '...', TOTAL_PAGES])
} else {
// #3 active page >= TOTAL_PAGES - 3 -> show last
setPages([
1,
'...',
TOTAL_PAGES - 4,
TOTAL_PAGES - 3,
TOTAL_PAGES - 2,
TOTAL_PAGES - 1,
TOTAL_PAGES,
])
}
}, [activePage, TOTAL_PAGES])
useEffect(() => {
onChange(activePage)
}, [activePage])
const {
theme: { pagination },
} = useContext(ThemeContext)
const baseStyle = pagination.base
return (
<div className={baseStyle} ref={ref} {...other}>
{/*
* This (label) should probably be an option, and not the default
*/}
<span data-testid="current-page-text" className="flex items-center font-semibold tracking-wide uppercase">
{currentPageText({
pageStart: activePage * resultsPerPage - resultsPerPage + 1,
pageEnd: Math.min(activePage * resultsPerPage, totalResults),
totalResults,
})}
</span>
<div className="flex mt-2 sm:mt-auto sm:justify-end">
<nav aria-label={label}>
<ul className="inline-flex items-center">
<li>
<NavigationButton
directionIcon="prev"
disabled={activePage === FIRST_PAGE}
onClick={handlePreviousClick}
/>
</li>
{pages.map((p, i) => (
<li key={p.toString() + i}>
{p === '...' ? (
<EmptyPageButton />
) : (
<PageButton
page={p}
isActive={p === activePage}
onClick={() => setActivePage(+p)}
/>
)}
</li>
))}
<li>
<NavigationButton
directionIcon="next"
disabled={activePage === LAST_PAGE}
onClick={handleNextClick}
/>
</li>
</ul>
</nav>
</div>
</div>
)
})
export default Pagination