From 3bc2591d8c72761b646221ff5244b600818ea13d Mon Sep 17 00:00:00 2001 From: Mutesasira Moses Date: Thu, 16 Jan 2025 12:10:54 +0300 Subject: [PATCH] Update AutoComplete.js --- frontend/src/components/common/AutoComplete.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/common/AutoComplete.js b/frontend/src/components/common/AutoComplete.js index c43e787db..90ff473ab 100644 --- a/frontend/src/components/common/AutoComplete.js +++ b/frontend/src/components/common/AutoComplete.js @@ -68,21 +68,27 @@ function AutoComplete(props) { const onKeyDown = (e) => { // Handeling enter key + const { onSelect } = props; if (e.keyCode === 13) { - e.preventDefault(); if (filteredSuggestions[activeSuggestion]) { const selectedValue = filteredSuggestions[activeSuggestion].value; - setTextValue(selectedValue); // Update local state + setUserInput(selectedValue); + setTextValue(selectedValue); setShowSuggestions(false); + setInvalid(false); + + if (typeof onSelect === "function") { + onSelect(filteredSuggestions[activeSuggestion].id); + } } - } + } // Handeling up arrow else if (e.keyCode === 38) { if (activeSuggestion === 0) { return; } setActiveSuggestion(activeSuggestion - 1); - } + } // Handeling down arrow else if (e.keyCode === 40) { if (activeSuggestion === filteredSuggestions.length - 1) {