|
| 1 | +import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/20/solid' |
| 2 | +interface PaginationProps { |
| 3 | + first: number, |
| 4 | + last: number, |
| 5 | + total: number, |
| 6 | + itemsPerPage: number, |
| 7 | + setPage: (page: number) => void, |
| 8 | +} |
| 9 | + |
| 10 | +export default function Pagination(props: PaginationProps) { |
| 11 | + const { first, last, total, itemsPerPage, setPage } = props |
| 12 | + const totalPages = Math.ceil(total / itemsPerPage) |
| 13 | + const currentPage = Math.floor(first / itemsPerPage) + 1 |
| 14 | + |
| 15 | + function setPageWrapped(e: React.MouseEvent, page: number) { |
| 16 | + e.preventDefault() |
| 17 | + if (page < 1) setPage(1) |
| 18 | + else if (page > totalPages) setPage(totalPages) |
| 19 | + else setPage(page) |
| 20 | + } |
| 21 | + return ( |
| 22 | + <div className="flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6"> |
| 23 | + <div className="flex flex-1 justify-between sm:hidden"> |
| 24 | + <a |
| 25 | + href="#" |
| 26 | + className="relative inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50" |
| 27 | + onClick={(e) => setPageWrapped(e, currentPage - 1)} |
| 28 | + > |
| 29 | + Previous |
| 30 | + </a> |
| 31 | + <a |
| 32 | + href="#" |
| 33 | + className="relative ml-3 inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50" |
| 34 | + onClick={(e) => setPageWrapped(e, currentPage + 1)} |
| 35 | + > |
| 36 | + Next |
| 37 | + </a> |
| 38 | + </div> |
| 39 | + <div className="hidden sm:flex sm:flex-1 sm:items-center sm:justify-between"> |
| 40 | + <div> |
| 41 | + <p className="text-sm text-gray-700"> |
| 42 | + Showing <span className="font-medium">{first + 1}</span> to <span className="font-medium">{last + 1}</span> of{' '} |
| 43 | + <span className="font-medium">{total}</span> results |
| 44 | + </p> |
| 45 | + </div> |
| 46 | + <div> |
| 47 | + <nav className="isolate inline-flex -space-x-px rounded-md shadow-sm" aria-label="Pagination"> |
| 48 | + <a |
| 49 | + href="#" |
| 50 | + className="relative inline-flex items-center rounded-l-md border border-gray-300 bg-white px-2 py-2 text-sm font-medium text-gray-500 hover:bg-gray-50 focus:z-20" |
| 51 | + onClick={(e) => setPageWrapped(e, currentPage - 1)} |
| 52 | + > |
| 53 | + <span className="sr-only">Previous</span> |
| 54 | + <ChevronLeftIcon className="h-5 w-5" aria-hidden="true" /> |
| 55 | + </a> |
| 56 | + {Array.from({ length: totalPages }, (_, i) => i + 1).map((page) => ( |
| 57 | + <a key={page} |
| 58 | + href="#" |
| 59 | + // className="relative inline-flex items-center border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-500 hover:bg-gray-50 focus:z-20" |
| 60 | + className={`relative inline-flex items-center border px-4 py-2 text-sm font-medium focus:z-20 ${currentPage === page ? 'z-10 bg-indigo-50 border-indigo-500 text-indigo-600' : 'bg-white border-gray-300 text-gray-500 hover:bg-gray-50'}`} |
| 61 | + onClick={(e) => setPageWrapped(e, page)} |
| 62 | + > |
| 63 | + {page} |
| 64 | + </a> |
| 65 | + ))} |
| 66 | + |
| 67 | + {/* Current: "z-10 bg-indigo-50 border-indigo-500 text-indigo-600", Default: "bg-white border-gray-300 text-gray-500 hover:bg-gray-50" */} |
| 68 | + {/* <a |
| 69 | + href="#" |
| 70 | + aria-current="page" |
| 71 | + className="relative z-10 inline-flex items-center border border-indigo-500 bg-indigo-50 px-4 py-2 text-sm font-medium text-indigo-600 focus:z-20" |
| 72 | + > |
| 73 | + 1 |
| 74 | + </a> */} |
| 75 | + |
| 76 | + <a |
| 77 | + href="#" |
| 78 | + className="relative inline-flex items-center rounded-r-md border border-gray-300 bg-white px-2 py-2 text-sm font-medium text-gray-500 hover:bg-gray-50 focus:z-20" |
| 79 | + onClick={(e) => setPageWrapped(e, currentPage + 1)} |
| 80 | + > |
| 81 | + <span className="sr-only">Next</span> |
| 82 | + <ChevronRightIcon className="h-5 w-5" aria-hidden="true" /> |
| 83 | + </a> |
| 84 | + </nav> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + ) |
| 89 | +} |
0 commit comments