Skip to content

Commit 908eb52

Browse files
authored
Expand TreeDataGrid docs (#3910)
* Expand TreeDataGrid docs * Caveats * add caveat
1 parent 6231adc commit 908eb52

File tree

1 file changed

+109
-5
lines changed

1 file changed

+109
-5
lines changed

README.md

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,20 +596,124 @@ test('grid', async () => {
596596

597597
#### `<TreeDataGrid />`
598598

599-
`TreeDataGrid` is component built on top of `DataGrid` to add row grouping. This implements the [Treegrid pattern](https://www.w3.org/WAI/ARIA/apg/patterns/treegrid/). At the moment `TreeDataGrid` does not support `onFill` and `isRowSelectionDisabled` props
599+
`TreeDataGrid` is a component built on top of `DataGrid` to add hierarchical row grouping. This implements the [Treegrid pattern](https://www.w3.org/WAI/ARIA/apg/patterns/treegrid/).
600+
601+
**How it works:**
602+
603+
1. The `groupBy` prop specifies which columns should be used for grouping
604+
2. The `rowGrouper` function groups rows by the specified column keys
605+
3. Group rows are rendered with expand/collapse toggles
606+
4. Child rows are nested under their parent groups
607+
5. Groups can be expanded/collapsed by clicking the toggle or using keyboard navigation (<kbd>←</kbd>, <kbd>→</kbd>)
608+
609+
**Keyboard Navigation:**
610+
611+
- <kbd>→</kbd> (Right Arrow): Expand a collapsed group row when focused
612+
- <kbd>←</kbd> (Left Arrow): Collapse an expanded group row when focused, or navigate to parent group
613+
614+
**Unsupported Props:**
615+
616+
The following `DataGrid` props are not supported in `TreeDataGrid`:
617+
618+
- `onFill` - Drag-fill is disabled for tree grids
619+
- `isRowSelectionDisabled` - Row selection disabling is not available
620+
621+
**Caveats:**
622+
623+
- Group columns cannot be rendered under one column
624+
- Group columns are automatically frozen and cannot be unfrozen
625+
- Cell copy/paste does not work on group rows
600626

601627
##### TreeDataGridProps
602628

603-
###### `groupBy?: Maybe<readonly string[]>`
629+
All [`DataGridProps`](#datagridprops) are supported except those listed above, plus the following additional props:
630+
631+
###### `groupBy: readonly string[]`
632+
633+
**Required.** An array of column keys to group by. The order determines the grouping hierarchy (first key is the top level, second key is nested under the first, etc.).
634+
635+
```tsx
636+
import { TreeDataGrid, type Column } from 'react-data-grid';
637+
638+
interface Row {
639+
id: number;
640+
country: string;
641+
city: string;
642+
name: string;
643+
}
644+
645+
const columns: readonly Column<Row>[] = [
646+
{ key: 'country', name: 'Country' },
647+
{ key: 'city', name: 'City' },
648+
{ key: 'name', name: 'Name' }
649+
];
604650

605-
###### `rowGrouper?: Maybe<(rows: readonly R[], columnKey: string) => Record<string, readonly R[]>>`
651+
function MyGrid() {
652+
return (
653+
<TreeDataGrid
654+
columns={columns}
655+
rows={rows}
656+
groupBy={['country', 'city']}
657+
// ... other props
658+
/>
659+
);
660+
}
661+
```
606662

607-
###### `expandedGroupIds?: Maybe<ReadonlySet<unknown>>`
663+
###### `rowGrouper: (rows: readonly R[], columnKey: string) => Record<string, readonly R[]>`
608664

609-
###### `onExpandedGroupIdsChange?: Maybe<(expandedGroupIds: Set<unknown>) => void>`
665+
**Required.** A function that groups rows by the specified column key. Returns an object where keys are the group values and values are arrays of rows belonging to that group.
666+
667+
```tsx
668+
function rowGrouper(rows: Row[], columnKey: string) {
669+
return Object.groupBy(rows, (row) => row[columnKey]);
670+
}
671+
```
672+
673+
###### `expandedGroupIds: ReadonlySet<unknown>`
674+
675+
**Required.** A set of group IDs that are currently expanded. Group IDs are generated by `groupIdGetter`.
676+
677+
```tsx
678+
import { useState } from 'react';
679+
import { TreeDataGrid } from 'react-data-grid';
680+
681+
function MyGrid() {
682+
const [expandedGroupIds, setExpandedGroupIds] = useState((): ReadonlySet<unknown> => new Set());
683+
684+
return (
685+
<TreeDataGrid
686+
expandedGroupIds={expandedGroupIds}
687+
onExpandedGroupIdsChange={setExpandedGroupIds}
688+
// ... other props
689+
/>
690+
);
691+
}
692+
```
693+
694+
###### `onExpandedGroupIdsChange: (expandedGroupIds: Set<unknown>) => void`
695+
696+
**Required.** Callback triggered when groups are expanded or collapsed.
610697

611698
###### `groupIdGetter?: Maybe<(groupKey: string, parentId?: string) => string>`
612699

700+
Function to generate unique IDs for group rows. If not provided, a default implementation is used that concatenates parent and group keys with `__`.
701+
702+
###### `rowHeight?: Maybe<number | ((args: RowHeightArgs<R>) => number)>`
703+
704+
**Note:** Unlike `DataGrid`, the `rowHeight` function receives [`RowHeightArgs<R>`](#rowheightargstrow) which includes a `type` property to distinguish between regular rows and group rows:
705+
706+
```tsx
707+
function getRowHeight(args: RowHeightArgs<Row>): number {
708+
if (args.type === 'GROUP') {
709+
return 50; // Custom height for group rows
710+
}
711+
return 35; // Height for regular rows
712+
}
713+
714+
<TreeDataGrid rowHeight={getRowHeight} ... />
715+
```
716+
613717
#### `<Row />`
614718

615719
The default row component. Can be wrapped via the `renderers.renderRow` prop.

0 commit comments

Comments
 (0)