FactuGES_ERP/packages/rdx-ui/src/components/datatable/data-table-pagination.tsx

150 lines
5.2 KiB
TypeScript
Raw Normal View History

2025-11-16 21:11:46 +00:00
import {
Pagination,
PaginationContent,
PaginationItem,
PaginationLink,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@repo/shadcn-ui/components";
import { cn } from "@repo/shadcn-ui/lib/utils";
import type { Table } from "@tanstack/react-table";
2025-10-16 11:18:55 +00:00
import {
2025-10-18 19:57:52 +00:00
ChevronLeftIcon,
ChevronRightIcon,
ChevronsLeftIcon,
2025-11-16 21:11:46 +00:00
ChevronsRightIcon,
2025-10-18 19:57:52 +00:00
} from "lucide-react";
2025-10-16 11:18:55 +00:00
2025-11-16 21:11:46 +00:00
import { useTranslation } from "../../locales/i18n.ts";
import type { DataTableMeta } from "./data-table.tsx";
2025-10-16 11:18:55 +00:00
interface DataTablePaginationProps<TData> {
2025-10-18 19:57:52 +00:00
table: Table<TData>;
className?: string;
2025-10-16 11:18:55 +00:00
}
2026-04-01 18:04:03 +00:00
export function DataTablePagination<TData>({ table, className }: DataTablePaginationProps<TData>) {
2025-10-18 19:57:52 +00:00
const { t } = useTranslation();
2025-10-18 20:33:01 +00:00
const { pageIndex: rawIndex, pageSize: rawSize } = table.getState().pagination;
2025-10-18 22:31:30 +00:00
const meta = table.options.meta as DataTableMeta<TData>;
const totalRows = meta?.totalItems ?? table.getFilteredRowModel().rows.length;
2025-10-18 20:33:01 +00:00
const pageIndex = Number.isFinite(rawIndex) && rawIndex >= 0 ? rawIndex : 0;
const pageSize = Number.isFinite(rawSize) && rawSize > 0 ? rawSize : 10;
2026-04-01 18:04:03 +00:00
const pageCount = Math.max(1, Math.ceil(totalRows / pageSize));
2025-10-18 19:57:52 +00:00
const hasSelected = table.getFilteredSelectedRowModel().rows.length > 0;
2025-10-18 20:33:01 +00:00
const start = totalRows > 0 ? pageIndex * pageSize + 1 : 0;
const end = totalRows > 0 ? Math.min(start + pageSize - 1, totalRows) : 0;
2025-10-18 19:57:52 +00:00
2026-04-01 18:04:03 +00:00
const gotoPage = (index: number) => {
const nextIndex = Math.max(0, Math.min(index, pageCount - 1));
table.setPageIndex(nextIndex);
};
2025-10-18 22:31:30 +00:00
2026-04-01 18:04:03 +00:00
const handlePageSizeChange = (size: string) => {
table.setPageSize(Number(size));
};
2025-10-18 22:31:30 +00:00
2025-10-16 11:18:55 +00:00
return (
2026-03-17 17:49:54 +00:00
<div className={cn("flex items-center justify-between text-muted-foreground", className)}>
2026-04-01 18:04:03 +00:00
<div className="flex flex-1 flex-col items-center gap-4 sm:flex-row">
2025-10-18 19:57:52 +00:00
<span aria-live="polite">
2025-10-18 22:31:30 +00:00
{t("components.datatable.pagination.showing_range", { start, end, total: totalRows })}
2025-10-18 19:57:52 +00:00
</span>
{hasSelected && (
<span aria-live="polite">
{t("components.datatable.pagination.rows_selected", {
count: table.getFilteredSelectedRowModel().rows.length,
total: table.getFilteredRowModel().rows.length,
})}
</span>
)}
2025-11-19 16:05:01 +00:00
</div>
2025-10-18 19:57:52 +00:00
2025-11-19 16:05:01 +00:00
<div className="flex items-center gap-2">
2025-10-18 19:57:52 +00:00
<div className="flex items-center gap-2">
2025-10-18 22:31:30 +00:00
<span>{t("components.datatable.pagination.rows_per_page")}</span>
2025-11-16 21:11:46 +00:00
<Select onValueChange={handlePageSizeChange} value={String(pageSize)}>
2026-04-01 18:04:03 +00:00
<SelectTrigger className="h-8 w-20 border-gray-200 bg-white">
2025-10-18 19:57:52 +00:00
<SelectValue placeholder={String(pageSize)} />
2025-10-16 11:18:55 +00:00
</SelectTrigger>
2025-10-18 19:57:52 +00:00
<SelectContent>
2026-03-30 18:59:27 +00:00
{[5, 10, 20, 25, 30, 40, 50].map((size) => (
2025-10-18 19:57:52 +00:00
<SelectItem key={size} value={String(size)}>
{size}
2025-10-16 11:18:55 +00:00
</SelectItem>
))}
</SelectContent>
</Select>
</div>
2026-04-01 18:04:03 +00:00
2025-10-18 19:57:52 +00:00
<Pagination>
<PaginationContent>
<PaginationItem>
<PaginationLink
aria-label={t("components.datatable.pagination.goto_first_page")}
2026-04-01 18:04:03 +00:00
className="cursor-pointer px-2.5"
2025-11-16 21:11:46 +00:00
isActive={pageIndex > 0}
2026-04-01 18:04:03 +00:00
onClick={() => pageIndex > 0 && gotoPage(0)}
2025-10-18 19:57:52 +00:00
size="sm"
>
<ChevronsLeftIcon className="size-4" />
</PaginationLink>
</PaginationItem>
<PaginationItem>
<PaginationLink
aria-label={t("components.datatable.pagination.goto_previous_page")}
2026-04-01 18:04:03 +00:00
className="cursor-pointer px-2.5"
2025-11-16 21:11:46 +00:00
isActive={pageIndex > 0}
2026-04-01 18:04:03 +00:00
onClick={() => pageIndex > 0 && gotoPage(pageIndex - 1)}
2025-10-18 19:57:52 +00:00
size="sm"
>
<ChevronLeftIcon className="size-4" />
</PaginationLink>
</PaginationItem>
2026-04-01 18:04:03 +00:00
<span aria-live="polite" className="px-2 text-sm text-muted-foreground">
{t("components.datatable.pagination.page_of", {
page: pageIndex + 1,
of: pageCount,
})}
2025-10-18 19:57:52 +00:00
</span>
<PaginationItem>
<PaginationLink
aria-label={t("components.datatable.pagination.goto_next_page")}
2026-04-01 18:04:03 +00:00
className="cursor-pointer px-2.5"
2025-11-16 21:11:46 +00:00
isActive={pageIndex < pageCount - 1}
2026-04-01 18:04:03 +00:00
onClick={() => pageIndex < pageCount - 1 && gotoPage(pageIndex + 1)}
2025-10-18 19:57:52 +00:00
size="sm"
>
<ChevronRightIcon className="size-4" />
</PaginationLink>
</PaginationItem>
<PaginationItem>
<PaginationLink
aria-label={t("components.datatable.pagination.goto_last_page")}
2026-04-01 18:04:03 +00:00
className="cursor-pointer px-2.5"
2025-11-16 21:11:46 +00:00
isActive={pageIndex < pageCount - 1}
2026-04-01 18:04:03 +00:00
onClick={() => pageIndex < pageCount - 1 && gotoPage(pageCount - 1)}
2025-10-18 19:57:52 +00:00
size="sm"
>
<ChevronsRightIcon className="size-4" />
</PaginationLink>
</PaginationItem>
</PaginationContent>
</Pagination>
2025-10-16 11:18:55 +00:00
</div>
</div>
2025-10-18 19:57:52 +00:00
);
2025-11-16 21:11:46 +00:00
}