Skip to content

Commit

Permalink
Control ArrowUp for Dropdown (#18324)
Browse files Browse the repository at this point in the history
* fix(Dropdown): 18277 - Disable arrow up on dropdown

* fix: refactor to make it easier to read and include last item

* fix: almost never allow ArrowUp for DropDown, always allow ArrowDown

* fix: add a simple test to verify arrow up and arrow down actions
  • Loading branch information
cknabe authored Jan 24, 2025
1 parent 5e097cd commit 5ec40d6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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
23 changes: 23 additions & 0 deletions packages/react/src/components/Dropdown/__tests__/Dropdown-test.js
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

0 comments on commit 5ec40d6

Please sign in to comment.