|
| 1 | +import { |
| 2 | + type ColumnDef, |
| 3 | + flexRender, |
| 4 | + getCoreRowModel, |
| 5 | + getPaginationRowModel, |
| 6 | + useReactTable, |
| 7 | +} from "@tanstack/react-table" |
| 8 | +import { |
| 9 | + ChevronLeft, |
| 10 | + ChevronRight, |
| 11 | + ChevronsLeft, |
| 12 | + ChevronsRight, |
| 13 | +} from "lucide-react" |
| 14 | + |
| 15 | +import { Button } from "@/components/ui/button" |
| 16 | +import { |
| 17 | + Select, |
| 18 | + SelectContent, |
| 19 | + SelectItem, |
| 20 | + SelectTrigger, |
| 21 | + SelectValue, |
| 22 | +} from "@/components/ui/select" |
| 23 | +import { |
| 24 | + Table, |
| 25 | + TableBody, |
| 26 | + TableCell, |
| 27 | + TableHead, |
| 28 | + TableHeader, |
| 29 | + TableRow, |
| 30 | +} from "@/components/ui/table" |
| 31 | + |
| 32 | +interface DataTableProps<TData, TValue> { |
| 33 | + columns: ColumnDef<TData, TValue>[] |
| 34 | + data: TData[] |
| 35 | +} |
| 36 | + |
| 37 | +export function DataTable<TData, TValue>({ |
| 38 | + columns, |
| 39 | + data, |
| 40 | +}: DataTableProps<TData, TValue>) { |
| 41 | + const table = useReactTable({ |
| 42 | + data, |
| 43 | + columns, |
| 44 | + getCoreRowModel: getCoreRowModel(), |
| 45 | + getPaginationRowModel: getPaginationRowModel(), |
| 46 | + }) |
| 47 | + |
| 48 | + return ( |
| 49 | + <div className="flex flex-col gap-4"> |
| 50 | + <Table> |
| 51 | + <TableHeader> |
| 52 | + {table.getHeaderGroups().map((headerGroup) => ( |
| 53 | + <TableRow key={headerGroup.id} className="hover:bg-transparent"> |
| 54 | + {headerGroup.headers.map((header) => { |
| 55 | + return ( |
| 56 | + <TableHead key={header.id}> |
| 57 | + {header.isPlaceholder |
| 58 | + ? null |
| 59 | + : flexRender( |
| 60 | + header.column.columnDef.header, |
| 61 | + header.getContext(), |
| 62 | + )} |
| 63 | + </TableHead> |
| 64 | + ) |
| 65 | + })} |
| 66 | + </TableRow> |
| 67 | + ))} |
| 68 | + </TableHeader> |
| 69 | + <TableBody> |
| 70 | + {table.getRowModel().rows.length ? ( |
| 71 | + table.getRowModel().rows.map((row) => ( |
| 72 | + <TableRow |
| 73 | + key={row.id} |
| 74 | + > |
| 75 | + {row.getVisibleCells().map((cell) => ( |
| 76 | + <TableCell key={cell.id}> |
| 77 | + {flexRender(cell.column.columnDef.cell, cell.getContext())} |
| 78 | + </TableCell> |
| 79 | + ))} |
| 80 | + </TableRow> |
| 81 | + )) |
| 82 | + ) : ( |
| 83 | + <TableRow className="hover:bg-transparent"> |
| 84 | + <TableCell |
| 85 | + colSpan={columns.length} |
| 86 | + className="h-32 text-center text-muted-foreground" |
| 87 | + > |
| 88 | + No results found. |
| 89 | + </TableCell> |
| 90 | + </TableRow> |
| 91 | + )} |
| 92 | + </TableBody> |
| 93 | + </Table> |
| 94 | + |
| 95 | + {table.getPageCount() > 1 && ( |
| 96 | + <div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4 p-4 border-t bg-muted/20"> |
| 97 | + <div className="flex flex-col sm:flex-row sm:items-center gap-4"> |
| 98 | + <div className="text-sm text-muted-foreground"> |
| 99 | + Showing{" "} |
| 100 | + {table.getState().pagination.pageIndex * |
| 101 | + table.getState().pagination.pageSize + |
| 102 | + 1}{" "} |
| 103 | + to{" "} |
| 104 | + {Math.min( |
| 105 | + (table.getState().pagination.pageIndex + 1) * |
| 106 | + table.getState().pagination.pageSize, |
| 107 | + data.length, |
| 108 | + )}{" "} |
| 109 | + of{" "} |
| 110 | + <span className="font-medium text-foreground">{data.length}</span>{" "} |
| 111 | + entries |
| 112 | + </div> |
| 113 | + <div className="flex items-center gap-x-2"> |
| 114 | + <p className="text-sm text-muted-foreground">Rows per page</p> |
| 115 | + <Select |
| 116 | + value={`${table.getState().pagination.pageSize}`} |
| 117 | + onValueChange={(value) => { |
| 118 | + table.setPageSize(Number(value)) |
| 119 | + }} |
| 120 | + > |
| 121 | + <SelectTrigger className="h-8 w-[70px]"> |
| 122 | + <SelectValue |
| 123 | + placeholder={table.getState().pagination.pageSize} |
| 124 | + /> |
| 125 | + </SelectTrigger> |
| 126 | + <SelectContent side="top"> |
| 127 | + {[5, 10, 25, 50].map((pageSize) => ( |
| 128 | + <SelectItem key={pageSize} value={`${pageSize}`}> |
| 129 | + {pageSize} |
| 130 | + </SelectItem> |
| 131 | + ))} |
| 132 | + </SelectContent> |
| 133 | + </Select> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + |
| 137 | + <div className="flex items-center gap-x-6"> |
| 138 | + <div className="flex items-center gap-x-1 text-sm text-muted-foreground"> |
| 139 | + <span>Page</span> |
| 140 | + <span className="font-medium text-foreground"> |
| 141 | + {table.getState().pagination.pageIndex + 1} |
| 142 | + </span> |
| 143 | + <span>of</span> |
| 144 | + <span className="font-medium text-foreground"> |
| 145 | + {table.getPageCount()} |
| 146 | + </span> |
| 147 | + </div> |
| 148 | + |
| 149 | + <div className="flex items-center gap-x-1"> |
| 150 | + <Button |
| 151 | + variant="outline" |
| 152 | + size="sm" |
| 153 | + className="h-8 w-8 p-0" |
| 154 | + onClick={() => table.setPageIndex(0)} |
| 155 | + disabled={!table.getCanPreviousPage()} |
| 156 | + > |
| 157 | + <span className="sr-only">Go to first page</span> |
| 158 | + <ChevronsLeft className="h-4 w-4" /> |
| 159 | + </Button> |
| 160 | + <Button |
| 161 | + variant="outline" |
| 162 | + size="sm" |
| 163 | + className="h-8 w-8 p-0" |
| 164 | + onClick={() => table.previousPage()} |
| 165 | + disabled={!table.getCanPreviousPage()} |
| 166 | + > |
| 167 | + <span className="sr-only">Go to previous page</span> |
| 168 | + <ChevronLeft className="h-4 w-4" /> |
| 169 | + </Button> |
| 170 | + <Button |
| 171 | + variant="outline" |
| 172 | + size="sm" |
| 173 | + className="h-8 w-8 p-0" |
| 174 | + onClick={() => table.nextPage()} |
| 175 | + disabled={!table.getCanNextPage()} |
| 176 | + > |
| 177 | + <span className="sr-only">Go to next page</span> |
| 178 | + <ChevronRight className="h-4 w-4" /> |
| 179 | + </Button> |
| 180 | + <Button |
| 181 | + variant="outline" |
| 182 | + size="sm" |
| 183 | + className="h-8 w-8 p-0" |
| 184 | + onClick={() => table.setPageIndex(table.getPageCount() - 1)} |
| 185 | + disabled={!table.getCanNextPage()} |
| 186 | + > |
| 187 | + <span className="sr-only">Go to last page</span> |
| 188 | + <ChevronsRight className="h-4 w-4" /> |
| 189 | + </Button> |
| 190 | + </div> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + )} |
| 194 | + </div> |
| 195 | + ) |
| 196 | +} |
0 commit comments