2024-03-15 20:00:25 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { BaseRecord } from "@refinedev/core";
|
|
|
|
import { useTable } from "@refinedev/react-table";
|
2024-03-12 03:41:05 +00:00
|
|
|
import {
|
|
|
|
ColumnDef,
|
|
|
|
flexRender,
|
2024-03-15 20:00:25 +00:00
|
|
|
} from "@tanstack/react-table";
|
2024-03-12 03:41:05 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
Table,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableHead,
|
|
|
|
TableHeader,
|
|
|
|
TableRow,
|
|
|
|
} from "./ui/table"
|
|
|
|
import { DataTablePagination } from "./table-pagination"
|
|
|
|
|
2024-03-15 20:00:25 +00:00
|
|
|
interface DataTableProps<TData extends BaseRecord = BaseRecord, TValue = unknown> {
|
2024-03-12 03:41:05 +00:00
|
|
|
columns: ColumnDef<TData, TValue>[]
|
|
|
|
}
|
|
|
|
|
2024-03-15 20:00:25 +00:00
|
|
|
export function DataTable<TData extends BaseRecord, TValue>({
|
|
|
|
columns
|
2024-03-12 03:41:05 +00:00
|
|
|
}: DataTableProps<TData, TValue>) {
|
2024-03-15 20:00:25 +00:00
|
|
|
const [hoveredRowId, setHoveredRowId] = useState<string>("");
|
|
|
|
|
|
|
|
const table = useTable({
|
2024-03-12 03:41:05 +00:00
|
|
|
columns,
|
2024-03-15 20:00:25 +00:00
|
|
|
meta: {
|
|
|
|
hoveredRowId,
|
|
|
|
},
|
|
|
|
refineCoreProps: {
|
|
|
|
resource: "files"
|
|
|
|
}
|
2024-03-12 03:41:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="rounded-lg">
|
|
|
|
<Table>
|
|
|
|
<TableHeader>
|
|
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
|
|
<TableRow key={headerGroup.id}>
|
|
|
|
{headerGroup.headers.map((header) => {
|
|
|
|
return (
|
2024-03-15 20:00:25 +00:00
|
|
|
<TableHead key={header.id} style={{ width: header.getSize() }}>
|
2024-03-12 03:41:05 +00:00
|
|
|
{header.isPlaceholder
|
|
|
|
? null
|
|
|
|
: flexRender(
|
|
|
|
header.column.columnDef.header,
|
|
|
|
header.getContext()
|
|
|
|
)}
|
|
|
|
</TableHead>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</TableRow>
|
|
|
|
))}
|
|
|
|
</TableHeader>
|
|
|
|
<TableBody>
|
|
|
|
{table.getRowModel().rows?.length ? (
|
|
|
|
table.getRowModel().rows.map((row) => (
|
|
|
|
<TableRow
|
|
|
|
key={row.id}
|
|
|
|
data-state={row.getIsSelected() && "selected"}
|
2024-03-15 20:00:25 +00:00
|
|
|
onMouseEnter={() => {
|
|
|
|
console.log(hoveredRowId, row.id);
|
|
|
|
setHoveredRowId(row.id)
|
|
|
|
}}
|
2024-03-12 03:41:05 +00: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.
|
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
)}
|
|
|
|
</TableBody>
|
|
|
|
</Table>
|
|
|
|
<DataTablePagination table={table} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|