Skip to content

Commit

Permalink
chore: possible fix for the autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Dec 6, 2024
1 parent feeeee6 commit c5434aa
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions frontend/src/components/AutocompleteClientLocation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const AutocompleteClientLocation: React.ForwardRefExoticComponent<AutocompleteCo
const [location, setLocation] = useState<AutocompleteProps | null>(null);
const [client, setClient] = useState<AutocompleteProps | null>(null);
const [valueTyped, setValueTyped] = useState<string | null>(null);
const [locationName, setLocationName] = useState<string | null>(null);

const clearClient = () => {
updateOptions("locations", []);
Expand All @@ -98,22 +99,24 @@ const AutocompleteClientLocation: React.ForwardRefExoticComponent<AutocompleteCo
selectClient(autocompleteEvent.selectedItem)
};

const handleBlur = () => {
console.log("handleBlur",valueTyped,options["clients"]);
console.log("handleBlur",valueTyped,options["clients"]?.find((item: AutocompleteProps) => item.label === valueTyped));
}

useEffect(() => {
const selectedItem = options["clients"]?.find((item: AutocompleteProps) => item.label === valueTyped);
if(valueTyped && selectedItem){
console.log("handleClientChange",selectedItem);
selectClient(selectedItem);
}

if(valueTyped)
fetchOptions(valueTyped, "clients")
},[valueTyped]);

useEffect(() => {

const selectedItem = options["locations"]?.find((item: AutocompleteProps) => item.label === locationName);
if(locationName && selectedItem){
setLocation(selectedItem);
}
}, [locationName]);

useImperativeHandle(ref, () => ({
reset: () => setLocation(null)
}));
Expand All @@ -131,7 +134,6 @@ const AutocompleteClientLocation: React.ForwardRefExoticComponent<AutocompleteCo
selectedItem={client}
onInputChange={setValueTyped}
onChange={handleClientChange}
onBlur={handleBlur}
itemToElement={(item: AutocompleteProps) => item.label}
helperText="Search by client name, number or acronym"
items={options["clients"] || []}
Expand All @@ -141,6 +143,7 @@ const AutocompleteClientLocation: React.ForwardRefExoticComponent<AutocompleteCo
disabled={!isActive}
id="client-location"
className="flex-fill"
onInputChange={setLocationName}
onChange={(item: AutocompleteComboboxProps) => setLocation(item.selectedItem)}
itemToElement={(item: AutocompleteProps) => item.label}
items={options["locations"] || [{ id: "", label: "No results found" }]}
Expand Down

0 comments on commit c5434aa

Please sign in to comment.