diff --git a/src/components/common/data-table.tsx b/src/components/common/data-table.tsx new file mode 100644 index 0000000..a07baca --- /dev/null +++ b/src/components/common/data-table.tsx @@ -0,0 +1,152 @@ +"use client"; + +import { + flexRender, + getCoreRowModel, + getFilteredRowModel, + getPaginationRowModel, + getSortedRowModel, + useReactTable, + type ColumnDef, + type SortingState, +} from "@tanstack/react-table"; +import { ArrowUpDown, ChevronLeft, ChevronRight, Search } from "lucide-react"; +import { useMemo, useState } from "react"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { cn } from "@/lib/utils"; + +export function DataTable({ + data, + columns, + searchPlaceholder = "Search", + compact = false, +}: { + data: TData[]; + columns: ColumnDef[]; + searchPlaceholder?: string; + compact?: boolean; +}) { + const [globalFilter, setGlobalFilter] = useState(""); + const [sorting, setSorting] = useState([]); + const memoColumns = useMemo(() => columns, [columns]); + const table = useReactTable({ + data, + columns: memoColumns, + state: { globalFilter, sorting }, + onGlobalFilterChange: setGlobalFilter, + onSortingChange: setSorting, + getCoreRowModel: getCoreRowModel(), + getFilteredRowModel: getFilteredRowModel(), + getSortedRowModel: getSortedRowModel(), + getPaginationRowModel: getPaginationRowModel(), + initialState: { pagination: { pageSize: compact ? 8 : 10 } }, + }); + + return ( +
+
+
+ + setGlobalFilter(event.target.value)} + placeholder={searchPlaceholder} + className="pl-8" + /> +
+
+ {table.getFilteredRowModel().rows.length} results +
+
+
+
+ + + {table.getHeaderGroups().map((headerGroup) => ( + + {headerGroup.headers.map((header) => ( + + ))} + + ))} + + + {table.getRowModel().rows.length ? ( + table.getRowModel().rows.map((row) => ( + + {row.getVisibleCells().map((cell) => ( + + ))} + + )) + ) : ( + + + + )} + +
+ {header.isPlaceholder ? null : ( + + )} +
+ {flexRender(cell.column.columnDef.cell, cell.getContext())} +
+ No matching records. +
+
+
+
+ + Page {table.getState().pagination.pageIndex + 1} of{" "} + {Math.max(1, table.getPageCount())} + +
+ + +
+
+
+ ); +}