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

feat: added support for Alt+Down Arrow and Alt+Up Arrow in ComboBox #14892

Merged
merged 9 commits into from
Nov 27, 2023
16 changes: 16 additions & 0 deletions packages/react/src/components/ComboBox/ComboBox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -234,5 +235,20 @@ describe('ComboBox', () => {
expect(firstListBox()).toBeEmptyDOMElement();
expect(secondListBox()).toBeEmptyDOMElement();
});
it('should open menu without moving focus on pressing Alt+ DownArrow', async () => {
render(<ComboBox {...mockProps} />);
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(<ComboBox {...mockProps} />);
await openMenu();
await userEvent.keyboard('{Alt>}{ArrowUp}');
assertMenuClosed(mockProps);
});
});
});
13 changes: 13 additions & 0 deletions packages/react/src/components/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,19 @@ const ComboBox = forwardRef(
event.target.value.length
);
}

if (event.altKey && event.key == 'ArrowDown') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(match(event, keys.Alt) && match(event,keys.ArrowDown))
{
 ////
}

this doesn't seem to be working ??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no definition for keys.Alt in packages/react/src/internal/keyboard/keys.js. Could you add it and retry using match() again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(match(event, keys.Alt) && match(event,keys.ArrowDown))
{
 ////
}

this doesn't seem to be working ??

I had tried this after adding definition for Alt key
However, if I put any one condition it works as expected but both simultaneously are not working :(

event.preventDownshiftDefault = true;
if (!isOpen) {
toggleMenu();
}
}
if (event.altKey && event.key == 'ArrowUp') {
event.preventDownshiftDefault = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this prevent exactly?

Copy link
Contributor Author

@k-rajat19 k-rajat19 Oct 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It prevents the default behavior of ArrowUp button, usually default behaviour of ArrowUp is not shown when both button are pressed carefully but some time I have detected this behavior (bottom-most index is highlighted)

if (isOpen) {
toggleMenu();
}
}
},
});

Expand Down
Loading