Pagination component

This commit is contained in:
headlesdev 2025-05-18 12:40:11 +02:00
parent 1bb07bd90c
commit 8f9c560706
2 changed files with 42 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import AddSite from "@/components/dialogues/AddSite";
import Sites from "@/components/cards/Sites";
import axios from "axios";
import { useEffect, useState } from "react";
import Pagination from "@/components/Pagination";
interface SitesPageProps {
username: string;
@ -67,13 +68,12 @@ export default function SitesPage({ username, name }: SitesPageProps) {
))}
</div>
<div className="flex justify-center pt-4">
<div className="join">
<button className="join-item btn" onClick={() => handlePageChange(currentPage - 1)} disabled={currentPage === 1}>«</button>
<button className="join-item btn">Page {currentPage}</button>
<button className="join-item btn" onClick={() => handlePageChange(currentPage + 1)} disabled={currentPage === Math.ceil(total / itemPerPage)}>»</button>
</div>
</div>
<Pagination
currentPage={currentPage}
totalItems={total}
itemsPerPage={itemPerPage}
onPageChange={handlePageChange}
/>
</main>
</Sidebar>
)

35
components/Pagination.tsx Normal file
View File

@ -0,0 +1,35 @@
export default function Pagination({
currentPage,
totalItems,
itemsPerPage,
onPageChange
}: {
currentPage: number;
totalItems: number;
itemsPerPage: number;
onPageChange: (page: number) => void;
}) {
const totalPages = Math.ceil(totalItems / itemsPerPage);
return (
<div className="flex justify-center pt-4">
<div className="join">
<button
className="join-item btn"
onClick={() => onPageChange(currentPage - 1)}
disabled={currentPage === 1}
>
«
</button>
<button className="join-item btn">Page {currentPage}</button>
<button
className="join-item btn"
onClick={() => onPageChange(currentPage + 1)}
disabled={currentPage === totalPages}
>
»
</button>
</div>
</div>
);
}