"use client"; import * as React from "react"; import { ColumnDef, SortingState, ColumnFiltersState, flexRender, getCoreRowModel, getFilteredRowModel, getPaginationRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; interface DataTableProps { columns: ColumnDef[]; data: TData[]; searchKey: string; searchPlaceholder?: string; } export function DataTable({ columns, data, searchKey, searchPlaceholder = "Filter records...", }: DataTableProps) { const [sorting, setSorting] = React.useState([]); const [columnFilters, setColumnFilters] = React.useState([]); const [rowSelection, setRowSelection] = React.useState({}); const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), onSortingChange: setSorting, getSortedRowModel: getSortedRowModel(), onColumnFiltersChange: setColumnFilters, getFilteredRowModel: getFilteredRowModel(), onRowSelectionChange: setRowSelection, state: { sorting, columnFilters, rowSelection, }, }); return (
table .getColumn(searchKey) ?.setFilterValue(event.target.value) } className="max-w-sm" />
{/* Wrap table in a responsive container */}
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column .columnDef.header, header.getContext() )} ))} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row .getVisibleCells() .map((cell) => ( {flexRender( cell.column .columnDef.cell, cell.getContext() )} ))} )) ) : ( No results. )}
{table.getFilteredSelectedRowModel().rows.length} of{" "} {table.getFilteredRowModel().rows.length} row(s) selected.
); }