Spaces:
Running

anycoder-cf48624d / components /Pagination.js
powerllm's picture
Upload components/Pagination.js with huggingface_hub
efe38cd verified
Raw History Blame Contribute Delete
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>
)
}