Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Fix TreeGrid level navigation when virtualized rows are wrapped in additional containers",
"packageName": "@fluentui-contrib/react-tree-grid",
"email": "bsunderhus@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,28 @@ const ThreadedVirtualizedRow = ({

if (item.type === 'header') {
return (
<TreeGridRow data-item-id={item.value} open style={style} subtree>
<TreeGridCell header>{item.value}</TreeGridCell>
<TreeGridCell>Header</TreeGridCell>
</TreeGridRow>
<div>
<TreeGridRow data-item-id={item.value} open style={style} subtree>
<TreeGridCell header>{item.value}</TreeGridCell>
<TreeGridCell>Header</TreeGridCell>
</TreeGridRow>
</div>
);
}

return (
<TreeGridRow
data-item-id={item.value}
data-item-parent-id={item.parentValue}
data-rowtype={item.type}
level={2}
style={style}
>
<TreeGridCell header>{item.value}</TreeGridCell>
<TreeGridCell>{item.type}</TreeGridCell>
</TreeGridRow>
<div>
<TreeGridRow
data-item-id={item.value}
data-item-parent-id={item.parentValue}
data-rowtype={item.type}
level={2}
style={style}
>
<TreeGridCell header>{item.value}</TreeGridCell>
<TreeGridCell>{item.type}</TreeGridCell>
</TreeGridRow>
</div>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,34 @@ const VirtualizedMeetingsRow = ({

if (item.type === 'section') {
return (
<div>
<TreeGridRow
data-item-id={item.value}
open={isOpen}
style={style}
subtree
>
<TreeGridCell header>{item.value}</TreeGridCell>
<TreeGridCell>Section</TreeGridCell>
</TreeGridRow>
</div>
);
}

return (
<div>
<TreeGridRow
data-item-id={item.value}
open={isOpen}
data-item-parent-id={item.parentValue}
level={2}
style={style}
subtree
>
<TreeGridCell header>{item.value}</TreeGridCell>
<TreeGridCell>Section</TreeGridCell>
<TreeGridCell header>
<Button>{item.value}</Button>
</TreeGridCell>
<TreeGridCell>Meeting</TreeGridCell>
</TreeGridRow>
);
}

return (
<TreeGridRow
data-item-id={item.value}
data-item-parent-id={item.parentValue}
level={2}
style={style}
>
<TreeGridCell header>
<Button>{item.value}</Button>
</TreeGridCell>
<TreeGridCell>Meeting</TreeGridCell>
</TreeGridRow>
</div>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ const createRow = (level: number): HTMLDivElement => {
return row;
};

const createWrappedRow = (
level: number,
treeGrid: HTMLElement
): HTMLDivElement => {
const wrapper = document.createElement('div');
const row = document.createElement('div');

row.setAttribute('role', 'row');
row.setAttribute('aria-level', String(level));
row.tabIndex = 0;
row.scrollIntoView = jest.fn();

wrapper.appendChild(row);
treeGrid.appendChild(wrapper);

return row;
};

const createTreeGrid = (): HTMLDivElement => {
const treeGrid = document.createElement('div');
treeGrid.setAttribute('role', 'treegrid');
document.body.appendChild(treeGrid);
return treeGrid;
};

const createKeyboardEvent = (
key: string,
target: HTMLElement
Expand Down Expand Up @@ -62,9 +87,10 @@ describe('useTreeGridLevelNavigation', () => {

it('focuses the next row at the same level', () => {
const { result } = renderHook(() => useTreeGridLevelNavigation());
const currentRow = createRow(2);
createRow(3);
const nextRow = createRow(2);
const treeGrid = createTreeGrid();
const currentRow = createWrappedRow(2, treeGrid);
createWrappedRow(3, treeGrid);
const nextRow = createWrappedRow(2, treeGrid);
const nativeEvent = createKeyboardEvent(ArrowDown, currentRow);
const keyboardEvent = createReactKeyboardEvent(nativeEvent);

Expand All @@ -77,8 +103,9 @@ describe('useTreeGridLevelNavigation', () => {

it('allows shallower-boundary navigation to fall through', () => {
const { result } = renderHook(() => useTreeGridLevelNavigation());
const parentRow = createRow(1);
const currentRow = createRow(2);
const treeGrid = createTreeGrid();
const parentRow = createWrappedRow(1, treeGrid);
const currentRow = createWrappedRow(2, treeGrid);
const nativeEvent = createKeyboardEvent(ArrowUp, currentRow);
const keyboardEvent = createReactKeyboardEvent(nativeEvent);
const moveFocusEvent = createMoveFocusEvent(nativeEvent, parentRow);
Expand All @@ -89,4 +116,19 @@ describe('useTreeGridLevelNavigation', () => {
expect(moveFocusEvent.defaultPrevented).toBe(false);
expect(keyboardEvent.preventDefault).not.toHaveBeenCalled();
});

it('skips wrapper elements when finding adjacent rows', () => {
const { result } = renderHook(() => useTreeGridLevelNavigation());
const treeGrid = createTreeGrid();
const currentRow = createWrappedRow(1, treeGrid);
const nextRow = createWrappedRow(1, treeGrid);
const nativeEvent = createKeyboardEvent(ArrowDown, currentRow);
const keyboardEvent = createReactKeyboardEvent(nativeEvent);

result.current.onKeyDown(keyboardEvent);

expect(keyboardEvent.preventDefault).toHaveBeenCalledTimes(1);
expect(nextRow.scrollIntoView).toHaveBeenCalledWith({ block: 'nearest' });
expect(nextRow).toBe(document.activeElement);
});
});
67 changes: 44 additions & 23 deletions packages/react-tree-grid/src/hooks/useTreeGridLevelNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,53 @@ const findAdjacentRowAtLevel = (
return { type: 'none' };
}

let sibling: Element | null =
direction === ArrowDown
? row.nextElementSibling
: row.previousElementSibling;

while (sibling) {
if (isHTMLElement(sibling) && sibling.getAttribute('role') === 'row') {
const siblingLevel = Number(sibling.getAttribute('aria-level'));

if (Number.isNaN(siblingLevel)) {
return { type: 'none' };
}

if (siblingLevel < level) {
return { type: 'shallower-boundary' };
}

if (siblingLevel === level) {
return { type: 'same-level', row: sibling };
}
const treeGrid = row.closest<HTMLElement>('[role="treegrid"]');
if (!treeGrid) {
return { type: 'none' };
}

const nodeFilter = row.ownerDocument.defaultView?.NodeFilter;
if (!nodeFilter) {
return { type: 'none' };
}

const rowWalker = row.ownerDocument.createTreeWalker(
treeGrid,
nodeFilter.SHOW_ELEMENT,
{
acceptNode: (node) =>
isHTMLElement(node) && node.getAttribute('role') === 'row'
? nodeFilter.FILTER_ACCEPT
: nodeFilter.FILTER_SKIP,
}
);

rowWalker.currentNode = row;

for (
let sibling =
direction === ArrowDown ? rowWalker.nextNode() : rowWalker.previousNode();
sibling;
sibling =
direction === ArrowDown
? sibling.nextElementSibling
: sibling.previousElementSibling;
direction === ArrowDown ? rowWalker.nextNode() : rowWalker.previousNode()
) {
if (!isHTMLElement(sibling)) {
continue;
}

const siblingLevel = Number(sibling.getAttribute('aria-level'));

if (Number.isNaN(siblingLevel)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth confirming: the walker now scans the whole treegrid rather than just direct siblings. If any role="row" without an aria-level can appear between data rows (e.g. a column-header row), it would hit this NaN branch and return none, disabling level navigation in that direction. Fine for the current fixtures since every row has a level — just flagging the assumption.

return { type: 'none' };
}

if (siblingLevel < level) {
return { type: 'shallower-boundary' };
}

if (siblingLevel === level) {
return { type: 'same-level', row: sibling };
}
}

return { type: 'none' };
Expand Down
Loading