Skip to content

Commit 2492cbc

Browse files
authored
fix: prevent scroll on tree title descendant focus (#1063)
1 parent b35c7b2 commit 2492cbc

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎src/Tree.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,10 +1076,16 @@ class Tree<TreeDataType extends DataNode | BasicDataNode = DataNode> extends Rea
10761076
};
10771077

10781078
onFocus: React.FocusEventHandler<HTMLDivElement> = (...args) => {
1079+
const [event] = args;
10791080
const { onFocus, disabled } = this.props;
10801081
const { activeKey, selectedKeys, flattenNodes } = this.state;
10811082

1082-
if (!this.focusedByMouse && !disabled && activeKey === null) {
1083+
if (
1084+
event.target === event.currentTarget &&
1085+
!this.focusedByMouse &&
1086+
!disabled &&
1087+
activeKey === null
1088+
) {
10831089
const visibleSelectedKey = selectedKeys.find(key => {
10841090
return flattenNodes.some(nodeItem => nodeItem.key === key);
10851091
});

‎tests/Tree.spec.tsx‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,4 +1485,50 @@ describe('Tree Basic', () => {
14851485
expect(scrollToSpy).not.toHaveBeenCalled();
14861486
scrollToSpy.mockRestore();
14871487
});
1488+
1489+
it.each([
1490+
{ description: 'a selected node', treeProps: { selectedKeys: ['1'] } },
1491+
{ description: 'selection disabled', treeProps: { selectable: false } },
1492+
])(
1493+
'should not scroll when an interactive title descendant receives focus with $description',
1494+
({ treeProps }) => {
1495+
const data = Array.from({ length: 20 }, (_, index) => ({
1496+
key: String(index),
1497+
title: `Node ${index}`,
1498+
}));
1499+
const treeRef = React.createRef<any>();
1500+
const onFocus = jest.fn();
1501+
const { getByRole } = render(
1502+
<Tree
1503+
ref={treeRef}
1504+
treeData={data}
1505+
height={100}
1506+
itemHeight={20}
1507+
titleRender={node =>
1508+
node.key === '0' ? (
1509+
<select aria-label="Node action">
1510+
<option>Action</option>
1511+
</select>
1512+
) : (
1513+
node.title
1514+
)
1515+
}
1516+
onFocus={onFocus}
1517+
{...treeProps}
1518+
/>,
1519+
);
1520+
const select = getByRole('combobox');
1521+
const scrollToSpy = jest.spyOn(treeRef.current, 'scrollTo');
1522+
1523+
fireEvent.mouseDown(select);
1524+
fireEvent.mouseUp(select);
1525+
expect(treeRef.current.focusedByMouse).toBe(false);
1526+
1527+
fireEvent.focus(select);
1528+
1529+
expect(onFocus).toHaveBeenCalledTimes(1);
1530+
expect(scrollToSpy).not.toHaveBeenCalled();
1531+
scrollToSpy.mockRestore();
1532+
},
1533+
);
14881534
});

0 commit comments

Comments
 (0)