From 128836e6fbcfa3fb688fee110d2ccaa92ab923ee Mon Sep 17 00:00:00 2001 From: Rajat Date: Tue, 28 Nov 2023 02:19:54 +0530 Subject: [PATCH] feat: added support for `Alt+Down Arrow` and `Alt+Up Arrow` in ComboBox (#14892) * feat: added support * feat: added test * fix: tests --- .../src/components/ComboBox/ComboBox-test.js | 16 ++++++++++++++++ .../react/src/components/ComboBox/ComboBox.tsx | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/packages/react/src/components/ComboBox/ComboBox-test.js b/packages/react/src/components/ComboBox/ComboBox-test.js index dca836a0b8c8..2a474364847e 100644 --- a/packages/react/src/components/ComboBox/ComboBox-test.js +++ b/packages/react/src/components/ComboBox/ComboBox-test.js @@ -16,6 +16,7 @@ import { generateGenericItem, } from '../ListBox/test-helpers'; import ComboBox from '../ComboBox'; +import { act } from 'react-dom/test-utils'; const findInputNode = () => screen.getByRole('combobox'); const openMenu = async () => { @@ -249,5 +250,20 @@ describe('ComboBox', () => { expect(firstListBox()).toBeEmptyDOMElement(); expect(secondListBox()).toBeEmptyDOMElement(); }); + it('should open menu without moving focus on pressing Alt+ DownArrow', async () => { + render(); + act(() => { + screen.getByRole('combobox').focus(); + }); + await userEvent.keyboard('{Alt>}{ArrowDown}'); + assertMenuOpen(mockProps); + }); + + it('should close menu and return focus to combobox on pressing Alt+ UpArrow', async () => { + render(); + await openMenu(); + await userEvent.keyboard('{Alt>}{ArrowUp}'); + assertMenuClosed(mockProps); + }); }); }); diff --git a/packages/react/src/components/ComboBox/ComboBox.tsx b/packages/react/src/components/ComboBox/ComboBox.tsx index 6771ef8be1bf..c286b1192080 100644 --- a/packages/react/src/components/ComboBox/ComboBox.tsx +++ b/packages/react/src/components/ComboBox/ComboBox.tsx @@ -635,6 +635,19 @@ const ComboBox = forwardRef( event.target.value.length ); } + + if (event.altKey && event.key == 'ArrowDown') { + event.preventDownshiftDefault = true; + if (!isOpen) { + toggleMenu(); + } + } + if (event.altKey && event.key == 'ArrowUp') { + event.preventDownshiftDefault = true; + if (isOpen) { + toggleMenu(); + } + } }, });