Skip to content

Commit e0cfafb

Browse files
committed
Expand TreeDataGrid docs
1 parent 6231adc commit e0cfafb

File tree

1 file changed

+108
-5
lines changed

1 file changed

+108
-5
lines changed

README.md

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,20 +596,123 @@ 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+
**Current Limitations:**
622+
623+
- Group columns cannot be rendered under one column
624+
- Group columns are automatically frozen and cannot be unfrozen
600625

601626
##### TreeDataGridProps
602627

603-
###### `groupBy?: Maybe<readonly string[]>`
628+
All [`DataGridProps`](#datagridprops) are supported except those listed above, plus the following additional props:
629+
630+
###### `groupBy: readonly string[]`
631+
632+
**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.).
633+
634+
```tsx
635+
import { TreeDataGrid, type Column } from 'react-data-grid';
636+
637+
interface Row {
638+
id: number;
639+
country: string;
640+
city: string;
641+
name: string;
642+
}
643+
644+
const columns: readonly Column<Row>[] = [
645+
{ key: 'country', name: 'Country' },
646+
{ key: 'city', name: 'City' },
647+
{ key: 'name', name: 'Name' }
648+
];
604649

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

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

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

611697
###### `groupIdGetter?: Maybe<(groupKey: string, parentId?: string) => string>`
612698

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

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

0 commit comments

Comments
 (0)