fix: prevent run query when selecting autocomplete option (#3480)

This commit is contained in:
Yury Molodov 2022-12-13 18:30:05 +01:00 committed by Aliaksandr Valialkin
parent ea7940e5a7
commit cf8ddc67ec
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 11 additions and 3 deletions

View file

@ -1,4 +1,4 @@
import React, { FC, useRef } from "preact/compat";
import React, { FC, useRef, useState } from "preact/compat";
import { KeyboardEvent } from "react";
import { ErrorTypes } from "../../../types";
import TextField from "../../Main/TextField/TextField";
@ -32,6 +32,7 @@ const QueryEditor: FC<QueryEditorProps> = ({
disabled = false
}) => {
const [openAutocomplete, setOpenAutocomplete] = useState(false);
const autocompleteAnchorEl = useRef<HTMLDivElement>(null);
const handleSelect = (val: string) => {
@ -59,7 +60,7 @@ const QueryEditor: FC<QueryEditorProps> = ({
}
// execute query
if (enter && !shiftKey) {
if (enter && !shiftKey && !openAutocomplete) {
onEnter();
}
};
@ -84,6 +85,7 @@ const QueryEditor: FC<QueryEditorProps> = ({
options={options}
anchor={autocompleteAnchorEl}
onSelect={handleSelect}
onOpenAutocomplete={setOpenAutocomplete}
/>
)}
</div>;

View file

@ -10,7 +10,8 @@ interface AutocompleteProps {
anchor: Ref<HTMLElement>
disabled?: boolean
maxWords?: number
onSelect: (val: string) => void
onSelect: (val: string) => void,
onOpenAutocomplete?: (val: boolean) => void
}
const Autocomplete: FC<AutocompleteProps> = ({
@ -20,6 +21,7 @@ const Autocomplete: FC<AutocompleteProps> = ({
disabled,
maxWords = 1,
onSelect,
onOpenAutocomplete
}) => {
const wrapperEl = useRef<HTMLDivElement>(null);
@ -99,6 +101,10 @@ const Autocomplete: FC<AutocompleteProps> = ({
setFocusOption(-1);
}, [foundOptions]);
useEffect(() => {
onOpenAutocomplete && onOpenAutocomplete(openAutocomplete);
}, [openAutocomplete]);
useClickOutside(wrapperEl, handleCloseAutocomplete);
return (