From 807ebe0b7684689866c85267fd65147750fc00e3 Mon Sep 17 00:00:00 2001 From: Matheus Wichman Date: Fri, 27 Mar 2020 23:32:14 -0300 Subject: [PATCH] [TreeView] Don't move focus when pressing a modifier key + letter --- .../material-ui-lab/src/TreeItem/TreeItem.js | 13 +++-------- .../src/TreeItem/TreeItem.test.js | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/material-ui-lab/src/TreeItem/TreeItem.js b/packages/material-ui-lab/src/TreeItem/TreeItem.js index 5371c5e8addabf..3146ee0c6bc955 100644 --- a/packages/material-ui-lab/src/TreeItem/TreeItem.js +++ b/packages/material-ui-lab/src/TreeItem/TreeItem.js @@ -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) { @@ -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; } } diff --git a/packages/material-ui-lab/src/TreeItem/TreeItem.test.js b/packages/material-ui-lab/src/TreeItem/TreeItem.test.js index abbcc4604fe6e7..f41dd6547f3366 100644 --- a/packages/material-ui-lab/src/TreeItem/TreeItem.test.js +++ b/packages/material-ui-lab/src/TreeItem/TreeItem.test.js @@ -651,6 +651,28 @@ describe('', () => { 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( + + + + + + , + ); + + getByTestId('apple').focus(); + expect(getByTestId('apple')).to.have.focus; + fireEvent.keyDown(document.activeElement, { key: 'c', 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', () => {