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 and ArrowDown keys for Dropdown #18324

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion packages/react/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,20 @@
}, 3000)
);
}
if (toggleButtonProps.onKeyDown) {

// Allow any key press that is not ArrowUp or ArrowDown
const isNotUpOrDown = evt.key !== 'ArrowUp' && evt.key !== 'ArrowDown';
// Allow any key press other than ArrowUp if the first item is selected
const isNotTopAndUp =
evt.key === 'ArrowUp' && items?.[0] !== selectedItem;
// Allow any key press other than ArrowDown if the last item is selected
const isNotBottomAndDown =
evt.key === 'ArrowDown' &&
items?.[items?.length - 1] !== selectedItem;

Check warning on line 560 in packages/react/src/components/Dropdown/Dropdown.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react/src/components/Dropdown/Dropdown.tsx#L560

Added line #L560 was not covered by tests
if (
toggleButtonProps.onKeyDown &&
(isNotUpOrDown || isOpen || isNotTopAndUp || isNotBottomAndDown)
) {
toggleButtonProps.onKeyDown(evt);
}
},
Expand Down
Loading