|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { |
| 3 | + PaginationUnit, |
| 4 | + PaginationContent, |
| 5 | + PaginationEllipsis, |
| 6 | + PaginationItem, |
| 7 | + PaginationLink, |
| 8 | + PaginationNext, |
| 9 | + PaginationPrevious, |
| 10 | +} from './pagination'; |
| 11 | + |
| 12 | +interface PaginationProps { |
| 13 | + defaultPage?: number; |
| 14 | + total: number; |
| 15 | + pageSize: number; |
| 16 | + onChange: (page: number) => void; |
| 17 | + mode?: 'dark' | 'light'; |
| 18 | + pageNo: number; |
| 19 | +} |
| 20 | + |
| 21 | +const getPageNumbers = (currentPageNo: number, totalPages: number) => { |
| 22 | + const pages: (number | 'ellipsis')[] = []; |
| 23 | + |
| 24 | + if (totalPages <= 7) { |
| 25 | + return Array.from({ length: totalPages }, (_, i) => i + 1); |
| 26 | + } |
| 27 | + |
| 28 | + // Always show first page |
| 29 | + pages.push(1); |
| 30 | + |
| 31 | + if (currentPageNo <= 3) { |
| 32 | + pages.push(2, 3, 4, 'ellipsis'); |
| 33 | + } else if (currentPageNo >= totalPages - 2) { |
| 34 | + pages.push('ellipsis', totalPages - 3, totalPages - 2, totalPages - 1); |
| 35 | + } else { |
| 36 | + pages.push( |
| 37 | + 'ellipsis', |
| 38 | + currentPageNo - 1, |
| 39 | + currentPageNo, |
| 40 | + currentPageNo + 1, |
| 41 | + 'ellipsis', |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + // Always show last page |
| 46 | + pages.push(totalPages); |
| 47 | + |
| 48 | + return pages; |
| 49 | +}; |
| 50 | + |
| 51 | +function Pagination(props: PaginationProps) { |
| 52 | + const { |
| 53 | + defaultPage = 1, |
| 54 | + total, |
| 55 | + pageSize, |
| 56 | + pageNo, |
| 57 | + onChange, |
| 58 | + mode = 'light', |
| 59 | + } = props; |
| 60 | + const totalPages = Math.ceil(total / pageSize); |
| 61 | + const [currentPage, setCurrentPage] = useState(pageNo || defaultPage); |
| 62 | + const [pageNumbers, setPageNumbers] = useState<(number | 'ellipsis')[]>( |
| 63 | + getPageNumbers(defaultPage, totalPages), |
| 64 | + ); |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + setCurrentPage(pageNo); |
| 68 | + }); |
| 69 | + useEffect(() => { |
| 70 | + setPageNumbers(getPageNumbers(currentPage, totalPages)); |
| 71 | + }, [currentPage, totalPages]); |
| 72 | + return ( |
| 73 | + <PaginationUnit style={{ userSelect: 'none' }}> |
| 74 | + <PaginationContent> |
| 75 | + <PaginationItem> |
| 76 | + <PaginationPrevious |
| 77 | + onClick={() => { |
| 78 | + if (currentPage > 1) { |
| 79 | + onChange(currentPage - 1); |
| 80 | + setCurrentPage(currentPage - 1); |
| 81 | + } |
| 82 | + }} |
| 83 | + mode={mode} |
| 84 | + style={{ |
| 85 | + opacity: currentPage <= 1 ? 0.25 : 1, |
| 86 | + cursor: currentPage <= 1 ? 'not-allowed' : 'pointer', |
| 87 | + pointerEvents: currentPage <= 1 ? 'none' : 'auto', |
| 88 | + padding: '0.5rem', |
| 89 | + borderRadius: '999px', |
| 90 | + backgroundColor: mode === 'light' ? '#232E3D' : '#FFF', |
| 91 | + }} |
| 92 | + /> |
| 93 | + </PaginationItem> |
| 94 | + |
| 95 | + {pageNumbers.map((page, index) => ( |
| 96 | + <PaginationItem key={index}> |
| 97 | + {page === 'ellipsis' ? ( |
| 98 | + <PaginationEllipsis mode={mode} /> |
| 99 | + ) : ( |
| 100 | + <PaginationLink |
| 101 | + onClick={() => { |
| 102 | + setCurrentPage(page); |
| 103 | + onChange(page); |
| 104 | + }} |
| 105 | + className={`${ |
| 106 | + mode === 'dark' |
| 107 | + ? 'undp-pagination-link dark-mode' |
| 108 | + : 'undp-pagination-link' |
| 109 | + }${page === currentPage ? ' selected' : ''}`} |
| 110 | + > |
| 111 | + {page} |
| 112 | + </PaginationLink> |
| 113 | + )} |
| 114 | + </PaginationItem> |
| 115 | + ))} |
| 116 | + |
| 117 | + <PaginationItem> |
| 118 | + <PaginationNext |
| 119 | + onClick={() => { |
| 120 | + if (currentPage < totalPages) { |
| 121 | + onChange(currentPage + 1); |
| 122 | + setCurrentPage(currentPage + 1); |
| 123 | + } |
| 124 | + }} |
| 125 | + mode={mode} |
| 126 | + style={{ |
| 127 | + opacity: currentPage >= totalPages ? 0.25 : 1, |
| 128 | + cursor: currentPage >= totalPages ? 'not-allowed' : 'pointer', |
| 129 | + pointerEvents: currentPage >= totalPages ? 'none' : 'auto', |
| 130 | + padding: '0.5rem', |
| 131 | + borderRadius: '999px', |
| 132 | + backgroundColor: mode === 'light' ? '#232E3D' : '#FFF', |
| 133 | + }} |
| 134 | + /> |
| 135 | + </PaginationItem> |
| 136 | + </PaginationContent> |
| 137 | + </PaginationUnit> |
| 138 | + ); |
| 139 | +} |
| 140 | + |
| 141 | +export { Pagination }; |
0 commit comments