mirror of
https://github.com/hanzoai/sign.git
synced 2026-10-21 01:21:51 +00:00
react-table went from 8 to 9 with nothing migrated. 9 has no useReactTable and no getCoreRowModel, so the one component every list in the app renders through could not be imported, and ColumnDef takes the table's features as its first type argument, so all 42 column lists stopped typing their rows: 217 of the 498 errors in the app's typecheck. DataTable now names the four features it uses — paging, row selection, column visibility, column sizing — and calls useTable. DataTableColumnDef is ColumnDef over those features, so a column list still says only what a row is. The second type parameter goes: 9's table takes columns of unknown value, and no caller ever passed one. Two things the types turned up. `enableSorting: false` sat on two select columns of a table that has never sorted; without the sorting feature the option does not exist, and neither does the sorting. The organisation insights table erased its three row types through `as unknown as` to feed one DataTable; it renders one per view, typed, and the casts are gone. The server rejects any other view before the component renders. data-table.test.tsx renders the table and drives the instance: headers and cells, a hidden column, a sized one, one-based paging read from the props and reported back, selection by the caller's row id. Against the old file it fails 7 of 7 at the import. Co-authored-by: Hanzo Dev <[email protected]>
230 lines
6.6 KiB
TypeScript
230 lines
6.6 KiB
TypeScript
import React, { useMemo } from 'react';
|
|
|
|
import { Trans } from '@lingui/react/macro';
|
|
import type {
|
|
ColumnDef,
|
|
ColumnVisibilityState,
|
|
PaginationState,
|
|
ReactTable,
|
|
RowData,
|
|
RowSelectionState,
|
|
Updater,
|
|
} from '@tanstack/react-table';
|
|
import {
|
|
columnSizingFeature,
|
|
columnVisibilityFeature,
|
|
flexRender,
|
|
rowPaginationFeature,
|
|
rowSelectionFeature,
|
|
tableFeatures,
|
|
useTable,
|
|
} from '@tanstack/react-table';
|
|
|
|
import { Skeleton } from './skeleton';
|
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from './table';
|
|
|
|
// What a DataTable does beyond listing rows. react-table 9 compiles in only the
|
|
// features named here, and a column option or table method exists in the types
|
|
// only when its feature does.
|
|
const features = tableFeatures({
|
|
columnSizingFeature,
|
|
columnVisibilityFeature,
|
|
rowPaginationFeature,
|
|
rowSelectionFeature,
|
|
});
|
|
|
|
type DataTableFeatures = typeof features;
|
|
|
|
export type { RowSelectionState } from '@tanstack/react-table';
|
|
|
|
export type DataTableInstance<TData extends RowData> = ReactTable<DataTableFeatures, TData>;
|
|
|
|
export type DataTableColumnDef<TData extends RowData> = ColumnDef<DataTableFeatures, TData>;
|
|
|
|
export type DataTableChildren<TData extends RowData> = (
|
|
_table: DataTableInstance<TData>,
|
|
) => React.ReactNode;
|
|
|
|
export interface DataTableProps<TData extends RowData> {
|
|
columns: DataTableColumnDef<TData>[];
|
|
columnVisibility?: ColumnVisibilityState;
|
|
data: TData[];
|
|
onRowClick?: (row: TData) => void;
|
|
rowClassName?: string;
|
|
perPage?: number;
|
|
currentPage?: number;
|
|
totalPages?: number;
|
|
onPaginationChange?: (_page: number, _perPage: number) => void;
|
|
onClearFilters?: () => void;
|
|
emptyState?: React.ReactNode;
|
|
hasFilters?: boolean;
|
|
children?: DataTableChildren<TData>;
|
|
skeleton?: {
|
|
enable: boolean;
|
|
rows: number;
|
|
component?: React.ReactNode;
|
|
};
|
|
error?: {
|
|
enable: boolean;
|
|
component?: React.ReactNode;
|
|
};
|
|
enableRowSelection?: boolean;
|
|
rowSelection?: RowSelectionState;
|
|
onRowSelectionChange?: (selection: RowSelectionState) => void;
|
|
getRowId?: (row: TData) => string;
|
|
}
|
|
|
|
export function DataTable<TData extends RowData>({
|
|
columns,
|
|
columnVisibility,
|
|
data,
|
|
error,
|
|
perPage,
|
|
currentPage,
|
|
totalPages,
|
|
skeleton,
|
|
hasFilters,
|
|
onClearFilters,
|
|
onPaginationChange,
|
|
onRowClick,
|
|
rowClassName,
|
|
children,
|
|
emptyState,
|
|
enableRowSelection,
|
|
rowSelection,
|
|
onRowSelectionChange,
|
|
getRowId,
|
|
}: DataTableProps<TData>) {
|
|
const pagination = useMemo<PaginationState>(() => {
|
|
if (currentPage !== undefined && perPage !== undefined) {
|
|
return {
|
|
pageIndex: currentPage - 1,
|
|
pageSize: perPage,
|
|
};
|
|
}
|
|
|
|
return {
|
|
pageIndex: 0,
|
|
pageSize: 0,
|
|
};
|
|
}, [currentPage, perPage]);
|
|
|
|
const manualPagination = Boolean(currentPage !== undefined && totalPages !== undefined);
|
|
|
|
const onTablePaginationChange = (updater: Updater<PaginationState>) => {
|
|
if (typeof updater === 'function') {
|
|
const newState = updater(pagination);
|
|
|
|
onPaginationChange?.(newState.pageIndex + 1, newState.pageSize);
|
|
} else {
|
|
onPaginationChange?.(updater.pageIndex + 1, updater.pageSize);
|
|
}
|
|
};
|
|
|
|
const onTableRowSelectionChange = (updater: Updater<RowSelectionState>) => {
|
|
if (onRowSelectionChange) {
|
|
const newSelection = typeof updater === 'function' ? updater(rowSelection ?? {}) : updater;
|
|
onRowSelectionChange(newSelection);
|
|
}
|
|
};
|
|
|
|
const table = useTable({
|
|
features,
|
|
data,
|
|
columns,
|
|
state: {
|
|
pagination: manualPagination ? pagination : undefined,
|
|
columnVisibility,
|
|
rowSelection: rowSelection ?? {},
|
|
},
|
|
manualPagination,
|
|
pageCount: totalPages,
|
|
onPaginationChange: onTablePaginationChange,
|
|
enableRowSelection,
|
|
onRowSelectionChange: onTableRowSelectionChange,
|
|
getRowId,
|
|
});
|
|
|
|
return (
|
|
<>
|
|
<div className="rounded-md border">
|
|
<Table>
|
|
<TableHeader>
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
<TableRow key={headerGroup.id}>
|
|
{headerGroup.headers.map((header) => {
|
|
return (
|
|
<TableHead key={header.id}>
|
|
{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'}
|
|
className={rowClassName}
|
|
onClick={() => onRowClick?.(row.original)}
|
|
>
|
|
{row.getVisibleCells().map((cell) => (
|
|
<TableCell
|
|
key={cell.id}
|
|
style={{
|
|
width: `${cell.column.getSize()}px`,
|
|
}}
|
|
>
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
</TableCell>
|
|
))}
|
|
</TableRow>
|
|
))
|
|
) : error?.enable ? (
|
|
<TableRow>
|
|
{error.component ?? (
|
|
<TableCell colSpan={columns.length} className="h-32 text-center">
|
|
<Trans>Something went wrong.</Trans>
|
|
</TableCell>
|
|
)}
|
|
</TableRow>
|
|
) : skeleton?.enable ? (
|
|
Array.from({ length: skeleton.rows }).map((_, i) => (
|
|
<TableRow key={`skeleton-row-${i}`}>{skeleton.component ?? <Skeleton />}</TableRow>
|
|
))
|
|
) : (
|
|
<TableRow>
|
|
<TableCell colSpan={columns.length} className="h-32 text-center">
|
|
{emptyState ?? (
|
|
<>
|
|
<p>
|
|
<Trans>No results found</Trans>
|
|
</p>
|
|
|
|
{hasFilters && onClearFilters !== undefined && (
|
|
<button
|
|
onClick={() => onClearFilters()}
|
|
className="text-foreground mt-1 text-sm"
|
|
>
|
|
<Trans>Clear filters</Trans>
|
|
</button>
|
|
)}
|
|
</>
|
|
)}
|
|
</TableCell>
|
|
</TableRow>
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
|
|
{children && <div className="mt-8 w-full">{children(table)}</div>}
|
|
</>
|
|
);
|
|
}
|