|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +type PaginationProps = { |
| 4 | + currentPage: number; |
| 5 | + totalPages: number; |
| 6 | + onPageChange: (page: number) => void; |
| 7 | +}; |
| 8 | + |
| 9 | +const Pagination: React.FC<PaginationProps> = ({ currentPage, totalPages, onPageChange }) => { |
| 10 | + if (totalPages <= 1) return null; |
| 11 | + |
| 12 | + const getPageNumbers = () => { |
| 13 | + const delta = 2; |
| 14 | + const pages: number[] = []; |
| 15 | + |
| 16 | + for (let i = Math.max(1, currentPage - delta); i <= Math.min(totalPages, currentPage + delta); i++) { |
| 17 | + pages.push(i); |
| 18 | + } |
| 19 | + |
| 20 | + return pages; |
| 21 | + }; |
| 22 | + |
| 23 | + return ( |
| 24 | + <div className="flex items-center justify-center space-x-2 mt-4"> |
| 25 | + <button |
| 26 | + onClick={() => onPageChange(currentPage - 1)} |
| 27 | + disabled={currentPage === 1} |
| 28 | + className={`px-3 py-1 rounded border text-sm ${ |
| 29 | + currentPage === 1 |
| 30 | + ? 'bg-gray-200 text-gray-500 cursor-not-allowed' |
| 31 | + : 'bg-white hover:bg-gray-100 border-gray-300 text-gray-700' |
| 32 | + }`} |
| 33 | + > |
| 34 | + Prev |
| 35 | + </button> |
| 36 | + |
| 37 | + {getPageNumbers().map((page) => ( |
| 38 | + <button |
| 39 | + key={page} |
| 40 | + onClick={() => onPageChange(page)} |
| 41 | + className={`px-3 py-1 rounded border text-sm ${ |
| 42 | + page === currentPage |
| 43 | + ? 'bg-blue-500 text-white border-blue-500' |
| 44 | + : 'bg-white hover:bg-gray-100 border-gray-300 text-gray-700' |
| 45 | + }`} |
| 46 | + > |
| 47 | + {page} |
| 48 | + </button> |
| 49 | + ))} |
| 50 | + |
| 51 | + <button |
| 52 | + onClick={() => onPageChange(currentPage + 1)} |
| 53 | + disabled={currentPage === totalPages} |
| 54 | + className={`px-3 py-1 rounded border text-sm ${ |
| 55 | + currentPage === totalPages |
| 56 | + ? 'bg-gray-200 text-gray-500 cursor-not-allowed' |
| 57 | + : 'bg-white hover:bg-gray-100 border-gray-300 text-gray-700' |
| 58 | + }`} |
| 59 | + > |
| 60 | + Next |
| 61 | + </button> |
| 62 | + </div> |
| 63 | + ); |
| 64 | +}; |
| 65 | + |
| 66 | +export default Pagination; |
0 commit comments