Skip to content

Commit

Permalink
[TreeView] fix inconsistent focus for programmatically focused treeit…
Browse files Browse the repository at this point in the history
…em (#20237)
  • Loading branch information
tonyhallett authored Mar 25, 2020
1 parent 4092ff4 commit b64e05c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/material-ui-lab/src/TreeItem/TreeItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ const TreeItem = React.forwardRef(function TreeItem(props, ref) {
};

const handleFocus = (event) => {
if (!focused && tabbable) {
if (!focused && event.currentTarget === event.target) {
focus(nodeId);
}

Expand Down
20 changes: 20 additions & 0 deletions packages/material-ui-lab/src/TreeItem/TreeItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,26 @@ describe('<TreeItem />', () => {

expect(getByTestId('two')).to.have.focus;
});

it('should work with programmatic focus', () => {
const { getByTestId } = render(
<React.Fragment>
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */}
<div data-testid="start" tabIndex={0} />
<TreeView>
<TreeItem nodeId="1" label="one" data-testid="one" />
<TreeItem nodeId="2" label="two" data-testid="two" />
</TreeView>
</React.Fragment>,
);

expect(getByTestId('one')).to.have.attribute('tabindex', '0');
expect(getByTestId('two')).to.have.attribute('tabindex', '-1');

getByTestId('two').focus();
expect(getByTestId('one')).to.have.attribute('tabindex', '-1');
expect(getByTestId('two')).to.have.attribute('tabindex', '0');
});
});

describe('Navigation', () => {
Expand Down

0 comments on commit b64e05c

Please sign in to comment.