Skip to content

Commit

Permalink
Improve variable naming for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Aug 6, 2021
1 parent 1f1470d commit b57d27f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/components/src/combobox-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,14 @@ function ComboboxControl( {
inputContainer.current.input.focus();
};

// Update selections on filter change.
// Update current selections when the filter input changes.
useEffect( () => {
const currentSelectionNotInSuggestions =
matchingSuggestions.indexOf( selectedSuggestion ) < 0;
if ( matchingSuggestions.length && currentSelectionNotInSuggestions ) {
// If the current selection isn't present in the list of suggestions, then automatically select the first item from the list of suggestion
const hasMatchingSuggestions = matchingSuggestions.length > 0;
const hasSelectedMatchingSuggestions =
matchingSuggestions.indexOf( selectedSuggestion ) > 0;

if ( hasMatchingSuggestions && ! hasSelectedMatchingSuggestions ) {
// If the current selection isn't present in the list of suggestions, then automatically select the first item from the list of suggestions.
setSelectedSuggestion( matchingSuggestions[ 0 ] );
}
}, [ matchingSuggestions, selectedSuggestion ] );
Expand Down

0 comments on commit b57d27f

Please sign in to comment.