2024-03-18 19:51:02 +00:00
|
|
|
import { useMemo} from "react";
|
2024-03-22 12:47:50 +00:00
|
|
|
import type { BaseRecord } from "@refinedev/core";
|
2024-03-15 20:00:25 +00:00
|
|
|
import { useTable } from "@refinedev/react-table";
|
2024-03-12 03:41:05 +00:00
|
|
|
import {
|
2024-03-22 12:47:50 +00:00
|
|
|
type ColumnDef,
|
2024-03-12 03:41:05 +00:00
|
|
|
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"
|
2024-03-18 19:51:02 +00:00
|
|
|
import { Skeleton } from "./ui/skeleton";
|
2024-03-12 03:41:05 +00:00
|
|
|
import { DataTablePagination } from "./table-pagination"
|
|
|
|
|
2024-03-15 20:00:25 +00:00
|
|
|
interface DataTableProps<TData extends BaseRecord = BaseRecord, TValue = unknown> {
|
2024-03-21 14:56:08 +00:00
|
|
|
columns: ColumnDef<TData, TValue>[],
|
|
|
|
resource: string;
|
|
|
|
dataProviderName?: string;
|
2024-03-12 03:41:05 +00:00
|
|
|
}
|
|
|
|
|
2024-03-15 20:00:25 +00:00
|
|
|
export function DataTable<TData extends BaseRecord, TValue>({
|
2024-03-18 19:51:02 +00:00
|
|
|
columns,
|
2024-03-21 14:56:08 +00:00
|
|
|
resource,
|
|
|
|
dataProviderName
|
2024-03-12 03:41:05 +00:00
|
|
|
}: DataTableProps<TData, TValue>) {
|
2024-03-15 20:00:25 +00:00
|
|
|
const table = useTable({
|
2024-03-12 03:41:05 +00:00
|
|
|
columns,
|
2024-03-15 20:00:25 +00:00
|
|
|
refineCoreProps: {
|
2024-03-21 14:56:08 +00:00
|
|
|
resource,
|
|
|
|
dataProviderName: dataProviderName || "default"
|
2024-03-15 20:00:25 +00:00
|
|
|
}
|
2024-03-12 03:41:05 +00:00
|
|
|
})
|
|
|
|
|
2024-03-18 19:51:02 +00:00
|
|
|
const loadingRows = useMemo(() => Array(4).fill({}).map(() => ({
|
|
|
|
getIsSelected: () => false,
|
|
|
|
getVisibleCells: () => columns.map(() => ({
|
|
|
|
column: {
|
|
|
|
columnDef: {
|
|
|
|
cell: <Skeleton className="h-4 w-full bg-primary-1/30" />,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getContext: () => null
|
|
|
|
})),
|
|
|
|
})), [])
|
|
|
|
|
|
|
|
const rows = table.refineCore.tableQueryResult.isLoading ? loadingRows : table.getRowModel().rows
|
|
|
|
|
2024-03-12 03:41:05 +00:00
|
|
|
return (
|
2024-03-18 19:51:02 +00:00
|
|
|
<>
|
2024-03-12 03:41:05 +00:00
|
|
|
<Table>
|
|
|
|
<TableHeader>
|
|
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
|
|
<TableRow key={headerGroup.id}>
|
2024-03-22 13:27:40 +00:00
|
|
|
{headerGroup.headers.map((header, index) => {
|
2024-03-12 03:41:05 +00:00
|
|
|
return (
|
2024-03-22 13:27:40 +00:00
|
|
|
<TableHead key={`FileDataTableHeader_${index}`} 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>
|
2024-03-18 19:51:02 +00:00
|
|
|
{rows.length ? (
|
2024-03-22 13:27:40 +00:00
|
|
|
rows.map((row, index) => (
|
2024-03-12 03:41:05 +00:00
|
|
|
<TableRow
|
2024-03-22 13:27:40 +00:00
|
|
|
key={`FileDataTableRow_${index}`}
|
2024-03-12 03:41:05 +00:00
|
|
|
data-state={row.getIsSelected() && "selected"}
|
2024-03-18 19:51:02 +00:00
|
|
|
className="group"
|
2024-03-12 03:41:05 +00:00
|
|
|
>
|
2024-03-22 13:27:40 +00:00
|
|
|
{row.getVisibleCells().map((cell, index) => (
|
|
|
|
<TableCell key={`FileDataTableCell_${index}`}>
|
2024-03-12 03:41:05 +00:00
|
|
|
{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} />
|
2024-03-18 19:51:02 +00:00
|
|
|
</>
|
2024-03-12 03:41:05 +00:00
|
|
|
)
|
|
|
|
}
|