Skip to content

Commit f700397

Browse files
feat(Table): window tables that use selection and row index
Selection and row-index columns register as fixed-height, so tables that use them stay windowed. The row index renders windowed numbers from the Table logical position. Selection, sticky columns and tree data skip row-context tracking, which they never read. Tree, grouped-row and row-expansion plugins name their structural row models, so their full-render fallback reports why. Tests pin that sticky-column and context-menu tables stay windowed and keep a menu's row. This diff was generated with AI assistance (Navi). Tags: ai-generated
1 parent 075e4f5 commit f700397

10 files changed

Lines changed: 409 additions & 25 deletions

File tree

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
import {useMemo} from 'react';
3+
import {
4+
render,
5+
screen,
6+
fireEvent,
7+
waitFor,
8+
cleanup,
9+
} from '@testing-library/react';
10+
import {afterEach, describe, it, expect, vi} from 'vitest';
11+
import {warmMenuBottomSheet} from '../DropdownMenu/__tests__/warmMenuBottomSheet';
12+
import {beforeAll} from 'vitest';
13+
beforeAll(warmMenuBottomSheet, 20000);
14+
import {Table} from './Table';
15+
import type {BodyCellRenderProps, BodyRowRenderProps} from './types';
16+
import {useTableRowsVirtualization} from './plugins/virtualization/useTableRowsVirtualization';
17+
const data = Array.from({length: 100}, (_, i) => ({
18+
id: String(i),
19+
name: `Row ${i}`,
20+
}));
21+
const columns = [{key: 'name', header: 'Name'}];
22+
afterEach(() => {
23+
cleanup();
24+
vi.restoreAllMocks();
25+
});
26+
function Fixture({items = data}: {items?: typeof data}) {
27+
const virtualization = useTableRowsVirtualization({
28+
rowHeight: 56,
29+
viewportHeight: 240,
30+
});
31+
const menu = useMemo(
32+
() => ({
33+
rowModel: 'flat' as const,
34+
transformBodyRow(props: BodyRowRenderProps) {
35+
return {...props, htmlProps: {...props.htmlProps, tabIndex: 0}};
36+
},
37+
transformBodyCell(props: BodyCellRenderProps) {
38+
return {
39+
...props,
40+
contextMenuActions: [
41+
{id: 'inspect', label: 'Inspect row', onSelect: () => {}},
42+
],
43+
};
44+
},
45+
}),
46+
[],
47+
);
48+
return (
49+
<Table
50+
data={items}
51+
columns={columns}
52+
idKey="id"
53+
virtualization={virtualization}
54+
plugins={{menu}}
55+
/>
56+
);
57+
}
58+
describe('Table recognized menu focus under windowing', () => {
59+
it('returns removed owned menu focus even when its row was not focused before opening', async () => {
60+
const {container, rerender} = render(<Fixture />);
61+
const viewport = container.querySelector(
62+
'.astryx-table-scroll-wrapper',
63+
) as HTMLDivElement;
64+
const body = container.querySelector('tbody') as HTMLTableSectionElement;
65+
let count = data.length;
66+
Object.defineProperties(viewport, {
67+
clientHeight: {configurable: true, value: 240},
68+
clientWidth: {configurable: true, value: 400},
69+
scrollHeight: {configurable: true, get: () => count * 56 + 36},
70+
scrollWidth: {configurable: true, value: 400},
71+
});
72+
vi.spyOn(viewport, 'getBoundingClientRect').mockReturnValue(
73+
new DOMRect(0, 0, 400, 240),
74+
);
75+
vi.spyOn(body, 'getBoundingClientRect').mockImplementation(
76+
() => new DOMRect(0, 36 - viewport.scrollTop, 400, count * 56),
77+
);
78+
fireEvent.scroll(viewport);
79+
const row = container.querySelector(
80+
'tbody tr[data-table-logical-index="0"]',
81+
) as HTMLTableRowElement;
82+
expect(document.activeElement).not.toBe(row);
83+
fireEvent.contextMenu(row.cells[0].firstElementChild as HTMLElement, {
84+
clientX: 50,
85+
clientY: 50,
86+
});
87+
const item = (
88+
await screen.findAllByRole('menuitem', {
89+
name: 'Inspect row',
90+
hidden: true,
91+
})
92+
)[0];
93+
item.focus();
94+
expect(document.activeElement).toBe(item);
95+
viewport.scrollTop = 4000;
96+
fireEvent.scroll(viewport);
97+
await waitFor(() =>
98+
expect(
99+
container.querySelector('tbody tr[data-table-logical-index="70"]'),
100+
).not.toBeNull(),
101+
);
102+
expect(row.isConnected).toBe(true);
103+
count -= 1;
104+
rerender(<Fixture items={data.slice(1)} />);
105+
await waitFor(() => expect(viewport).toHaveFocus());
106+
expect(row.isConnected).toBe(false);
107+
});
108+
it.each([false, true])(
109+
'releases a null-target blur only when the document is focused (focused=%s)',
110+
async isDocumentFocused => {
111+
const {container} = render(<Fixture />);
112+
const viewport = container.querySelector(
113+
'.astryx-table-scroll-wrapper',
114+
) as HTMLDivElement;
115+
const body = container.querySelector('tbody')!;
116+
Object.defineProperties(viewport, {
117+
clientHeight: {configurable: true, value: 240},
118+
clientWidth: {configurable: true, value: 400},
119+
scrollHeight: {configurable: true, value: 5636},
120+
scrollWidth: {configurable: true, value: 400},
121+
});
122+
vi.spyOn(viewport, 'getBoundingClientRect').mockReturnValue({
123+
x: 0,
124+
y: 0,
125+
top: 0,
126+
left: 0,
127+
right: 400,
128+
bottom: 240,
129+
width: 400,
130+
height: 240,
131+
toJSON: () => ({}),
132+
});
133+
vi.spyOn(body, 'getBoundingClientRect').mockImplementation(() => ({
134+
x: 0,
135+
y: 36 - viewport.scrollTop,
136+
top: 36 - viewport.scrollTop,
137+
left: 0,
138+
right: 400,
139+
bottom: 5636 - viewport.scrollTop,
140+
width: 400,
141+
height: 5600,
142+
toJSON: () => ({}),
143+
}));
144+
fireEvent.scroll(viewport);
145+
const row = container.querySelector(
146+
'tbody tr[data-table-logical-index="0"]',
147+
) as HTMLTableRowElement;
148+
row.focus();
149+
viewport.scrollTop = 4000;
150+
fireEvent.scroll(viewport);
151+
await waitFor(() =>
152+
expect(
153+
container.querySelector('tbody tr[data-table-logical-index="70"]'),
154+
).not.toBeNull(),
155+
);
156+
expect(row.isConnected).toBe(true);
157+
vi.spyOn(document, 'hasFocus').mockReturnValue(isDocumentFocused);
158+
fireEvent.focusOut(row, {relatedTarget: null});
159+
await waitFor(() => expect(row.isConnected).toBe(!isDocumentFocused));
160+
if (!isDocumentFocused) {
161+
expect(document.activeElement).toBe(row);
162+
const outside = document.createElement('button');
163+
document.body.append(outside);
164+
outside.focus();
165+
await waitFor(() => expect(row.isConnected).toBe(false));
166+
expect(document.activeElement).toBe(outside);
167+
outside.remove();
168+
}
169+
},
170+
);
171+
it('retains the same logical row while its menu owns focus and returns to its trigger', async () => {
172+
const {container} = render(<Fixture />);
173+
const viewport = container.querySelector(
174+
'.astryx-table-scroll-wrapper',
175+
) as HTMLDivElement;
176+
const body = container.querySelector('tbody')!;
177+
Object.defineProperties(viewport, {
178+
clientHeight: {configurable: true, value: 240},
179+
clientWidth: {configurable: true, value: 400},
180+
scrollHeight: {configurable: true, value: 5636},
181+
scrollWidth: {configurable: true, value: 400},
182+
});
183+
vi.spyOn(viewport, 'getBoundingClientRect').mockReturnValue({
184+
x: 0,
185+
y: 0,
186+
top: 0,
187+
left: 0,
188+
right: 400,
189+
bottom: 240,
190+
width: 400,
191+
height: 240,
192+
toJSON: () => ({}),
193+
});
194+
vi.spyOn(body, 'getBoundingClientRect').mockImplementation(() => ({
195+
x: 0,
196+
y: 36 - viewport.scrollTop,
197+
top: 36 - viewport.scrollTop,
198+
left: 0,
199+
right: 400,
200+
bottom: 5636 - viewport.scrollTop,
201+
width: 400,
202+
height: 5600,
203+
toJSON: () => ({}),
204+
}));
205+
fireEvent.scroll(viewport);
206+
const row = container.querySelector(
207+
'tbody tr[data-table-logical-index="0"]',
208+
) as HTMLTableRowElement;
209+
row.focus();
210+
fireEvent.contextMenu(row.cells[0].firstElementChild!, {
211+
clientX: 50,
212+
clientY: 50,
213+
});
214+
const item = (
215+
await screen.findAllByRole('menuitem', {
216+
name: 'Inspect row',
217+
hidden: true,
218+
})
219+
)[0];
220+
item.focus();
221+
viewport.scrollTop = 4000;
222+
fireEvent.scroll(viewport);
223+
await waitFor(() =>
224+
expect(
225+
container.querySelector('tbody tr[data-table-logical-index="70"]'),
226+
).not.toBeNull(),
227+
);
228+
expect(row.isConnected).toBe(true);
229+
expect(document.activeElement).toBe(item);
230+
fireEvent.keyDown(item, {key: 'Escape'});
231+
await waitFor(() => expect(document.activeElement).toBe(row));
232+
expect(row.isConnected).toBe(true);
233+
});
234+
});

