Skip to content

Commit

Permalink
Improve autocompleter performance (#41197)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored May 20, 2022
1 parent 860a396 commit a512d78
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/components/src/autocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
useEffect,
useState,
useRef,
useMemo,
} from '@wordpress/element';
import {
ENTER,
Expand Down Expand Up @@ -132,7 +133,7 @@ function useAutocomplete( {
const [ filterValue, setFilterValue ] = useState( '' );
const [ autocompleter, setAutocompleter ] = useState( null );
const [ AutocompleterUI, setAutocompleterUI ] = useState( null );
const [ backspacing, setBackspacing ] = useState( false );
const backspacing = useRef( false );

function insertCompletion( replacement ) {
const end = record.start;
Expand Down Expand Up @@ -218,7 +219,7 @@ function useAutocomplete( {
}

function handleKeyDown( event ) {
setBackspacing( event.keyCode === BACKSPACE );
backspacing.current = event.keyCode === BACKSPACE;

if ( ! autocompleter ) {
return;
Expand Down Expand Up @@ -268,11 +269,11 @@ function useAutocomplete( {
event.preventDefault();
}

let textContent;

if ( isCollapsed( record ) ) {
textContent = getTextContent( slice( record, 0 ) );
}
const textContent = useMemo( () => {
if ( isCollapsed( record ) ) {
return getTextContent( slice( record, 0 ) );
}
}, [ record ] );

useEffect( () => {
if ( ! textContent ) {
Expand Down Expand Up @@ -325,7 +326,8 @@ function useAutocomplete( {
// Ex: "Some text @marcelo sekkkk" <--- "kkkk" caused a mismatch, but
// if the user presses backspace here, it will show the completion popup again.
const matchingWhileBackspacing =
backspacing && textWithoutTrigger.split( /\s/ ).length <= 3;
backspacing.current &&
textWithoutTrigger.split( /\s/ ).length <= 3;

if (
mismatch &&
Expand Down

0 comments on commit a512d78

Please sign in to comment.