Skip to content

Commit

Permalink
fix: stop certain keyboard events from bubbling in the combobox
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemcd committed Nov 20, 2024
1 parent 5794a9a commit 2ed88f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/small-hairs-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@telegraph/combobox": patch
---

fix events from bubbling in combobox
12 changes: 11 additions & 1 deletion packages/combobox/src/Combobox/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ const Trigger = ({ size = "2", ...props }: TriggerProps) => {
}

if (event.key === "ArrowDown") {
// Don't allow the event to bubble up outside of the menu
event.stopPropagation();
event.preventDefault();
context.onOpenToggle();
return;
}
Expand Down Expand Up @@ -514,6 +517,8 @@ const Content = <T extends TgphElement>({
<DismissableLayer
onEscapeKeyDown={(event) => {
if (context.open) {
// Don't allow the event to bubble up outside of the menu
event.stopPropagation();
event.preventDefault();
context.setOpen(false);
}
Expand Down Expand Up @@ -552,6 +557,9 @@ const Content = <T extends TgphElement>({
event.preventDefault();
}}
onKeyDown={(event: React.KeyboardEvent) => {
// Don't allow the event to bubble up outside of the menu
event.stopPropagation();

// If the first option is focused and the user presses the up
// arrow key, focus the search input
const options = context.contentRef?.current?.querySelectorAll(
Expand Down Expand Up @@ -650,12 +658,14 @@ const Option = <T extends TgphElement>({
: contextValue?.value === value;

const handleSelection = (event: Event | React.KeyboardEvent) => {
// Don't allow the event to bubble up outside of the menu
event.stopPropagation();

// Don't do anything if the key isn't a selection key
const keyboardEvent = event as React.KeyboardEvent;
if (keyboardEvent.key && !SELECT_KEYS.includes(keyboardEvent.key)) return;

// Don't bubble up the event
event.stopPropagation();
event.preventDefault();

if (context.closeOnSelect === true) {
Expand Down

0 comments on commit 2ed88f4

Please sign in to comment.