Skip to content

Commit 7cce87a

Browse files
committed
[TreeView] fix inconsistent focus for programmatically focused tree item
1 parent 7e1da61 commit 7cce87a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/material-ui-lab/src/TreeItem/TreeItem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ const TreeItem = React.forwardRef(function TreeItem(props, ref) {
328328
};
329329

330330
const handleFocus = (event) => {
331-
if (!focused && tabbable) {
331+
if (!focused && event.currentTarget === event.target) {
332332
focus(nodeId);
333333
}
334334

packages/material-ui-lab/src/TreeItem/TreeItem.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,26 @@ describe('<TreeItem />', () => {
300300

301301
expect(getByTestId('two')).to.have.focus;
302302
});
303+
304+
it('should work with programmatic focus', () => {
305+
const { getByTestId } = render(
306+
<React.Fragment>
307+
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */}
308+
<div data-testid="start" tabIndex={0} />
309+
<TreeView>
310+
<TreeItem nodeId="1" label="one" data-testid="one" />
311+
<TreeItem nodeId="2" label="two" data-testid="two" />
312+
</TreeView>
313+
</React.Fragment>,
314+
);
315+
316+
expect(getByTestId('one')).to.have.attribute('tabindex', '0');
317+
expect(getByTestId('two')).to.have.attribute('tabindex', '-1');
318+
319+
getByTestId('two').focus();
320+
expect(getByTestId('one')).to.have.attribute('tabindex', '-1');
321+
expect(getByTestId('two')).to.have.attribute('tabindex', '0');
322+
});
303323
});
304324

305325
describe('Navigation', () => {

0 commit comments

Comments
 (0)