-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15680c7
commit 595c7b0
Showing
3 changed files
with
83 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,79 @@ | ||
import { | ||
Pagination, | ||
PaginationContent, | ||
PaginationItem, | ||
PaginationLink, | ||
PaginationNext, | ||
PaginationPrevious, | ||
} from "@/components/ui/pagination"; | ||
import { ChevronLeft, ChevronRight } from "lucide-react"; | ||
import React from "react"; | ||
|
||
interface MyPaginationProps { | ||
interface PaginationProps { | ||
currentPage: number; | ||
totalPages: number; | ||
onPageChange: (page: number) => void; | ||
} | ||
|
||
export const MyPagination: React.FC<MyPaginationProps> = ({ | ||
export const MyPagination: React.FC<PaginationProps> = ({ | ||
currentPage, | ||
totalPages, | ||
onPageChange, | ||
}) => { | ||
const pageNumbers = []; | ||
for (let i = 1; i <= totalPages; i++) { | ||
pageNumbers.push(i); | ||
} | ||
|
||
const visiblePageNumbers = pageNumbers.filter( | ||
(num) => | ||
num === 1 || | ||
num === totalPages || | ||
(num >= currentPage - 1 && num <= currentPage + 1) | ||
); | ||
|
||
return ( | ||
<Pagination> | ||
<PaginationContent> | ||
<PaginationItem> | ||
<PaginationPrevious | ||
href="#" | ||
onClick={() => onPageChange(Math.max(1, currentPage - 1))} | ||
/> | ||
</PaginationItem> | ||
{[...Array(totalPages)].map((_, index) => ( | ||
<PaginationItem key={index}> | ||
<PaginationLink | ||
href="#" | ||
isActive={currentPage === index + 1} | ||
onClick={() => onPageChange(index + 1)} | ||
> | ||
{index + 1} | ||
</PaginationLink> | ||
</PaginationItem> | ||
))} | ||
<PaginationItem> | ||
<PaginationNext | ||
href="#" | ||
onClick={() => onPageChange(Math.min(totalPages, currentPage + 1))} | ||
/> | ||
</PaginationItem> | ||
</PaginationContent> | ||
</Pagination> | ||
<nav className="flex items-center justify-center space-x-2"> | ||
<button | ||
onClick={() => onPageChange(Math.max(1, currentPage - 1))} | ||
disabled={currentPage === 1} | ||
className="p-2 rounded-full hover:bg-gray-200 disabled:opacity-50" | ||
> | ||
<ChevronLeft size={20} /> | ||
</button> | ||
|
||
{visiblePageNumbers.map((number, index) => { | ||
if (index > 0 && visiblePageNumbers[index - 1] !== number - 1) { | ||
return ( | ||
<React.Fragment key={`ellipsis-${number}`}> | ||
<span className="px-2">...</span> | ||
<button | ||
onClick={() => onPageChange(number)} | ||
className={`w-8 h-8 rounded-full ${ | ||
currentPage === number | ||
? "bg-blue-600 text-white" | ||
: "hover:bg-gray-200" | ||
}`} | ||
> | ||
{number} | ||
</button> | ||
</React.Fragment> | ||
); | ||
} | ||
return ( | ||
<button | ||
key={number} | ||
onClick={() => onPageChange(number)} | ||
className={`w-8 h-8 rounded-full ${ | ||
currentPage === number | ||
? "bg-blue-600 text-white" | ||
: "hover:bg-gray-200" | ||
}`} | ||
> | ||
{number} | ||
</button> | ||
); | ||
})} | ||
|
||
<button | ||
onClick={() => onPageChange(Math.min(totalPages, currentPage + 1))} | ||
disabled={currentPage === totalPages} | ||
className="p-2 rounded-full hover:bg-gray-200 disabled:opacity-50" | ||
> | ||
<ChevronRight size={10} /> | ||
</button> | ||
</nav> | ||
); | ||
}; |