gitscope
← marzneshin / v0.5.4 / dashboard/src/features/entity-table/components/table-pagination.tsx
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/entity-table/components/table-pagination.tsx · TypeScript
Blame Raw
  1import {
  2    ChevronLeftIcon,
  3    ChevronRightIcon,
  4    DoubleArrowLeftIcon,
  5    DoubleArrowRightIcon,
  6} from "@radix-ui/react-icons";
  7import {
  8    Button,
  9    Select,
 10    SelectContent,
 11    SelectItem,
 12    SelectTrigger,
 13    SelectValue,
 14} from "@marzneshin/components";
 15import { useTranslation } from "react-i18next";
 16import { useEffect } from "react";
 17import { Table } from "@tanstack/react-table"
 18
 19export function DataTablePagination<TData>({ table }: { table: Table<TData> }) {
 20    const { t } = useTranslation();
 21
 22    useEffect(() => {
 23        if (table.getState().pagination.pageIndex === 1) table.setPageIndex(2);
 24    }, [table]);
 25
 26    return (
 27        <div className="flex justify-between items-center p-2 w-full">
 28            {table.options.onRowSelectionChange && (
 29                <div className="flex-1 text-sm text-muted-foreground">
 30                    {table.getFilteredSelectedRowModel().rows.length} {"/"}
 31                    {table.getFilteredRowModel().rows.length} {t("table.selected")}
 32                </div>
 33            )}
 34            <div className="flex justify-between items-center space-x-6 w-full lg:space-x-8">
 35                <div className="flex items-center space-x-2">
 36                    <label id="rows-per-page-label" htmlFor="rows-per-page-select" className="text-sm font-medium">
 37                        {t("table.row-per-page")}
 38                    </label>
 39                    <Select
 40                        value={`${table.getState().pagination.pageSize}`}
 41                        onValueChange={(value) => {
 42                            table.setPageSize(Number(value));
 43                            table.setPageIndex(1);
 44                        }}
 45                        aria-labelledby="rows-per-page-label"
 46                    >
 47                        <SelectTrigger
 48                            className="h-8 w-[70px]"
 49                            aria-controls="rows-per-page-listbox"
 50                            aria-labelledby="rows-per-page-label"
 51                        >
 52                            <SelectValue placeholder={table.getState().pagination.pageSize} />
 53                        </SelectTrigger>
 54                        <SelectContent
 55                            id="rows-per-page-listbox"
 56                            side="top"
 57                            aria-labelledby="rows-per-page-label"
 58                        >
 59                            {[10, 20, 50, 100].map((pageSize) => (
 60                                <SelectItem
 61                                    key={pageSize}
 62                                    value={`${pageSize}`}
 63                                    aria-selected={table.getState().pagination.pageSize === pageSize}
 64                                >
 65                                    {pageSize}
 66                                </SelectItem>
 67                            ))}
 68                        </SelectContent>
 69                    </Select>
 70                </div>
 71                <div className="flex justify-center items-center text-sm font-medium w-[100px]">
 72                    Page {table.getState().pagination.pageIndex + 1} of{" "}
 73                    {table.getPageCount() - 1}
 74                </div>
 75                <div className="flex items-center space-x-2">
 76                    <Button
 77                        variant="outline"
 78                        className="hidden p-0 w-8 h-8 lg:flex"
 79                        onClick={() => table.setPageIndex(1)}
 80                        disabled={!table.getCanPreviousPage()}
 81                    >
 82                        <span className="sr-only">Go to first page</span>
 83                        <DoubleArrowLeftIcon className="w-4 h-4" />
 84                    </Button>
 85                    <Button
 86                        variant="outline"
 87                        className="p-0 w-8 h-8"
 88                        onClick={() => table.previousPage()}
 89                        disabled={!table.getCanPreviousPage()}
 90                    >
 91                        <span className="sr-only">Go to previous page</span>
 92                        <ChevronLeftIcon className="w-4 h-4" />
 93                    </Button>
 94                    <Button
 95                        variant="outline"
 96                        className="p-0 w-8 h-8"
 97                        onClick={() => table.nextPage()}
 98                        disabled={!table.getCanNextPage()}
 99                    >
100                        <span className="sr-only">Go to next page</span>
101                        <ChevronRightIcon className="w-4 h-4" />
102                    </Button>
103                    <Button
104                        variant="outline"
105                        className="hidden p-0 w-8 h-8 lg:flex"
106                        onClick={() => table.setPageIndex(table.getPageCount() - 1)}
107                        disabled={!table.getCanNextPage()}
108                    >
109                        <span className="sr-only">Go to last page</span>
110                        <DoubleArrowRightIcon className="w-4 h-4" />
111                    </Button>
112                </div>
113            </div>
114        </div>
115    );
116}