From f86a0d29b261871ffb2ec4ea937a09c242ba594a Mon Sep 17 00:00:00 2001 From: Charles Wahome Date: Fri, 25 Nov 2022 22:07:53 +0800 Subject: [PATCH] Fix: Autocomplete double click (#2255) --- .../auto-complete/AutoComplete.tsx | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/app/views/query-runner/query-input/auto-complete/AutoComplete.tsx b/src/app/views/query-runner/query-input/auto-complete/AutoComplete.tsx index 474586646..7c10b6738 100644 --- a/src/app/views/query-runner/query-input/auto-complete/AutoComplete.tsx +++ b/src/app/views/query-runner/query-input/auto-complete/AutoComplete.tsx @@ -170,12 +170,12 @@ const AutoComplete = (props: IAutoCompleteProps) => { const displayAutoCompleteSuggestions = (url: string) => { + setShouldShowSuggestions(false); + const { index } = getLastDelimiterInUrl(url); - const { previous: preceedingText } = getSearchText(url, index!); + const { previous: preceedingText, searchText: searchTerm } = getSearchText(url, index!); const shouldSuggestVersions = preceedingText === GRAPH_URL + '/'; - setShouldShowSuggestions(false); - let theSuggestions: string[] = []; if (shouldSuggestVersions) { theSuggestions = GRAPH_API_VERSIONS; @@ -184,16 +184,19 @@ const AutoComplete = (props: IAutoCompleteProps) => { theSuggestions = getSuggestions(url, autoCompleteOptions); } - if (theSuggestions.length > 0) { - const filtered = (searchText) ? getFilteredSuggestions(searchText, theSuggestions) : theSuggestions; - if (filtered.length > 0) { - setSuggestions(filtered); - setShouldShowSuggestions(true); - } - } else { - setShouldShowSuggestions(false); + if (theSuggestions.length === 0) { + return; + } + + const filtered = (searchText) ? getFilteredSuggestions(searchText, theSuggestions) : theSuggestions; + if (filtered.length > 0) { + setSuggestions(filtered); + setShouldShowSuggestions(true); } + if (filtered.length === 1 && filtered[0] === searchTerm) { + setShouldShowSuggestions(false); + } } const trackSuggestionSelectionEvent = (suggestion: string) => {