Skip to content

Commit

Permalink
feat: Pagination basic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricy committed Nov 14, 2023
1 parent 705ef26 commit 6ad7755
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified src/.DS_Store
Binary file not shown.
Binary file modified src/components/.DS_Store
Binary file not shown.
87 changes: 87 additions & 0 deletions src/components/Pagination/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { ComponentProps, PropsWithChildren } from "react";
import cx from "clsx";

interface PaginationProps extends ComponentProps<"div"> {
currentPage: number;
totalPages: number;
onPageChange: (page: number) => void;
}

const Pagination: React.FC<PaginationProps> = ({
currentPage,
totalPages,
onPageChange,
className,
...props
}) => {
const renderPageNumbers = () => {
const adjacentPageCount = 2;
const pageNumbers = Array.from(
{ length: totalPages },
(_, index) => index + 1
);

const visiblePages = pageNumbers.filter(
(page) =>
page === 1 ||
page === totalPages ||
(page >= currentPage - adjacentPageCount &&
page <= currentPage + adjacentPageCount)
);

const pagesWithEllipses: (number | null)[] = [];
let lastPage = 0;

visiblePages.forEach((page) => {
if (lastPage !== 0 && page - lastPage > 1) {
// If there is a gap between pages, insert an ellipse
pagesWithEllipses.push(null);
}
pagesWithEllipses.push(page);
lastPage = page;
});

return pagesWithEllipses.map((page) =>
page !== null ? (
<PaginationContainer>
<button key={page} onClick={() => onPageChange(page)}>
{page}
</button>
</PaginationContainer>
) : (
<span key="ellipsis">...</span>
)
);
};

return (
<div
className={cx("flex flex-row items-center justify-center", className)}
{...props}
>
<button
onClick={() => onPageChange(currentPage - 1)}
disabled={currentPage === 1}
>
Previous
</button>
{renderPageNumbers()}
<button
onClick={() => onPageChange(currentPage + 1)}
disabled={currentPage === totalPages}
>
Next
</button>
</div>
);
};

export default Pagination;

const PaginationContainer: React.FC<PropsWithChildren> = ({ children }) => {
return (
<div className="w-[30px] h-[30px] border border-solid hover:border-[#ffffff]">
{children}
</div>
);
};

0 comments on commit 6ad7755

Please sign in to comment.