-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPagination.tsx
More file actions
129 lines (116 loc) · 3.29 KB
/
Pagination.tsx
File metadata and controls
129 lines (116 loc) · 3.29 KB
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
import { Button } from '../Button';
interface Link {
label: string;
url: string | null;
active: boolean;
page: number | null;
}
interface Pagination<T = Record<string, unknown>> {
data: T[];
links: Link[];
current_page: number;
last_page: number;
first_page_url?: string;
last_page_url?: string;
next_page_url?: string | null;
prev_page_url?: string | null;
path?: string;
per_page: number;
total: number;
from: number;
to: number;
}
interface PaginationProps<T = Record<string, unknown>> {
data: Pagination<T>;
align?: 'start' | 'center' | 'end';
showInfo?: boolean;
buttonStyle?: 'fill' | 'outline' | 'link';
paginationButtonAs?: 'a' | 'button';
}
export function Pagination<T = Record<string, unknown>>({
data,
align = 'end',
showInfo = false,
buttonStyle = 'fill',
paginationButtonAs = 'a',
}: PaginationProps<T>) {
if (!data || (!data.next_page_url && !data.prev_page_url)) return null;
const {
prev_page_url,
next_page_url,
from,
to,
total,
links,
} = data;
const pageLinks = links.filter(
link =>
!link.label.includes('«') &&
!link.label.includes('»') &&
link.label !== '...',
);
const getAlignStyle = () => {
if (align === 'end') return 'md:justify-end';
if (align === 'start') return 'md:justify-start';
if (align === 'center') return 'md:justify-center';
return 'md:justify-end';
};
const Info = ({ showInfo }: { showInfo: boolean }) => {
if (!showInfo) return <></>;
return (
<div className="mb-2 w-full text-center text-sm text-gray-600 sm:mb-0 md:text-left" aria-live="polite">
Showing {from} to {to} of {total} entries
</div>
);
};
return (
<div className="flex w-full flex-col items-center justify-center md:flex-row md:justify-between">
<Info showInfo={showInfo} />
<nav
className={`flex w-full flex-wrap items-center justify-center space-x-2 ${getAlignStyle()}`}
aria-label="Pagination Navigation"
>
<Button
variant="secondary"
style={buttonStyle}
size="medium"
disabled={!prev_page_url}
as={paginationButtonAs}
href={prev_page_url || '#'}
className={!prev_page_url ? 'pointer-events-none' : ''}
aria-label="Go to previous page"
>
Previous
</Button>
{pageLinks.map((link, index) => (
<Button
key={index}
variant={link.active ? 'primary' : 'secondary'}
style={buttonStyle}
size="medium"
disabled={!link.url}
as={link.url ? 'a' : paginationButtonAs}
href={link.url || '#'}
className={!link.url ? 'pointer-events-none' : ''}
aria-label={`Go to page ${link.label}`}
aria-current={link.active ? 'page' : undefined}
>
{link.label}
</Button>
))}
<Button
variant="secondary"
style={buttonStyle}
size="medium"
disabled={!next_page_url}
as={paginationButtonAs}
href={next_page_url || '#'}
className={!next_page_url ? 'pointer-events-none' : ''}
aria-label="Go to next page"
>
Next
</Button>
</nav>
</div>
);
}