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

Control ArrowUp for Dropdown #18324

Merged
merged 5 commits into from
Jan 24, 2025
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
7 changes: 6 additions & 1 deletion packages/react/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,12 @@ const Dropdown = React.forwardRef(
if (['Enter'].includes(evt.key) && !selectedItem && !isOpen) {
setIsFocused(true);
}
if (toggleButtonProps.onKeyDown) {

// For Dropdowns the arrow up key is only allowed if the Dropdown is open
if (
toggleButtonProps.onKeyDown &&
(evt.key !== 'ArrowUp' || (isOpen && evt.key === 'ArrowUp'))
) {
toggleButtonProps.onKeyDown(evt);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ describe('Dropdown', () => {
});
});

it('should not open on arrowUp', async () => {
let mockProps1 = { ...mockProps };
const ref = React.createRef();
render(<Dropdown {...mockProps1} readOnly={false} ref={ref} />);

const button = screen.getByRole('combobox');

if (button) {
assertMenuClosed();
// ArrowUp should not open the menu
fireEvent.keyDown(button, { key: 'ArrowUp' });
assertMenuClosed();
// ArrowDown should open the menu
fireEvent.keyDown(button, { key: 'ArrowDown' });
assertMenuOpen(mockProps1);
// ArrowUp is allowed now that the menu is open
fireEvent.keyDown(button, { key: 'ArrowUp' });
assertMenuOpen(mockProps1);
}

mockProps.onChange.mockClear();
});

it('should respect readOnly prop', async () => {
let onChange = jest.fn();
let onKeyDown = jest.fn();
Expand Down
Loading