Skip to content

Commit

Permalink
Fix useEffect to updateSuggestions().
Browse files Browse the repository at this point in the history
  • Loading branch information
torounit committed Jun 1, 2022
1 parent db88c52 commit edb5402
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/components/src/form-token-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ export function FormTokenField( props: FormTokenFieldProps ) {
false
);

const debouncedSpeak = useDebounce( speak, 500 );
const prevSuggestions = usePrevious< string[] >( suggestions );
const prevValue = usePrevious< ( string | TokenItem )[] >( value );

const input = useRef< HTMLInputElement >( null );
const tokensAndInput = useRef< HTMLInputElement >( null );

const debouncedSpeak = useDebounce( speak, 500 );

useEffect( () => {
// Make sure to focus the input when the isActive state is true.
if ( isActive && ! hasFocus() ) {
Expand All @@ -115,11 +118,18 @@ export function FormTokenField( props: FormTokenFieldProps ) {
prevSuggestions || []
);

if ( suggestionsDidUpdate || value !== prevValue ) {
updateSuggestions( suggestionsDidUpdate );
}

updateSuggestions( suggestionsDidUpdate );
}, [ suggestions ] );
// TODO: updateSuggestions() should first be refactored so its actual deps are clearer.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ suggestions, prevSuggestions, value, prevValue ] );

useEffect( () => {
updateSuggestions();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ incompleteTokenValue ] );

if ( disabled && isActive ) {
Expand Down

0 comments on commit edb5402

Please sign in to comment.