|
| 1 | +import { ArrowNarrowLeftIcon, ArrowNarrowRightIcon } from '@heroicons/react/outline'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import React from 'react'; |
| 4 | +import { Link } from 'remix'; |
| 5 | + |
| 6 | +interface PaginationProps { |
| 7 | + total: number; |
| 8 | + itemsPerPage: number; |
| 9 | + currentPage: number; |
| 10 | + getLink: (page: number) => string; |
| 11 | +} |
| 12 | +const Pagination: React.FC<PaginationProps> = ({ total, itemsPerPage, currentPage, getLink }) => { |
| 13 | + const pageCount = Math.ceil(total / itemsPerPage); |
| 14 | + |
| 15 | + const getButtons = () => { |
| 16 | + let buttons: JSX.Element[] = []; |
| 17 | + |
| 18 | + for (let i = 1; i <= pageCount; i++) { |
| 19 | + const pageButton = ( |
| 20 | + <Link |
| 21 | + key={`pagination-${i}`} |
| 22 | + to={getLink(i)} |
| 23 | + className={clsx( |
| 24 | + 'border-transparent border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium', |
| 25 | + i === currentPage ? 'border-brand-500 text-brand-600' : 'text-gray-500 hover:text-gray-700 hover:border-gray-300', |
| 26 | + )} |
| 27 | + aria-current="page" |
| 28 | + > |
| 29 | + {i} |
| 30 | + </Link> |
| 31 | + ); |
| 32 | + |
| 33 | + const divider = ( |
| 34 | + <span |
| 35 | + key={`pagination-divider-${i}`} |
| 36 | + className="border-transparent text-gray-500 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium" |
| 37 | + > |
| 38 | + ... |
| 39 | + </span> |
| 40 | + ); |
| 41 | + if (pageCount > 8) { |
| 42 | + //Always have a divider after page 1 |
| 43 | + if (i === 1) { |
| 44 | + buttons.push(pageButton); |
| 45 | + buttons.push(divider); |
| 46 | + //Always have a divider before the last page |
| 47 | + } else if (i === pageCount) { |
| 48 | + buttons.push(divider); |
| 49 | + buttons.push(pageButton); |
| 50 | + } else if ( |
| 51 | + i === currentPage || |
| 52 | + i === currentPage - 1 || |
| 53 | + i === currentPage + 1 || |
| 54 | + (currentPage < 3 && i < 5) || |
| 55 | + (currentPage >= pageCount - 2 && i > pageCount - 4) |
| 56 | + ) { |
| 57 | + buttons.push(pageButton); |
| 58 | + } |
| 59 | + } else { |
| 60 | + buttons.push(pageButton); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return buttons; |
| 65 | + }; |
| 66 | + |
| 67 | + return pageCount > 1 ? ( |
| 68 | + <nav className="border-t border-gray-200 my-4 px-4 flex items-center justify-between"> |
| 69 | + <div className="-mt-px w-0 flex-1 flex"> |
| 70 | + {currentPage !== 1 && ( |
| 71 | + <Link |
| 72 | + to={getLink(currentPage - 1)} |
| 73 | + className="border-t-2 border-transparent pt-4 pr-1 inline-flex items-center text-sm font-medium text-gray-500 hover:text-gray-700 hover:border-gray-300" |
| 74 | + > |
| 75 | + <ArrowNarrowLeftIcon className="mr-3 h-5 w-5 text-gray-400" aria-hidden="true" /> |
| 76 | + Previous |
| 77 | + </Link> |
| 78 | + )} |
| 79 | + </div> |
| 80 | + <div className="">{getButtons()}</div> |
| 81 | + <div className="-mt-px w-0 flex-1 flex justify-end"> |
| 82 | + {currentPage !== pageCount && ( |
| 83 | + <Link |
| 84 | + to={getLink(currentPage + 1)} |
| 85 | + className="border-t-2 border-transparent pt-4 pl-1 inline-flex items-center text-sm font-medium text-gray-500 hover:text-gray-700 hover:border-gray-300" |
| 86 | + > |
| 87 | + Next |
| 88 | + <ArrowNarrowRightIcon className="ml-3 h-5 w-5 text-gray-400" aria-hidden="true" /> |
| 89 | + </Link> |
| 90 | + )} |
| 91 | + </div> |
| 92 | + </nav> |
| 93 | + ) : null; |
| 94 | +}; |
| 95 | + |
| 96 | +export default Pagination; |
0 commit comments