diff --git a/site/src/components/Autocomplete/Autocomplete.tsx b/site/src/components/Autocomplete/Autocomplete.tsx index cdc57bb96c..4d2f78632c 100644 --- a/site/src/components/Autocomplete/Autocomplete.tsx +++ b/site/src/components/Autocomplete/Autocomplete.tsx @@ -4,7 +4,6 @@ import { type ReactNode, type SyntheticEvent, useCallback, - useEffect, useId, useRef, useState, @@ -83,7 +82,6 @@ export function Autocomplete({ "data-testid": testId, }: AutocompleteProps) { const inlineInputRef = useRef(null); - const highlightedValueRef = useRef(null); const [managedOpen, setManagedOpen] = useState(false); const [managedInputValue, setManagedInputValue] = useState(""); const [highlightedValue, setHighlightedValue] = useState(null); @@ -91,7 +89,6 @@ export function Autocomplete({ const listboxId = `${generatedListboxId}-listbox`; const updateHighlightedValue = useCallback((newValue: string | null) => { - highlightedValueRef.current = newValue; setHighlightedValue(newValue); }, []); const isOpen = controlledOpen ?? managedOpen; @@ -166,20 +163,13 @@ export function Autocomplete({ [handleOpenChange, onEscapeKeyDown], ); - useEffect(() => { - if ( - highlightedValue !== null && - !options.some((option) => getOptionValue(option) === highlightedValue) - ) { - updateHighlightedValue(null); - } - }, [highlightedValue, options, getOptionValue, updateHighlightedValue]); - const displayValue = value ? getOptionLabel(value) : ""; const showClearButton = clearable && value && !disabled; const highlightedIndex = options.findIndex( (option) => getOptionValue(option) === highlightedValue, ); + const effectiveHighlightedValue = + highlightedIndex >= 0 ? highlightedValue : null; const activeDescendant = highlightedIndex >= 0 ? `${listboxId}-option-${highlightedIndex}` @@ -202,7 +192,7 @@ export function Autocomplete({ } const currentIndex = options.findIndex( - (option) => getOptionValue(option) === highlightedValueRef.current, + (option) => getOptionValue(option) === highlightedValue, ); const nextIndex = e.key === "ArrowDown" @@ -226,7 +216,7 @@ export function Autocomplete({ } const highlightedOption = options.find( - (option) => getOptionValue(option) === highlightedValueRef.current, + (option) => getOptionValue(option) === highlightedValue, ); if (highlightedOption) { handleSelect(highlightedOption); @@ -332,7 +322,7 @@ export function Autocomplete({ > { if (newValue) { updateHighlightedValue(newValue);