"use client"; import * as React from "react"; import { type PaginationState, type ColumnSizingState, type ColumnDef, type SortingState, type ColumnFiltersState, flexRender, getCoreRowModel, useReactTable, getSortedRowModel, getFilteredRowModel, getPaginationRowModel, } from "@tanstack/react-table"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Input } from "../ui/input"; import { DataTablePagination } from "./pagination"; interface DataTableProps { columns: ColumnDef[]; data: TData[]; } export function DataTable({ columns, data, }: DataTableProps) { const [pagination, setPagination] = React.useState({ pageSize: 25, pageIndex: 0, }); const [sorting, setSorting] = React.useState([]); const [columnFilters, setColumnFilters] = React.useState( [], ); const [columnSizing, setColumnSizing] = React.useState({}); const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), getFilteredRowModel: getFilteredRowModel(), onPaginationChange: setPagination, onColumnFiltersChange: setColumnFilters, onSortingChange: setSorting, onColumnSizingChange: setColumnSizing, state: { sorting, columnFilters, columnSizing, pagination, }, }); return (
{JSON.stringify(sorting)}
table.getColumn("email")?.setFilterValue(event.target.value) } className="max-w-sm" />
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( {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. )}
); }