Best Practices for row groups in useTable #5497
-
Hello there :) I'm implementing a table component and am really impressed by the functionalities provided by Snacks/Country
...where the table rows are split up into another layer of grouping. Sorting would then happen inside each group, rather than per row. Is there a way to do this with Thank you in advance 🙂 If it helps, this is my current interface TableComponentProps<T> extends AriaTableProps<T>, TableProps<T> {
selectionBehaviour?: SelectionBehavior;
className?: string;
}
export const Table = <T extends Record<string, unknown>>({
selectionMode,
selectionBehaviour,
className,
...props
}: TableComponentProps<T>): React.JSX.Element => {
const state = useTableState({
...props,
showSelectionCheckboxes: selectionMode === "multiple" && selectionBehaviour !== "replace"
});
const ref = useRef<HTMLTableElement>(null);
const { collection } = state;
const { gridProps } = useTable({ ...props }, state, ref);
return (
<table {...gridProps} ref={ref} className={twMerge("border-collapse text-left", className)}>
<TableRowGroup type={"thead"}>
{collection.headerRows.map(headerRow => (
<TableHeaderRow key={headerRow.key} item={headerRow} state={state}>
{[...(collection.getChildren?.(headerRow.key) ?? [])].map(column => (
<TableColumnHeader key={column.key} column={column} state={state} />
))}
</TableHeaderRow>
))}
</TableRowGroup>
<TableRowGroup type={"tbody"}>
{[...collection.body.childNodes].map(row => (
<TableRow key={row.key} item={row} state={state}>
{[...(collection.getChildren?.(row.key) ?? [])].map((cell, index) => (
<TableCell isFirst={index === 0} key={cell.key} cell={cell} state={state} />
))}
</TableRow>
))}
</TableRowGroup>
</table>
);
}; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I had done some work earlier this year for supporting this use case in the React Spectrum TableView and ListView. You can see this code #4210 and #4173. At the moment, it isn't supported within Things to keep in mind:
We plan to pick up this work in the future but it may be a while due to other priorities |
Beta Was this translation helpful? Give feedback.
-
So how we can use colSpan and rowSpan? |
Beta Was this translation helpful? Give feedback.
I had done some work earlier this year for supporting this use case in the React Spectrum TableView and ListView. You can see this code #4210 and #4173. At the moment, it isn't supported within
useTable
anduseGrid
because the GridCollection/TableCollection need some section (aka rowgroups) specific updates to process the new TableSection wrapper. There are quite a number of changes in those PRs but you can focus on looking at the changes in the aria table hooks and the stately table changes to start with.Things to keep in mind:
aria-rowspan
andaria-colspan
behaving worse than if you were to use native html e…