Skip to content

Commit

Permalink
Get rid of useEffect and useState. I made it work based off input foc…
Browse files Browse the repository at this point in the history
…us alone via the special prop.
  • Loading branch information
alexstine committed Sep 28, 2022
1 parent 1852c29 commit 98e96e7
Showing 1 changed file with 2 additions and 13 deletions.
15 changes: 2 additions & 13 deletions packages/components/src/form-token-field/token-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { ChangeEvent, ForwardedRef } from 'react';
/**
* WordPress dependencies
*/
import { forwardRef, useEffect, useState } from '@wordpress/element';
import { forwardRef } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -32,17 +32,6 @@ export function UnForwardedTokenInput(

const size = value ? value.length + 1 : 0;

// Is this our first render? Take the value and find out if it should be. If no value, A11Y concerns will not exist so false is okay.
const [ isInitialRender, setIsInitialRender ] = useState(
value ? true : false
);
useEffect( () => {
// If initial render is set to true but the user just placed focus on the input, now focus can be allowed.
if ( isInitialRender && inputHasFocus ) {
setIsInitialRender( false );
}
}, [ inputHasFocus ] );

const onChangeHandler = ( event: ChangeEvent< HTMLInputElement > ) => {
if ( onChange ) {
onChange( {
Expand Down Expand Up @@ -74,7 +63,7 @@ export function UnForwardedTokenInput(
: undefined
}
aria-activedescendant={
! isInitialRender && selectedSuggestionIndex !== -1
inputHasFocus && selectedSuggestionIndex !== -1
? `components-form-token-suggestions-${ instanceId }-${ selectedSuggestionIndex }`
: undefined
}
Expand Down

0 comments on commit 98e96e7

Please sign in to comment.