-
Notifications
You must be signed in to change notification settings - Fork 3
/
pagination-with-links.tsx
201 lines (184 loc) · 5.55 KB
/
pagination-with-links.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
"use client";
import { type ReactNode, useCallback } from "react";
import {
Pagination,
PaginationContent,
PaginationEllipsis,
PaginationItem,
PaginationLink,
PaginationNext,
PaginationPrevious,
} from "./pagination";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./select";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { cn } from "@/lib/utils";
export interface PaginationWithLinksProps {
pageSizeSelectOptions?: {
pageSizeSearchParam?: string;
pageSizeOptions: number[];
};
totalCount: number;
pageSize: number;
page: number;
pageSearchParam?: string;
}
/**
* Navigate with Nextjs links (need to update your own `pagination.tsx` to use Nextjs Link)
*
* @example
* ```
* <PaginationWithLinks
page={1}
pageSize={20}
totalCount={500}
/>
* ```
*/
export function PaginationWithLinks({
pageSizeSelectOptions,
pageSize,
totalCount,
page,
pageSearchParam,
}: PaginationWithLinksProps) {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
const totalPageCount = Math.ceil(totalCount / pageSize);
const buildLink = useCallback(
(newPage: number) => {
const key = pageSearchParam || "page";
if (!searchParams) return `${pathname}?${key}=${newPage}`;
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.set(key, String(newPage));
return `${pathname}?${newSearchParams.toString()}`;
},
[searchParams, pathname],
);
const navToPageSize = useCallback(
(newPageSize: number) => {
const key = pageSizeSelectOptions?.pageSizeSearchParam || "pageSize";
const newSearchParams = new URLSearchParams(searchParams || undefined);
newSearchParams.set(key, String(newPageSize));
router.push(`${pathname}?${newSearchParams.toString()}`);
},
[searchParams, pathname],
);
const renderPageNumbers = () => {
const items: ReactNode[] = [];
const maxVisiblePages = 5;
if (totalPageCount <= maxVisiblePages) {
for (let i = 1; i <= totalPageCount; i++) {
items.push(
<PaginationItem key={i}>
<PaginationLink href={buildLink(i)} isActive={page === i}>
{i}
</PaginationLink>
</PaginationItem>,
);
}
} else {
items.push(
<PaginationItem key={1}>
<PaginationLink href={buildLink(1)} isActive={page === 1}>
1
</PaginationLink>
</PaginationItem>,
);
if (page > 3) {
items.push(
<PaginationItem key="ellipsis-start">
<PaginationEllipsis />
</PaginationItem>,
);
}
const start = Math.max(2, page - 1);
const end = Math.min(totalPageCount - 1, page + 1);
for (let i = start; i <= end; i++) {
items.push(
<PaginationItem key={i}>
<PaginationLink href={buildLink(i)} isActive={page === i}>
{i}
</PaginationLink>
</PaginationItem>,
);
}
if (page < totalPageCount - 2) {
items.push(
<PaginationItem key="ellipsis-end">
<PaginationEllipsis />
</PaginationItem>,
);
}
items.push(
<PaginationItem key={totalPageCount}>
<PaginationLink href={buildLink(totalPageCount)} isActive={page === totalPageCount}>
{totalPageCount}
</PaginationLink>
</PaginationItem>,
);
}
return items;
};
return (
<div className="flex flex-col md:flex-row items-center gap-3 w-full">
{pageSizeSelectOptions && (
<div className="flex flex-col gap-4 flex-1">
<SelectRowsPerPage
options={pageSizeSelectOptions.pageSizeOptions}
setPageSize={navToPageSize}
pageSize={pageSize}
/>
</div>
)}
<Pagination className={cn({ "md:justify-end": pageSizeSelectOptions })}>
<PaginationContent className="max-sm:gap-0">
<PaginationItem>
<PaginationPrevious
href={buildLink(Math.max(page - 1, 1))}
aria-disabled={page === 1}
tabIndex={page === 1 ? -1 : undefined}
className={page === 1 ? "pointer-events-none opacity-50" : undefined}
/>
</PaginationItem>
{renderPageNumbers()}
<PaginationItem>
<PaginationNext
href={buildLink(Math.min(page + 1, totalPageCount))}
aria-disabled={page === totalPageCount}
tabIndex={page === totalPageCount ? -1 : undefined}
className={page === totalPageCount ? "pointer-events-none opacity-50" : undefined}
/>
</PaginationItem>
</PaginationContent>
</Pagination>
</div>
);
}
function SelectRowsPerPage({
options,
setPageSize,
pageSize,
}: {
options: number[];
setPageSize: (newSize: number) => void;
pageSize: number;
}) {
return (
<div className="flex items-center gap-4">
<span className="whitespace-nowrap text-sm">Rows per page</span>
<Select value={String(pageSize)} onValueChange={(value) => setPageSize(Number(value))}>
<SelectTrigger>
<SelectValue placeholder="Select page size">{String(pageSize)}</SelectValue>
</SelectTrigger>
<SelectContent>
{options.map((option) => (
<SelectItem key={option} value={String(option)}>
{option}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
);
}