improved datatable

This commit is contained in:
Boki 2025-06-18 07:54:42 -04:00
parent 344478c577
commit 7f4a70309c
2 changed files with 137 additions and 175 deletions

View file

@ -4,29 +4,30 @@ import {
ColumnDef,
flexRender,
getCoreRowModel,
getExpandedRowModel,
getSortedRowModel,
Row,
SortingState,
useReactTable,
} from '@tanstack/react-table';
import { useState } from 'react';
import { TableVirtuoso } from 'react-virtuoso';
import { Fragment, useState } from 'react';
interface DataTableProps<T> {
data: T[];
columns: ColumnDef<T>[];
height?: number;
loading?: boolean;
onRowClick?: (row: T) => void;
className?: string;
getRowCanExpand?: (row: Row<T>) => boolean;
renderSubComponent?: (props: { row: Row<T> }) => React.ReactElement;
}
export function DataTable<T>({
data,
columns,
height,
loading = false,
onRowClick,
className = '',
getRowCanExpand,
renderSubComponent,
}: DataTableProps<T>) {
const [sorting, setSorting] = useState<SortingState>([]);
@ -39,11 +40,12 @@ export function DataTable<T>({
onSortingChange: setSorting,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
enableSorting: true,
getExpandedRowModel: getExpandedRowModel(),
getRowCanExpand,
enableColumnResizing: true,
columnResizeMode: 'onChange',
});
const { rows } = table.getRowModel();
if (loading) {
return (
<div className="flex items-center justify-center h-64 bg-background border border-border rounded-lg">
@ -53,87 +55,88 @@ export function DataTable<T>({
}
return (
<TableVirtuoso
style={height ? { height: `${height}px` } : { height: '100%' }}
className={cn('border border-border rounded-lg', className)}
totalCount={rows.length}
components={{
Table: ({ style, ...props }) => (
<table
{...props}
{...{
style: {
...style,
width: table.getCenterTotalSize(),
minWidth: '100%',
},
}}
className="bg-background"
/>
),
TableRow: props => {
const index = props['data-index'] as number;
const row = rows[index];
return (
<tr
{...props}
className="hover:bg-surface cursor-pointer border-b border-border"
onClick={() => onRowClick?.(row.original)}
>
{row.getVisibleCells().map(cell => (
<td
key={cell.id}
className="px-3 py-2 text-sm text-text-primary border-r border-border"
style={{ width: cell.column.getSize() }}
<div className={cn('border border-border rounded-lg overflow-auto', className)}>
<table className="w-full">
<thead className="bg-surface border-b border-border">
{table.getHeaderGroups().map(headerGroup => (
<tr key={headerGroup.id}>
{headerGroup.headers.map(header => (
<th
key={header.id}
colSpan={header.colSpan}
className="relative px-3 py-2 text-xs font-medium text-text-secondary uppercase tracking-wider text-left"
style={{ width: header.getSize() }}
>
<div className="truncate">
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</div>
</td>
))}
</tr>
);
},
}}
fixedHeaderContent={() =>
table.getHeaderGroups().map(headerGroup => (
<tr key={headerGroup.id} className="bg-surface border-b border-border">
{headerGroup.headers.map(header => (
<th
key={header.id}
colSpan={header.colSpan}
className={cn(
'px-3 py-2 text-xs font-medium text-text-secondary uppercase tracking-wider text-left border-r border-border',
header.column.getCanSort() &&
'cursor-pointer select-none hover:bg-surface-secondary'
)}
style={{ width: header.getSize() }}
onClick={header.column.getToggleSortingHandler()}
>
{header.isPlaceholder ? null : (
<div className="flex items-center justify-between">
<span className="truncate">
{flexRender(header.column.columnDef.header, header.getContext())}
</span>
{header.column.getCanSort() && (
<div className="ml-2 flex-shrink-0">
{header.column.getIsSorted() === 'asc' ? (
<ChevronUpIcon className="h-4 w-4 text-primary-400" />
) : header.column.getIsSorted() === 'desc' ? (
<ChevronDownIcon className="h-4 w-4 text-primary-400" />
) : (
<div className="h-4 w-4" />
{header.isPlaceholder ? null : (
<>
<div
className={cn(
'flex items-center justify-between',
header.column.getCanSort() && 'cursor-pointer select-none hover:text-text-primary'
)}
onClick={header.column.getToggleSortingHandler()}
>
<span className="truncate">
{flexRender(header.column.columnDef.header, header.getContext())}
</span>
{header.column.getCanSort() && (
<div className="ml-2 flex-shrink-0">
{header.column.getIsSorted() === 'asc' ? (
<ChevronUpIcon className="h-4 w-4 text-primary-400" />
) : header.column.getIsSorted() === 'desc' ? (
<ChevronDownIcon className="h-4 w-4 text-primary-400" />
) : (
<div className="h-4 w-4" />
)}
</div>
)}
</div>
)}
</div>
)}
</th>
))}
</tr>
))
}
/>
{/* Column resizer */}
{header.column.getCanResize() && (
<div
onMouseDown={header.getResizeHandler()}
onTouchStart={header.getResizeHandler()}
className={cn(
'absolute right-0 top-0 h-full w-1 cursor-col-resize user-select-none touch-none',
'hover:bg-primary-500 hover:opacity-100',
header.column.getIsResizing() ? 'bg-primary-500 opacity-100' : 'bg-border opacity-0'
)}
/>
)}
</>
)}
</th>
))}
</tr>
))}
</thead>
<tbody>
{table.getRowModel().rows.map(row => (
<Fragment key={row.id}>
<tr className="hover:bg-surface border-b border-border">
{row.getVisibleCells().map(cell => (
<td
key={cell.id}
className="px-3 py-2 text-sm text-text-primary"
style={{ width: cell.column.getSize() }}
>
<div className="truncate">
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</div>
</td>
))}
</tr>
{row.getIsExpanded() && renderSubComponent && (
<tr className="bg-surface-secondary/50">
<td colSpan={row.getVisibleCells().length} className="p-0">
{renderSubComponent({ row })}
</td>
</tr>
)}
</Fragment>
))}
</tbody>
</table>
</div>
);
}
}