Skip to content

Commit

Permalink
fix: don't capture non-left mouse clicks on combobox
Browse files Browse the repository at this point in the history
Right-clicking on a combobox input or menu option should
not behave like a left click. this commit skips the
custom mouse down handling if not left-click.
  • Loading branch information
mike-mike-mike-mike-mike committed Dec 21, 2023
1 parent 987d408 commit cbaca10
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/components/Combobox/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ function Combobox<T>({
setFocusedOptionIndex(visibleIndex);
}}
onMouseDown={(ev) => {
if (ev.button !== 0) {
return;
}

ev.preventDefault();
ev.stopPropagation();
selectOption(option.value);
Expand Down Expand Up @@ -308,6 +312,10 @@ function Combobox<T>({
data-testid="create-new-option"
disabled={!isValidNewOption(inputValue)}
onMouseDown={(ev) => {
if (ev.button !== 0) {
return;
}

ev.preventDefault();
ev.stopPropagation();
createOption();
Expand Down Expand Up @@ -386,17 +394,22 @@ function Combobox<T>({
onChange(undefined);
}
}}
onMouseDown={(e) => {
onMouseDown={(ev) => {
if (ev.button !== 0) {
ev.preventDefault();
return;
}

if (!multi && selected) {
inputElement!.current!.setSelectionRange(0, 0);

if ((selected as Option<T>).label === inputValue) {
e.preventDefault();
ev.preventDefault();
}
}

// @ts-ignore
e.target.focus();
ev.target.focus();
}}
onKeyDown={handleOptionsKeyboardNav}
onKeyPress={clearSelectedPreview}
Expand All @@ -412,6 +425,10 @@ function Combobox<T>({
disabled={disabled}
active={open}
onMouseDown={(ev: React.MouseEvent<HTMLButtonElement>) => {
if (ev.button !== 0) {
return;
}

ev.stopPropagation();
setOpen(!open);
}}
Expand Down

0 comments on commit cbaca10

Please sign in to comment.