Created
July 21, 2025 17:51
-
-
Save Klerith/92b8e6ae88961a5f6869528607a1a92c to your computer and use it in GitHub Desktop.
Componente personalizado de paginación
This file contains hidden or 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
| import { ChevronLeft, ChevronRight } from 'lucide-react'; | |
| import { Button } from '../ui/button'; | |
| import { useSearchParams } from 'react-router'; | |
| interface Props { | |
| totalPages: number; | |
| } | |
| export const CustomPagination = ({ totalPages }: Props) => { | |
| const [searchParams, setSearchParams] = useSearchParams(); | |
| const queryPage = searchParams.get('page') || '1'; | |
| const page = isNaN(+queryPage) ? 1 : +queryPage; | |
| const handlePageChange = (page: number) => { | |
| if (page < 1 || page > totalPages) return; | |
| searchParams.set('page', page.toString()); | |
| setSearchParams(searchParams); | |
| }; | |
| return ( | |
| <div className="flex items-center justify-center space-x-2"> | |
| <Button | |
| variant="outline" | |
| size="sm" | |
| disabled={page === 1} | |
| onClick={() => handlePageChange(page - 1)} | |
| > | |
| <ChevronLeft className="h-4 w-4" /> | |
| Anteriores | |
| </Button> | |
| {Array.from({ length: totalPages }).map((_, index) => ( | |
| <Button | |
| key={index} | |
| variant={page === index + 1 ? 'default' : 'outline'} | |
| size="sm" | |
| onClick={() => handlePageChange(index + 1)} | |
| > | |
| {index + 1} | |
| </Button> | |
| ))} | |
| <Button | |
| variant="outline" | |
| size="sm" | |
| disabled={page === totalPages} | |
| onClick={() => handlePageChange(page + 1)} | |
| > | |
| Siguientes | |
| <ChevronRight className="h-4 w-4" /> | |
| </Button> | |
| </div> | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment