|
| 1 | +import { useState } from 'react'; |
| 2 | + |
| 3 | +export default function Pagination({ currentPage, totalPages, totalItems, pageSize = 10, onPageChange }) { |
| 4 | + const [jumpPage, setJumpPage] = useState(''); |
| 5 | + |
| 6 | + const handlePageChange = (newPage) => { |
| 7 | + if (newPage < 1 || newPage > totalPages) return; |
| 8 | + onPageChange(newPage); |
| 9 | + }; |
| 10 | + |
| 11 | + const handleJumpPage = (e) => { |
| 12 | + e.preventDefault(); |
| 13 | + const pageNum = parseInt(jumpPage); |
| 14 | + if (!isNaN(pageNum) && pageNum >= 1 && pageNum <= totalPages) { |
| 15 | + handlePageChange(pageNum); |
| 16 | + setJumpPage(''); |
| 17 | + } |
| 18 | + }; |
| 19 | + |
| 20 | + if (totalPages <= 1) return null; |
| 21 | + |
| 22 | + const delta = 2; |
| 23 | + const range = []; |
| 24 | + for (let i = Math.max(2, currentPage - delta); i <= Math.min(totalPages - 1, currentPage + delta); i++) { |
| 25 | + range.push(i); |
| 26 | + } |
| 27 | + |
| 28 | + if (currentPage - delta > 2) { |
| 29 | + range.unshift('...'); |
| 30 | + } |
| 31 | + if (currentPage + delta < totalPages - 1) { |
| 32 | + range.push('...'); |
| 33 | + } |
| 34 | + |
| 35 | + const pageItems = [1, ...range, totalPages].filter( |
| 36 | + (page, index, self) => self.indexOf(page) === index |
| 37 | + ); |
| 38 | + |
| 39 | + return ( |
| 40 | + <div className="flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6 mt-4"> |
| 41 | + <div className="flex flex-1 justify-between sm:hidden"> |
| 42 | + <button |
| 43 | + onClick={() => handlePageChange(currentPage - 1)} |
| 44 | + disabled={currentPage === 1} |
| 45 | + 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 disabled:opacity-50" |
| 46 | + > |
| 47 | + 上一页 |
| 48 | + </button> |
| 49 | + <button |
| 50 | + onClick={() => handlePageChange(currentPage + 1)} |
| 51 | + disabled={currentPage === totalPages} |
| 52 | + 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 disabled:opacity-50" |
| 53 | + > |
| 54 | + 下一页 |
| 55 | + </button> |
| 56 | + </div> |
| 57 | + |
| 58 | + <div className="hidden sm:flex sm:flex-1 sm:items-center sm:justify-between"> |
| 59 | + <div> |
| 60 | + <p className="text-sm text-gray-700"> |
| 61 | + {totalItems !== undefined ? ( |
| 62 | + <> |
| 63 | + 显示第 <span className="font-medium">{(currentPage - 1) * pageSize + 1}</span> 到{' '} |
| 64 | + <span className="font-medium">{Math.min(currentPage * pageSize, totalItems)}</span> 条结果,共{' '} |
| 65 | + <span className="font-medium">{totalItems}</span> 条 |
| 66 | + </> |
| 67 | + ) : ( |
| 68 | + <> |
| 69 | + 第 <span className="font-medium">{currentPage}</span> 页,共{' '} |
| 70 | + <span className="font-medium">{totalPages}</span> 页 |
| 71 | + </> |
| 72 | + )} |
| 73 | + </p> |
| 74 | + </div> |
| 75 | + <div className="flex items-center gap-4"> |
| 76 | + <nav className="isolate inline-flex -space-x-px rounded-md shadow-sm" aria-label="Pagination"> |
| 77 | + <button |
| 78 | + onClick={() => handlePageChange(1)} |
| 79 | + disabled={currentPage === 1} |
| 80 | + className="relative inline-flex items-center rounded-l-md px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-20 focus:outline-offset-0 disabled:opacity-50" |
| 81 | + > |
| 82 | + <span className="sr-only">首页</span> |
| 83 | + <svg className="h-5 w-5" fill="currentColor" viewBox="0 0 20 20"> |
| 84 | + <path fillRule="evenodd" d="M15.707 15.707a1 1 0 01-1.414 0l-5-5a1 1 0 010-1.414l5-5a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 010 1.414zm-6 0a1 1 0 01-1.414 0l-5-5a1 1 0 010-1.414l5-5a1 1 0 011.414 1.414L5.414 10l4.293 4.293a1 1 0 010 1.414z" clipRule="evenodd" /> |
| 85 | + </svg> |
| 86 | + </button> |
| 87 | + |
| 88 | + <button |
| 89 | + onClick={() => handlePageChange(currentPage - 1)} |
| 90 | + disabled={currentPage === 1} |
| 91 | + className="relative inline-flex items-center px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-20 focus:outline-offset-0 disabled:opacity-50" |
| 92 | + > |
| 93 | + <span className="sr-only">上一页</span> |
| 94 | + <svg className="h-5 w-5" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> |
| 95 | + <path fillRule="evenodd" d="M12.79 5.23a.75.75 0 01-.02 1.06L8.832 10l3.938 3.71a.75.75 0 11-1.04 1.08l-4.5-4.25a.75.75 0 010-1.08l4.5-4.25a.75.75 0 011.06.02z" clipRule="evenodd" /> |
| 96 | + </svg> |
| 97 | + </button> |
| 98 | + |
| 99 | + {pageItems.map((page, index) => ( |
| 100 | + <button |
| 101 | + key={index} |
| 102 | + onClick={() => typeof page === 'number' && handlePageChange(page)} |
| 103 | + disabled={page === '...' || page === currentPage} |
| 104 | + className={`relative inline-flex items-center px-4 py-2 text-sm font-semibold ${ |
| 105 | + page === currentPage |
| 106 | + ? 'z-10 bg-blue-600 text-white focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600' |
| 107 | + : page === '...' |
| 108 | + ? 'text-gray-700' |
| 109 | + : 'text-gray-900 ring-1 ring-inset ring-gray-300 hover:bg-gray-50' |
| 110 | + }`} |
| 111 | + > |
| 112 | + {page} |
| 113 | + </button> |
| 114 | + ))} |
| 115 | + |
| 116 | + <button |
| 117 | + onClick={() => handlePageChange(currentPage + 1)} |
| 118 | + disabled={currentPage === totalPages} |
| 119 | + className="relative inline-flex items-center px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-20 focus:outline-offset-0 disabled:opacity-50" |
| 120 | + > |
| 121 | + <span className="sr-only">下一页</span> |
| 122 | + <svg className="h-5 w-5" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> |
| 123 | + <path fillRule="evenodd" d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z" clipRule="evenodd" /> |
| 124 | + </svg> |
| 125 | + </button> |
| 126 | + |
| 127 | + <button |
| 128 | + onClick={() => handlePageChange(totalPages)} |
| 129 | + disabled={currentPage === totalPages} |
| 130 | + className="relative inline-flex items-center rounded-r-md px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-20 focus:outline-offset-0 disabled:opacity-50" |
| 131 | + > |
| 132 | + <span className="sr-only">末页</span> |
| 133 | + <svg className="h-5 w-5" fill="currentColor" viewBox="0 0 20 20"> |
| 134 | + <path fillRule="evenodd" d="M4.293 4.293a1 1 0 011.414 0l5 5a1 1 0 010 1.414l-5 5a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414zm6 0a1 1 0 011.414 0l5 5a1 1 0 010 1.414l-5 5a1 1 0 01-1.414-1.414L14.586 10l-4.293-4.293a1 1 0 010-1.414z" clipRule="evenodd" /> |
| 135 | + </svg> |
| 136 | + </button> |
| 137 | + </nav> |
| 138 | + |
| 139 | + <form onSubmit={handleJumpPage} className="flex items-center gap-2"> |
| 140 | + <span className="text-sm text-gray-700">跳转到</span> |
| 141 | + <input |
| 142 | + type="number" |
| 143 | + min="1" |
| 144 | + max={totalPages} |
| 145 | + value={jumpPage} |
| 146 | + onChange={(e) => setJumpPage(e.target.value)} |
| 147 | + className="w-16 px-2 py-1.5 text-sm border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" |
| 148 | + placeholder="页码" |
| 149 | + /> |
| 150 | + <button |
| 151 | + type="submit" |
| 152 | + className="px-3 py-1.5 text-sm bg-blue-600 text-white rounded-md hover:bg-blue-700 transition-colors duration-200" |
| 153 | + > |
| 154 | + 跳转 |
| 155 | + </button> |
| 156 | + </form> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + ); |
| 161 | +} |
0 commit comments