fixed up tables to use virtualized
This commit is contained in:
parent
7f4a70309c
commit
587fc0f228
2 changed files with 116 additions and 80 deletions
|
|
@ -11,6 +11,7 @@ import {
|
||||||
useReactTable,
|
useReactTable,
|
||||||
} from '@tanstack/react-table';
|
} from '@tanstack/react-table';
|
||||||
import { Fragment, useState } from 'react';
|
import { Fragment, useState } from 'react';
|
||||||
|
import { TableVirtuoso } from 'react-virtuoso';
|
||||||
|
|
||||||
interface DataTableProps<T> {
|
interface DataTableProps<T> {
|
||||||
data: T[];
|
data: T[];
|
||||||
|
|
@ -19,6 +20,8 @@ interface DataTableProps<T> {
|
||||||
className?: string;
|
className?: string;
|
||||||
getRowCanExpand?: (row: Row<T>) => boolean;
|
getRowCanExpand?: (row: Row<T>) => boolean;
|
||||||
renderSubComponent?: (props: { row: Row<T> }) => React.ReactElement;
|
renderSubComponent?: (props: { row: Row<T> }) => React.ReactElement;
|
||||||
|
onRowClick?: (row: T) => void;
|
||||||
|
height?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function DataTable<T>({
|
export function DataTable<T>({
|
||||||
|
|
@ -28,6 +31,8 @@ export function DataTable<T>({
|
||||||
className = '',
|
className = '',
|
||||||
getRowCanExpand,
|
getRowCanExpand,
|
||||||
renderSubComponent,
|
renderSubComponent,
|
||||||
|
onRowClick,
|
||||||
|
height,
|
||||||
}: DataTableProps<T>) {
|
}: DataTableProps<T>) {
|
||||||
const [sorting, setSorting] = useState<SortingState>([]);
|
const [sorting, setSorting] = useState<SortingState>([]);
|
||||||
|
|
||||||
|
|
@ -54,89 +59,121 @@ export function DataTable<T>({
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { rows } = table.getRowModel();
|
||||||
|
|
||||||
|
// For expanded rows, we need to create a flattened list
|
||||||
|
const flatRows = rows.reduce<Array<{ type: 'row' | 'expanded'; row: Row<T> }>>((acc, row) => {
|
||||||
|
acc.push({ type: 'row', row });
|
||||||
|
if (row.getIsExpanded() && renderSubComponent) {
|
||||||
|
acc.push({ type: 'expanded', row });
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('border border-border rounded-lg overflow-auto', className)}>
|
<TableVirtuoso
|
||||||
<table className="w-full">
|
style={{ height: height ? `${height}px` : '100%' }}
|
||||||
<thead className="bg-surface border-b border-border">
|
className={cn('border border-border rounded-lg', className)}
|
||||||
{table.getHeaderGroups().map(headerGroup => (
|
totalCount={flatRows.length}
|
||||||
<tr key={headerGroup.id}>
|
components={{
|
||||||
{headerGroup.headers.map(header => (
|
Table: ({ style, ...props }) => (
|
||||||
<th
|
<table
|
||||||
key={header.id}
|
{...props}
|
||||||
colSpan={header.colSpan}
|
style={{
|
||||||
className="relative px-3 py-2 text-xs font-medium text-text-secondary uppercase tracking-wider text-left"
|
...style,
|
||||||
style={{ width: header.getSize() }}
|
width: '100%',
|
||||||
|
}}
|
||||||
|
className="bg-background"
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
TableRow: props => {
|
||||||
|
const index = props['data-index'] as number;
|
||||||
|
const item = flatRows[index];
|
||||||
|
|
||||||
|
if (!item) return null;
|
||||||
|
|
||||||
|
if (item.type === 'expanded') {
|
||||||
|
return (
|
||||||
|
<tr {...props} className="bg-surface-secondary/50">
|
||||||
|
<td colSpan={item.row.getVisibleCells().length} className="p-0">
|
||||||
|
{renderSubComponent?.({ row: item.row })}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<tr
|
||||||
|
{...props}
|
||||||
|
className="hover:bg-surface border-b border-border cursor-pointer"
|
||||||
|
onClick={() => onRowClick?.(item.row.original)}
|
||||||
|
>
|
||||||
|
{item.row.getVisibleCells().map(cell => (
|
||||||
|
<td
|
||||||
|
key={cell.id}
|
||||||
|
className="px-3 py-2 text-sm text-text-primary"
|
||||||
|
style={{ width: cell.column.getSize() }}
|
||||||
>
|
>
|
||||||
{header.isPlaceholder ? null : (
|
<div className="truncate">
|
||||||
<>
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||||
<div
|
</div>
|
||||||
className={cn(
|
</td>
|
||||||
'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>
|
|
||||||
{/* 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>
|
</tr>
|
||||||
))}
|
);
|
||||||
</thead>
|
},
|
||||||
<tbody>
|
}}
|
||||||
{table.getRowModel().rows.map(row => (
|
fixedHeaderContent={() =>
|
||||||
<Fragment key={row.id}>
|
table.getHeaderGroups().map(headerGroup => (
|
||||||
<tr className="hover:bg-surface border-b border-border">
|
<tr key={headerGroup.id} className="bg-surface border-b border-border">
|
||||||
{row.getVisibleCells().map(cell => (
|
{headerGroup.headers.map(header => (
|
||||||
<td
|
<th
|
||||||
key={cell.id}
|
key={header.id}
|
||||||
className="px-3 py-2 text-sm text-text-primary"
|
colSpan={header.colSpan}
|
||||||
style={{ width: cell.column.getSize() }}
|
className={cn(
|
||||||
>
|
'relative px-3 py-2 text-xs font-medium text-text-secondary uppercase tracking-wider text-left',
|
||||||
<div className="truncate">
|
header.column.getCanSort() && 'cursor-pointer select-none hover:text-text-primary'
|
||||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
)}
|
||||||
|
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" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</td>
|
{/* Column resizer */}
|
||||||
))}
|
{header.column.getCanResize() && (
|
||||||
</tr>
|
<div
|
||||||
{row.getIsExpanded() && renderSubComponent && (
|
onMouseDown={header.getResizeHandler()}
|
||||||
<tr className="bg-surface-secondary/50">
|
onTouchStart={header.getResizeHandler()}
|
||||||
<td colSpan={row.getVisibleCells().length} className="p-0">
|
className={cn(
|
||||||
{renderSubComponent({ row })}
|
'absolute right-0 top-0 h-full w-1 cursor-col-resize user-select-none touch-none',
|
||||||
</td>
|
'hover:bg-primary-500 hover:opacity-100',
|
||||||
</tr>
|
header.column.getIsResizing() ? 'bg-primary-500 opacity-100' : 'bg-border opacity-0'
|
||||||
)}
|
)}
|
||||||
</Fragment>
|
/>
|
||||||
))}
|
)}
|
||||||
</tbody>
|
</>
|
||||||
</table>
|
)}
|
||||||
</div>
|
</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
))
|
||||||
|
}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -368,7 +368,6 @@ export function ExchangesTable() {
|
||||||
data={exchanges || []}
|
data={exchanges || []}
|
||||||
columns={columns}
|
columns={columns}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
className="max-h-[calc(100vh-300px)]"
|
|
||||||
getRowCanExpand={() => true}
|
getRowCanExpand={() => true}
|
||||||
renderSubComponent={renderSubComponent}
|
renderSubComponent={renderSubComponent}
|
||||||
/>
|
/>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue