Spaces:
Running
Running
Download components/Pagination.js from powerllm/anycoder-cf48624d: direct link, hf CLI and curl.
- Browser
- Download file 1.98 kB
-
https://huggingface.co/spaces/powerllm/anycoder-cf48624d/resolve/main/components/Pagination.js
- Command line
-
hf download hf://spaces/powerllm/anycoder-cf48624d/components/Pagination.js
-
curl -L -o Pagination.js https://huggingface.co/spaces/powerllm/anycoder-cf48624d/resolve/main/components/Pagination.js
1.98 kB
| export default function Pagination({ currentPage, totalPages, onPageChange }) { | |
| if (totalPages <= 1) return null | |
| const pages = [] | |
| const maxVisible = 5 | |
| let start = Math.max(1, currentPage - Math.floor(maxVisible / 2)) | |
| let end = Math.min(totalPages, start + maxVisible - 1) | |
| if (end - start < maxVisible - 1) { | |
| start = Math.max(1, end - maxVisible + 1) | |
| } | |
| for (let i = start; i <= end; i++) { | |
| pages.push(i) | |
| } | |
| return ( | |
| <div className="flex items-center space-x-2"> | |
| <button | |
| onClick={() => onPageChange(currentPage - 1)} | |
| disabled={currentPage === 1} | |
| className="btn-secondary px-3 py-1 text-sm disabled:opacity-50 disabled:cursor-not-allowed" | |
| > | |
| Previous | |
| </button> | |
| {start > 1 && ( | |
| <> | |
| <button | |
| onClick={() => onPageChange(1)} | |
| className="btn-secondary px-3 py-1 text-sm" | |
| > | |
| 1 | |
| </button> | |
| {start > 2 && <span className="text-dark-500">...</span>} | |
| </> | |
| )} | |
| {pages.map((page) => ( | |
| <button | |
| key={page} | |
| onClick={() => onPageChange(page)} | |
| className={`px-3 py-1 text-sm rounded-lg transition-all ${ | |
| page === currentPage | |
| ? 'bg-primary-600 text-white' | |
| : 'btn-secondary' | |
| }`} | |
| > | |
| {page} | |
| </button> | |
| ))} | |
| {end < totalPages && ( | |
| <> | |
| {end < totalPages - 1 && <span className="text-dark-500">...</span>} | |
| <button | |
| onClick={() => onPageChange(totalPages)} | |
| className="btn-secondary px-3 py-1 text-sm" | |
| > | |
| {totalPages} | |
| </button> | |
| </> | |
| )} | |
| <button | |
| onClick={() => onPageChange(currentPage + 1)} | |
| disabled={currentPage === totalPages} | |
| className="btn-secondary px-3 py-1 text-sm disabled:opacity-50 disabled:cursor-not-allowed" | |
| > | |
| Next | |
| </button> | |
| </div> | |
| ) | |
| } |