-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pagination.tsx
181 lines (171 loc) · 4.8 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
import React, { useState, useEffect, useCallback } from "react";
// Constants for default values
const DEFAULT_COLOR = "#007bff";
const DEFAULT_SHAPE = "square";
const DEFAULT_BUTTON_COUNT = 5;
const DEFAULT_FIRST_TEXT = "First";
const DEFAULT_PREV_TEXT = "Prev";
const DEFAULT_NEXT_TEXT = "Next";
const DEFAULT_LAST_TEXT = "Last";
type PaginationProps = {
totalItems: number;
itemsPerPage: number;
onPageChange?: (page: number) => void;
color?: string;
shape?: "circle" | "square";
visibleButtonCount?: number;
showNextPrev?: boolean;
showFirstLast?: boolean;
onSuccess?: (page: number) => void;
onError?: (error: Error) => void;
customStyles?: React.CSSProperties;
firstText?: React.ReactNode;
prevText?: React.ReactNode;
nextText?: React.ReactNode;
lastText?: React.ReactNode;
className?: string;
buttonClassName?: string;
};
// Button component used for all the buttons in the pagination component
const Button: React.FC<{
style: React.CSSProperties;
onClick: () => void;
disabled: boolean;
className?: string;
children: React.ReactNode;
}> = React.memo(({ style, onClick, disabled, className, children }) => (
<button
style={style}
onClick={onClick}
disabled={disabled}
className={className}
>
{children}
</button>
));
// Pagination component
const Pagination: React.FC<PaginationProps> = ({
totalItems,
itemsPerPage,
onPageChange = () => {},
color = DEFAULT_COLOR,
shape = DEFAULT_SHAPE,
visibleButtonCount = DEFAULT_BUTTON_COUNT,
showNextPrev = false,
showFirstLast = false,
onSuccess = () => {},
onError = () => {},
customStyles = {},
firstText = DEFAULT_FIRST_TEXT,
prevText = DEFAULT_PREV_TEXT,
nextText = DEFAULT_NEXT_TEXT,
lastText = DEFAULT_LAST_TEXT,
className = "",
buttonClassName = "",
}) => {
const [currentPage, setCurrentPage] = useState(1);
const [buttonNumbers, setButtonNumbers] = useState<number[]>([]);
// Calculate total number of pages
const totalPages = Math.ceil(totalItems / itemsPerPage);
// Calculate the button numbers to display
useEffect(() => {
// Calculate start and end page numbers
let startPage = Math.max(
1,
currentPage - Math.floor(visibleButtonCount / 2),
);
let endPage = startPage + visibleButtonCount - 1;
// Adjust start and end page if they go beyond the total number of pages
if (endPage > totalPages) {
endPage = totalPages;
startPage = Math.max(1, endPage - visibleButtonCount + 1);
}
// Generate button numbers
const newButtonNumbers = Array.from(
{ length: endPage - startPage + 1 },
(_, i) => startPage + i,
);
setButtonNumbers(newButtonNumbers);
}, [currentPage, totalPages, visibleButtonCount]);
// Style for the buttons
const buttonStyle = {
color: "white",
backgroundColor: color,
borderRadius: shape === "circle" ? "50%" : "0",
padding: "0.5rem 1rem",
margin: "0 0.2rem",
border: "none",
cursor: "pointer",
...customStyles,
};
// Callback function to handle page changes
const handlePageChange = useCallback(
(page: number) => {
if (page >= 1 && page <= totalPages) {
setCurrentPage(page);
onPageChange(page);
onSuccess(page);
} else {
onError(new Error("Page number is out of range"));
}
},
[totalPages, onPageChange, onSuccess, onError],
);
// Render the pagination component
return (
<div className={className}>
{showFirstLast ? (
<Button
style={buttonStyle}
onClick={() => handlePageChange(1)}
disabled={currentPage === 1}
className={buttonClassName}
>
{firstText}
</Button>
) : null}
{showNextPrev ? (
<Button
style={buttonStyle}
onClick={() => handlePageChange(currentPage - 1)}
disabled={currentPage === 1}
className={buttonClassName}
>
{prevText}
</Button>
) : null}
{buttonNumbers.map((number) => (
<Button
key={number}
style={buttonStyle}
onClick={() => handlePageChange(number)}
disabled={false}
className={buttonClassName}
>
{number}
</Button>
))}
{showNextPrev ? (
<Button
style={buttonStyle}
onClick={() => handlePageChange(currentPage + 1)}
disabled={currentPage === totalPages}
className={buttonClassName}
>
{nextText}
</Button>
) : null}
{showFirstLast ? (
<Button
style={buttonStyle}
onClick={() => handlePageChange(totalPages)}
disabled={currentPage === totalPages}
className={buttonClassName}
>
{lastText}
</Button>
) : null}
</div>
);
};
export default Pagination;