Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TreeView] Don't move focus when pressing a modifier key + letter #20309

Merged
merged 2 commits into from
Mar 28, 2020
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
13 changes: 3 additions & 10 deletions packages/material-ui-lab/src/TreeItem/TreeItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,6 @@ const TreeItem = React.forwardRef(function TreeItem(props, ref) {
}
};

const printableCharacter = (event, key) => {
if (isPrintableCharacter(key)) {
focusByFirstCharacter(nodeId, key);
return true;
}
return false;
};

const handleNextArrow = (event) => {
if (expandable) {
if (expanded) {
Expand Down Expand Up @@ -306,8 +298,9 @@ const TreeItem = React.forwardRef(function TreeItem(props, ref) {
flag = true;
} else if (multiSelect && ctrlPressed && key.toLowerCase() === 'a') {
flag = selectAllNodes(event);
} else if (isPrintableCharacter(key)) {
flag = printableCharacter(event, key);
} else if (!ctrlPressed && !event.shiftKey && isPrintableCharacter(key)) {
focusByFirstCharacter(nodeId, key);
flag = true;
}
}

Expand Down
22 changes: 22 additions & 0 deletions packages/material-ui-lab/src/TreeItem/TreeItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,28 @@ describe('<TreeItem />', () => {
fireEvent.keyDown(document.activeElement, { key: 't' });
expect(getByTestId('two')).to.have.focus;
});

it('should not move focus when pressing a modifier key + letter', () => {
const { getByTestId } = render(
<TreeView>
<TreeItem nodeId="apple" label="apple" data-testid="apple" />
<TreeItem nodeId="lemon" label="lemon" data-testid="lemon" />
<TreeItem nodeId="coconut" label="coconut" data-testid="coconut" />
<TreeItem nodeId="vanilla" label="vanilla" data-testid="vanilla" />
</TreeView>,
);

getByTestId('apple').focus();
expect(getByTestId('apple')).to.have.focus;
fireEvent.keyDown(document.activeElement, { key: 'v', ctrlKey: true });
expect(getByTestId('apple')).to.have.focus;

fireEvent.keyDown(document.activeElement, { key: 'v', metaKey: true });
expect(getByTestId('apple')).to.have.focus;

fireEvent.keyDown(document.activeElement, { key: 'v', shiftKey: true });
expect(getByTestId('apple')).to.have.focus;
});
});

describe('asterisk key interaction', () => {
Expand Down