Skip to content

Commit 5a08270

Browse files
committed
Address comments
1 parent 884073c commit 5a08270

2 files changed

Lines changed: 30 additions & 40 deletions

File tree

src/TreeDataGrid.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface TreeDataGridProps<R, SR = unknown, K extends Key = Key>
3939
) => Record<string, readonly NoInfer<R>[]>;
4040
expandedGroupIds: ReadonlySet<unknown>;
4141
onExpandedGroupIdsChange: (expandedGroupIds: Set<unknown>) => void;
42-
generateGroupId?: (groupKey: string, parentId?: string) => string;
42+
groupIdGetter?: Maybe<(groupKey: string, parentId?: string) => string>;
4343
}
4444

4545
type GroupByDictionary<TRow> = Record<
@@ -67,14 +67,15 @@ export function TreeDataGrid<R, SR = unknown, K extends Key = Key>({
6767
rowGrouper,
6868
expandedGroupIds,
6969
onExpandedGroupIdsChange,
70-
generateGroupId,
70+
groupIdGetter: rawGroupIdGetter,
7171
...props
7272
}: TreeDataGridProps<R, SR, K>) {
7373
const defaultRenderers = useDefaultRenderers<R, SR>();
7474
const rawRenderRow = renderers?.renderRow ?? defaultRenderers?.renderRow ?? defaultRenderRow;
7575
const headerAndTopSummaryRowsCount = 1 + (props.topSummaryRows?.length ?? 0);
7676
const { leftKey, rightKey } = getLeftRightKey(props.direction);
7777
const toggleGroupLatest = useLatestFunc(toggleGroup);
78+
const groupIdGetter = rawGroupIdGetter ?? defaultGroupIdGetter;
7879

7980
const { columns, groupBy } = useMemo(() => {
8081
const columns = [...rawColumns].sort(({ key: aKey }, { key: bKey }) => {
@@ -147,14 +148,6 @@ export function TreeDataGrid<R, SR = unknown, K extends Key = Key>({
147148

148149
const flattenedRows: Array<R | GroupRow<R>> = [];
149150

150-
const groupIdGenerator = (groupKey: string, parentId?: string) => {
151-
if (generateGroupId !== undefined && typeof generateGroupId === 'function') {
152-
return generateGroupId(groupKey, parentId);
153-
}
154-
155-
return `${parentId}__${groupKey}`;
156-
};
157-
158151
const expandGroup = (
159152
rows: GroupByDictionary<R> | readonly R[],
160153
parentId: string | undefined,
@@ -165,7 +158,7 @@ export function TreeDataGrid<R, SR = unknown, K extends Key = Key>({
165158
return;
166159
}
167160
Object.keys(rows).forEach((groupKey, posInSet, keys) => {
168-
const id = groupIdGenerator(groupKey, parentId);
161+
const id = groupIdGetter(groupKey, parentId);
169162
const isExpanded = expandedGroupIds.has(id);
170163
const { childRows, childGroups, startRowIndex } = rows[groupKey];
171164

@@ -195,7 +188,7 @@ export function TreeDataGrid<R, SR = unknown, K extends Key = Key>({
195188
function isGroupRow(row: R | GroupRow<R>): row is GroupRow<R> {
196189
return allGroupRows.has(row);
197190
}
198-
}, [expandedGroupIds, groupedRows, rawRows, generateGroupId]);
191+
}, [expandedGroupIds, groupedRows, rawRows, groupIdGetter]);
199192

200193
const rowHeight = useMemo(() => {
201194
if (typeof rawRowHeight === 'function') {
@@ -455,6 +448,10 @@ export function TreeDataGrid<R, SR = unknown, K extends Key = Key>({
455448
);
456449
}
457450

451+
function defaultGroupIdGetter(groupKey: string, parentId: string | undefined) {
452+
return parentId !== undefined ? `${parentId}__${groupKey}` : groupKey;
453+
}
454+
458455
function isReadonlyArray(arr: unknown): arr is readonly unknown[] {
459456
return Array.isArray(arr);
460457
}

test/browser/TreeDataGrid.test.tsx

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ const onCellPasteSpy = vi.fn(({ row }: { row: Row }) => row);
8484
function rowKeyGetter(row: Row) {
8585
return row.id;
8686
}
87-
88-
interface TestGridProps {
87+
function TestGrid({
88+
groupBy,
89+
groupIdGetter
90+
}: {
8991
groupBy: string[];
90-
generateGroupId: ((groupKey: string, parentId?: string) => string) | undefined;
91-
}
92-
93-
function TestGrid({ groupBy, generateGroupId }: TestGridProps) {
92+
groupIdGetter: ((groupKey: string, parentId?: string) => string) | undefined;
93+
}) {
9494
const [rows, setRows] = useState(initialRows);
9595
const [selectedRows, setSelectedRows] = useState((): ReadonlySet<number> => new Set());
9696
const [expandedGroupIds, setExpandedGroupIds] = useState(
@@ -113,7 +113,7 @@ function TestGrid({ groupBy, generateGroupId }: TestGridProps) {
113113
onRowsChange={setRows}
114114
onCellCopy={onCellCopySpy}
115115
onCellPaste={onCellPasteSpy}
116-
{...(generateGroupId && { generateGroupId })}
116+
groupIdGetter={groupIdGetter}
117117
/>
118118
);
119119
}
@@ -124,11 +124,8 @@ function rowGrouper(rows: readonly Row[], columnKey: string) {
124124
return Object.groupBy(rows, (r) => r[columnKey]) as Record<string, readonly R[]>;
125125
}
126126

127-
function setup(
128-
groupBy: string[],
129-
generateGroupId?: (groupKey: string, parentId?: string) => string
130-
) {
131-
page.render(<TestGrid groupBy={groupBy} generateGroupId={generateGroupId} />);
127+
function setup(groupBy: string[], groupIdGetter?: (groupKey: string, parentId?: string) => string) {
128+
page.render(<TestGrid groupBy={groupBy} groupIdGetter={groupIdGetter} />);
132129
}
133130

134131
function getHeaderCellsContent() {
@@ -162,13 +159,23 @@ test('should group by multiple columns', async () => {
162159
expect(getRows()).toHaveLength(4);
163160
});
164161

165-
test('should group by multiple columns when passing generateGroupId', () => {
166-
setup(['country', 'year'], (groupKey, parentId) =>
162+
test('should use groupIdGetter when provided', async () => {
163+
const groupIdGetter = vi.fn((groupKey: string, parentId?: string) =>
167164
parentId !== undefined ? `${groupKey}#${parentId}` : groupKey
168165
);
166+
setup(['country', 'year'], groupIdGetter);
167+
expect(groupIdGetter).toHaveBeenCalled();
169168
expect(getTreeGrid()).toHaveAttribute('aria-rowcount', '13');
170169
expect(getHeaderCellsContent()).toStrictEqual(['', 'Country', 'Year', 'Sport', 'Id']);
171170
expect(getRows()).toHaveLength(4);
171+
groupIdGetter.mockClear();
172+
await userEvent.click(page.getByRole('gridcell', { name: 'USA' }));
173+
expect(getRows()).toHaveLength(6);
174+
expect(groupIdGetter).toHaveBeenCalled();
175+
await userEvent.click(page.getByRole('gridcell', { name: 'Canada' }));
176+
expect(getRows()).toHaveLength(8);
177+
await userEvent.click(page.getByRole('gridcell', { name: '2020' }));
178+
expect(getRows()).toHaveLength(9);
172179
});
173180

174181
test('should ignore duplicate groupBy columns', async () => {
@@ -194,20 +201,6 @@ test('should toggle group when group cell is clicked', async () => {
194201
expect(getRows()).toHaveLength(5);
195202
});
196203

197-
test('should toggle group when group cell is clicked and is passing `generateGroupId` props', async () => {
198-
setup(['country', 'year'], (groupKey, parentId) =>
199-
parentId !== undefined ? `${groupKey}#${parentId}` : groupKey
200-
);
201-
expect(getTreeGrid()).toHaveAttribute('aria-rowcount', '13');
202-
expect(getHeaderCellsContent()).toStrictEqual(['', 'Country', 'Year', 'Sport', 'Id']);
203-
await userEvent.click(page.getByRole('gridcell', { name: 'USA' }));
204-
expect(getRows()).toHaveLength(6);
205-
await userEvent.click(page.getByRole('gridcell', { name: 'Canada' }));
206-
expect(getRows()).toHaveLength(8);
207-
await userEvent.click(page.getByRole('gridcell', { name: '2020' }));
208-
expect(getRows()).toHaveLength(9);
209-
});
210-
211204
test('should toggle group using keyboard', async () => {
212205
setup(['year']);
213206
expect(getRows()).toHaveLength(5);

0 commit comments

Comments
 (0)