‎packages/core/src/Table/plugins/groupedRows/useTableGroupedRows.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ export function useTableGroupedRows<T extends Record<string, unknown>>(
353353

354354
const plugin = useMemo(
355355
(): TablePlugin<T> => ({
356+
rowModel: 'grouped',
356357
// A header row is not one of the caller's rows, so running a cell
357358
// renderer against it can only misread it or throw — and the cells are
358359
// discarded by `transformBodyRow` below regardless.

‎packages/core/src/Table/plugins/rowExpansion/useTableRowExpansion.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ export function useTableRowExpansion<T extends Record<string, unknown>>(
911911

912912
return useMemo(
913913
(): TablePlugin<T> => ({
914+
rowModel: 'detail',
914915
// One colour-mode watch per table for the open panels' dividers.
915916
transformTableContext(children: ReactNode): ReactNode {
916917
return (

‎packages/core/src/Table/plugins/rowIndex/useTableRowIndex.test.tsx‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import {describe, it, expect} from 'vitest';
44
import {act, render, screen, within} from '@testing-library/react';
55
import {useState} from 'react';
6+
import {vi} from 'vitest';
7+
import {useTableRowsVirtualization} from '../virtualization/useTableRowsVirtualization';
68
import {Table} from '../../Table';
79
import type {TableColumn, TablePlugin} from '../../types';
810
import {useTableRowIndex} from './useTableRowIndex';
@@ -83,6 +85,120 @@ describe('useTableRowIndex', () => {
8385
expect(within(rows[2]).getByText('2')).toBeInTheDocument();
8486
});
8587

88+
it('falls back instead of clipping a measured windowed number without changing legacy column widths', () => {
89+
const rows = Array.from({length: 20}, (_, index) => ({
90+
id: String(index),
91+
name: `Product ${index}`,
92+
}));
93+
vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockReturnValue(320);
94+
vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(48);
95+
const createRange = document.createRange.bind(document);
96+
vi.spyOn(document, 'createRange').mockImplementation(() => {
97+
const range = createRange();
98+
Object.defineProperty(range, 'getBoundingClientRect', {
99+
value: () => ({
100+
width: 53,
101+
height: 16,
102+
top: 0,
103+
bottom: 16,
104+
left: 0,
105+
right: 53,
106+
x: 0,
107+
y: 0,
108+
toJSON() {
109+
return {};
110+
},
111+
}),
112+
});
113+
return range;
114+
});
115+
vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(
116+
function (this: HTMLElement) {
117+
return {
118+
height: this.localName === 'tbody' ? rows.length * 56 : 0,
119+
top: 0,
120+
bottom: 0,
121+
left: 0,
122+
right: 0,
123+
width: 0,
124+
x: 0,
125+
y: 0,
126+
toJSON() {
127+
return {};
128+
},
129+
};
130+
},
131+
);
132+
const warning = vi.spyOn(console, 'warn').mockImplementation(() => {});
133+
function Fixture() {
134+
const rowIndex = useTableRowIndex({data: rows});
135+
const virtualization = useTableRowsVirtualization({
136+
rowHeight: 56,
137+
viewportHeight: 320,
138+
});
139+
return (
140+
<Table
141+
data={rows}
142+
columns={columns}
143+
idKey="id"
144+
plugins={{rowIndex}}
145+
virtualization={virtualization}
146+
/>
147+
);
148+
}
149+
try {
150+
const {container} = render(<Fixture />);
151+
expect(container.querySelectorAll('tbody > tr')).toHaveLength(
152+
rows.length,
153+
);
154+
expect(container.querySelector('[data-table-logical-index]')).toBeNull();
155+
expect(screen.getByRole('columnheader', {name: '#'}).style.width).toBe(
156+
'48px',
157+
);
158+
expect(warning).toHaveBeenCalledWith(
159+
expect.stringContaining('row-number content exceeds'),
160+
);
161+
} finally {
162+
vi.restoreAllMocks();
163+
}
164+
});
165+
it('numbers windowed rows from Table positions after same-array append without consulting a second key lookup', () => {
166+
const rows: Row[] = data.map(item => ({...item}));
167+
const getRowKey = vi.fn((item: Row) => item.id);
168+
function Windowed() {
169+
const rowIndex = useTableRowIndex({data: rows, getRowKey});
170+
const virtualization = useTableRowsVirtualization({
171+
rowHeight: 56,
172+
viewportHeight: 320,
173+
});
174+
return (
175+
<Table
176+
data={rows}
177+
columns={columns}
178+
idKey="id"
179+
virtualization={virtualization}
180+
plugins={{rowIndex}}
181+
/>
182+
);
183+
}
184+
const view = render(<Windowed />);
185+
rows.push({id: 'd', name: 'Dora'});
186+
view.rerender(<Windowed />);
187+
const appended = screen
188+
.getByText('Dora')
189+
.closest('tr') as HTMLTableRowElement;
190+
expect(appended).toHaveAttribute('aria-rowindex', '5');
191+
expect(appended.cells[0]).toHaveTextContent('4');
192+
expect(getRowKey).not.toHaveBeenCalled();
193+
rows.reverse();
194+
view.rerender(<Windowed />);
195+
const reordered = screen
196+
.getByText('Dora')
197+
.closest('tr') as HTMLTableRowElement;
198+
expect(reordered).toHaveAttribute('aria-rowindex', '2');
199+
expect(reordered.cells[0]).toHaveTextContent('1');
200+
});
201+
86202
it('prepends a header cell with the default label', () => {
87203
render(<Harness />);
88204
const headers = screen.getAllByRole('columnheader');

0 commit comments

Comments
 (0)