);
}
function renderPageNumber(
pageNumber: number,
currentPage: number,
size: Size,
onPageClick?: (currentPage: number) => void
) {
return (
);
}
function renderEllipses(size: "sm" | "xs") {
return (
...
);
}
function getPageButtons(
currentPage: number,
totalPages: number,
slots: number,
size: Size,
onPageClick?: (currentPage: number) => void
) {
const pagination: React.ReactNode[] = [];
// If total pages are less than or equal to slots, show all pages
if (totalPages <= slots) {
for (let i = 0; i < totalPages; i++) {
pagination.push(renderPageNumber(i, currentPage, size, onPageClick));
}
return pagination;
}
const remainingSlots = slots - 2; // slots excluding first and last page
const halfSlots = Math.floor(remainingSlots / 2);
// Ensure current page is within bounds
currentPage = Math.max(0, Math.min(currentPage, totalPages - 1));
pagination.push(renderPageNumber(0, currentPage, size, onPageClick)); // Always show the first page
// Determine the range of pages to display
let start, end;
if (currentPage <= halfSlots + 1) {
start = 1;
end = remainingSlots - 1;
} else if (currentPage >= totalPages - halfSlots - 2) {
start = totalPages - remainingSlots;
end = totalPages - 2;
} else {
start = currentPage - halfSlots + 1;
end = currentPage + halfSlots - 1;
}
// Add ellipsis if there is a gap between the first page and the start of the range
if (start > 1) {
pagination.push(renderEllipses(size));
}
// Add the range of pages
for (let i = start; i <= end; i++) {
pagination.push(renderPageNumber(i, currentPage, size, onPageClick));
}
// Add ellipsis if there is a gap between the end of the range and the last page
if (end < totalPages - 2) {
pagination.push(renderEllipses(size));
}
pagination.push(
renderPageNumber(totalPages - 1, currentPage, size, onPageClick)
); // Always show the last page
return pagination;
}