2024-06-11 16:48:09 +00:00
|
|
|
import { DEFAULT_PAGE_SIZES, INITIAL_PAGE_INDEX } from "@/lib/hooks";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
import { Button, Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/ui";
|
|
|
|
|
import { Table } from "@tanstack/react-table";
|
2024-06-13 11:09:26 +00:00
|
|
|
import { t } from "i18next";
|
2024-06-11 16:48:09 +00:00
|
|
|
import {
|
|
|
|
|
ChevronLeftIcon,
|
|
|
|
|
ChevronRightIcon,
|
|
|
|
|
ChevronsLeftIcon,
|
|
|
|
|
ChevronsRightIcon,
|
|
|
|
|
} from "lucide-react";
|
|
|
|
|
import { useMemo } from "react";
|
|
|
|
|
|
|
|
|
|
export type DataTablePaginationProps<TData> = {
|
|
|
|
|
table: Table<TData>;
|
|
|
|
|
className?: string;
|
|
|
|
|
visible?: boolean | "auto";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function DataTablePagination<TData>({
|
|
|
|
|
table,
|
|
|
|
|
className,
|
|
|
|
|
visible = "auto",
|
|
|
|
|
}: DataTablePaginationProps<TData>) {
|
|
|
|
|
const isVisible = useMemo(() => visible === true, [visible]);
|
|
|
|
|
const isAuto = useMemo(() => visible === "auto", [visible]);
|
|
|
|
|
|
|
|
|
|
if (!isVisible || (isAuto && table.getPageCount() < 1)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn("flex items-center justify-between px-2", className)}>
|
|
|
|
|
<div className='flex-1 text-base text-muted-foreground'>
|
2024-06-13 11:09:26 +00:00
|
|
|
{t("common.rows_selected", {
|
|
|
|
|
count: table.getFilteredSelectedRowModel().rows.length,
|
|
|
|
|
total: table.getFilteredRowModel().rows.length,
|
|
|
|
|
})}
|
2024-06-11 16:48:09 +00:00
|
|
|
</div>
|
|
|
|
|
<div className='flex items-center space-x-6 lg:space-x-8'>
|
|
|
|
|
<div className='flex items-center space-x-2'>
|
2024-06-13 11:09:26 +00:00
|
|
|
<p className='text-base font-medium'>{t("common.rows_per_page")}</p>
|
|
|
|
|
|
2024-06-11 16:48:09 +00:00
|
|
|
<Select
|
|
|
|
|
value={`${table.getState().pagination.pageSize}`}
|
|
|
|
|
onValueChange={(value) => {
|
|
|
|
|
table.setPageSize(Number(value));
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<SelectTrigger className='h-8 w-[70px]'>
|
|
|
|
|
<SelectValue placeholder={table.getState().pagination.pageSize} />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent side='top'>
|
|
|
|
|
{DEFAULT_PAGE_SIZES.map((pageSize) => (
|
|
|
|
|
<SelectItem key={pageSize} value={`${pageSize}`}>
|
|
|
|
|
{pageSize}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
</div>
|
|
|
|
|
<div className='flex w-[100px] items-center justify-center text-base font-medium'>
|
2024-06-13 11:09:26 +00:00
|
|
|
{t("common.num_page_of_total", {
|
|
|
|
|
count: table.getState().pagination.pageIndex + 1,
|
|
|
|
|
total: table.getPageCount(),
|
|
|
|
|
})}
|
2024-06-11 16:48:09 +00:00
|
|
|
</div>
|
|
|
|
|
<div className='flex items-center space-x-2'>
|
|
|
|
|
<Button
|
|
|
|
|
variant='outline'
|
|
|
|
|
className='hidden w-8 h-8 p-0 lg:flex'
|
|
|
|
|
onClick={() => table.setPageIndex(INITIAL_PAGE_INDEX)}
|
|
|
|
|
disabled={!table.getCanPreviousPage()}
|
|
|
|
|
>
|
2024-06-13 11:09:26 +00:00
|
|
|
<span className='sr-only'>{t("common.go_to_first_page")}</span>
|
2024-06-11 16:48:09 +00:00
|
|
|
<ChevronsLeftIcon className='w-4 h-4' />
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant='outline'
|
|
|
|
|
className='w-8 h-8 p-0'
|
|
|
|
|
onClick={() => table.previousPage()}
|
|
|
|
|
disabled={!table.getCanPreviousPage()}
|
|
|
|
|
>
|
2024-06-13 11:09:26 +00:00
|
|
|
<span className='sr-only'>{t("common.go_to_prev_page")}</span>
|
2024-06-11 16:48:09 +00:00
|
|
|
<ChevronLeftIcon className='w-4 h-4' />
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant='outline'
|
|
|
|
|
className='w-8 h-8 p-0'
|
|
|
|
|
onClick={() => table.nextPage()}
|
|
|
|
|
disabled={!table.getCanNextPage()}
|
|
|
|
|
>
|
2024-06-13 11:09:26 +00:00
|
|
|
<span className='sr-only'>{t("common.go_to_next_page")}</span>
|
2024-06-11 16:48:09 +00:00
|
|
|
<ChevronRightIcon className='w-4 h-4' />
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant='outline'
|
|
|
|
|
className='hidden w-8 h-8 p-0 lg:flex'
|
|
|
|
|
onClick={() => table.setPageIndex(table.getPageCount() + 1)}
|
|
|
|
|
disabled={!table.getCanNextPage()}
|
|
|
|
|
>
|
2024-06-13 11:09:26 +00:00
|
|
|
<span className='sr-only'>{t("common.go_to_last_page")}</span>
|
2024-06-11 16:48:09 +00:00
|
|
|
<ChevronsRightIcon className='w-4 h-4' />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|