import React, { type ReactNode, type ComponentProps } from 'react'; import { type TableProps as RACTableProps, ResizableTableContainer, ColumnResizer, Table as RACTable, TableBody, Cell, } from 'react-aria-components'; import { TableHeader } from './TableHeader'; import { Column } from './Column'; import { Row } from './Row'; interface ColumnType { id: string; name: ReactNode; isRowHeader?: boolean; // TODO support width constraints for resizable columns } interface RowType { id: string; textValue?: string; [key: string]: ReactNode; // TODO can we make this more specific? } interface TableProps extends RACTableProps { columns?: C[]; rows?: R[]; resizableColumns?: boolean; dragColumnHeader?: ComponentProps['dragColumnHeader']; renderEmptyState?: ComponentProps['renderEmptyState']; // TODO maybe a custom "selectall" component? Is it doable with react-aria-components? } /** * A wrapper around the `react-aria-components` Table component. * * See https://react-spectrum.adobe.com/react-aria/Table.html */ export function Table({ columns, rows, resizableColumns, dragColumnHeader, renderEmptyState, ...otherProps }: TableProps) { let table = null; if (Array.isArray(columns) && Array.isArray(rows)) { table = ( {(column) => ( {resizableColumns && (
{column.name}
)} {!resizableColumns && column.name}
)}
{(item) => ( {(column) => {item[column.id]}} )}
); } else { table = ; if (Array.isArray(columns)) { console.warn('The Table component was given columns but no rows'); } else if (Array.isArray(rows)) { console.warn('The Table component was given rows but no columns'); } } if (resizableColumns) { return {table}; } else { return table; } }