2025-04-12 15:04:32 -04:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import {
|
|
|
|
ColumnDef,
|
|
|
|
flexRender,
|
|
|
|
getCoreRowModel,
|
|
|
|
useReactTable,
|
|
|
|
getPaginationRowModel,
|
|
|
|
SortingState,
|
|
|
|
getSortedRowModel,
|
|
|
|
ColumnFiltersState,
|
|
|
|
getFilteredRowModel
|
|
|
|
} from "@tanstack/react-table";
|
|
|
|
import {
|
|
|
|
Table,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableHead,
|
|
|
|
TableHeader,
|
|
|
|
TableRow
|
|
|
|
} from "@/components/ui/table";
|
|
|
|
import { Button } from "@app/components/ui/button";
|
|
|
|
import { useState } from "react";
|
|
|
|
import { Input } from "@app/components/ui/input";
|
|
|
|
import { DataTablePagination } from "@app/components/DataTablePagination";
|
|
|
|
import { Plus, Search } from "lucide-react";
|
2025-04-13 14:21:18 -04:00
|
|
|
import {
|
|
|
|
Card,
|
|
|
|
CardContent,
|
|
|
|
CardHeader,
|
|
|
|
CardTitle
|
|
|
|
} from "@app/components/ui/card";
|
2025-04-12 15:04:32 -04:00
|
|
|
|
|
|
|
interface DataTableProps<TData, TValue> {
|
|
|
|
columns: ColumnDef<TData, TValue>[];
|
|
|
|
data: TData[];
|
|
|
|
title?: string;
|
|
|
|
addButtonText?: string;
|
|
|
|
onAdd?: () => void;
|
|
|
|
searchPlaceholder?: string;
|
|
|
|
searchColumn?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function DataTable<TData, TValue>({
|
|
|
|
columns,
|
|
|
|
data,
|
|
|
|
title,
|
|
|
|
addButtonText,
|
|
|
|
onAdd,
|
|
|
|
searchPlaceholder = "Search...",
|
|
|
|
searchColumn = "name"
|
|
|
|
}: DataTableProps<TData, TValue>) {
|
|
|
|
const [sorting, setSorting] = useState<SortingState>([]);
|
|
|
|
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
|
2025-04-13 14:21:18 -04:00
|
|
|
const [globalFilter, setGlobalFilter] = useState<any>([]);
|
2025-04-12 15:04:32 -04:00
|
|
|
|
|
|
|
const table = useReactTable({
|
|
|
|
data,
|
|
|
|
columns,
|
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
|
getPaginationRowModel: getPaginationRowModel(),
|
|
|
|
onSortingChange: setSorting,
|
|
|
|
getSortedRowModel: getSortedRowModel(),
|
|
|
|
onColumnFiltersChange: setColumnFilters,
|
|
|
|
getFilteredRowModel: getFilteredRowModel(),
|
2025-04-13 14:21:18 -04:00
|
|
|
onGlobalFilterChange: setGlobalFilter,
|
2025-04-12 15:04:32 -04:00
|
|
|
initialState: {
|
|
|
|
pagination: {
|
|
|
|
pageSize: 20,
|
|
|
|
pageIndex: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
state: {
|
|
|
|
sorting,
|
2025-04-13 14:21:18 -04:00
|
|
|
columnFilters,
|
|
|
|
globalFilter
|
2025-04-12 15:04:32 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="container mx-auto max-w-12xl">
|
|
|
|
<Card>
|
|
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-4">
|
2025-04-13 14:21:18 -04:00
|
|
|
<div className="flex items-center max-w-sm w-full relative mr-2">
|
2025-04-12 15:04:32 -04:00
|
|
|
<Input
|
|
|
|
placeholder={searchPlaceholder}
|
2025-04-13 14:21:18 -04:00
|
|
|
value={globalFilter ?? ""}
|
|
|
|
onChange={(e) =>
|
|
|
|
table.setGlobalFilter(String(e.target.value))
|
2025-04-12 15:04:32 -04:00
|
|
|
}
|
|
|
|
className="w-full pl-8"
|
|
|
|
/>
|
|
|
|
<Search className="h-4 w-4 absolute left-2 top-1/2 transform -translate-y-1/2 text-muted-foreground" />
|
|
|
|
</div>
|
|
|
|
{onAdd && addButtonText && (
|
|
|
|
<Button onClick={onAdd}>
|
|
|
|
<Plus className="mr-2 h-4 w-4" />
|
|
|
|
{addButtonText}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</CardHeader>
|
|
|
|
<CardContent>
|
|
|
|
<Table>
|
|
|
|
<TableHeader>
|
|
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
|
|
<TableRow key={headerGroup.id}>
|
|
|
|
{headerGroup.headers.map((header) => (
|
|
|
|
<TableHead key={header.id}>
|
|
|
|
{header.isPlaceholder
|
|
|
|
? null
|
|
|
|
: flexRender(
|
2025-04-13 14:21:18 -04:00
|
|
|
header.column.columnDef
|
|
|
|
.header,
|
2025-04-12 15:04:32 -04:00
|
|
|
header.getContext()
|
|
|
|
)}
|
|
|
|
</TableHead>
|
|
|
|
))}
|
|
|
|
</TableRow>
|
|
|
|
))}
|
|
|
|
</TableHeader>
|
|
|
|
<TableBody>
|
|
|
|
{table.getRowModel().rows?.length ? (
|
|
|
|
table.getRowModel().rows.map((row) => (
|
|
|
|
<TableRow
|
|
|
|
key={row.id}
|
2025-04-13 14:21:18 -04:00
|
|
|
data-state={
|
|
|
|
row.getIsSelected() && "selected"
|
|
|
|
}
|
2025-04-12 15:04:32 -04:00
|
|
|
>
|
|
|
|
{row.getVisibleCells().map((cell) => (
|
|
|
|
<TableCell key={cell.id}>
|
|
|
|
{flexRender(
|
|
|
|
cell.column.columnDef.cell,
|
|
|
|
cell.getContext()
|
|
|
|
)}
|
|
|
|
</TableCell>
|
|
|
|
))}
|
|
|
|
</TableRow>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<TableRow>
|
|
|
|
<TableCell
|
|
|
|
colSpan={columns.length}
|
|
|
|
className="h-24 text-center"
|
|
|
|
>
|
|
|
|
No results found.
|
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
)}
|
|
|
|
</TableBody>
|
|
|
|
</Table>
|
|
|
|
<div className="mt-4">
|
|
|
|
<DataTablePagination table={table} />
|
|
|
|
</div>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|