Skip to content

Commit

Permalink
Fix Link UI displaying out of sync results (#57522)
Browse files Browse the repository at this point in the history
* Remove updating suggestions state

* Reset current request after completion and don’t fetch on focus
  • Loading branch information
getdave committed Jan 8, 2024
1 parent 06e57f2 commit 56e53df
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions packages/block-editor/src/components/url-input/index.js
Expand Up @@ -66,7 +66,6 @@ class URLInput extends Component {
this.state = {
suggestions: [],
showSuggestions: false,
isUpdatingSuggestions: false,
suggestionsValue: null,
selectedSuggestion: null,
suggestionsListboxId: '',
Expand Down Expand Up @@ -102,11 +101,7 @@ class URLInput extends Component {
}

// Update suggestions when the value changes.
if (
prevProps.value !== value &&
! this.props.disableSuggestions &&
! this.state.isUpdatingSuggestions
) {
if ( prevProps.value !== value && ! this.props.disableSuggestions ) {
if ( value?.length ) {
// If the new value is not empty we need to update with suggestions for it.
this.updateSuggestions( value );
Expand Down Expand Up @@ -183,7 +178,6 @@ class URLInput extends Component {
}

this.setState( {
isUpdatingSuggestions: true,
selectedSuggestion: null,
loading: true,
} );
Expand All @@ -203,7 +197,6 @@ class URLInput extends Component {

this.setState( {
suggestions,
isUpdatingSuggestions: false,
suggestionsValue: value,
loading: false,
showSuggestions: !! suggestions.length,
Expand Down Expand Up @@ -235,9 +228,15 @@ class URLInput extends Component {
}

this.setState( {
isUpdatingSuggestions: false,
loading: false,
} );
} )
.finally( () => {
// If this is the current promise then reset the reference
// to allow for checking if a new request is made.
if ( this.suggestionsRequest === request ) {
this.suggestionsRequest = null;
}
} );

// Note that this assignment is handled *before* the async search request
Expand All @@ -255,11 +254,12 @@ class URLInput extends Component {

// When opening the link editor, if there's a value present, we want to load the suggestions pane with the results for this input search value
// Don't re-run the suggestions on focus if there are already suggestions present (prevents searching again when tabbing between the input and buttons)
// or there is already a request in progress.
if (
value &&
! disableSuggestions &&
! this.state.isUpdatingSuggestions &&
! ( suggestions && suggestions.length )
! ( suggestions && suggestions.length ) &&
this.suggestionsRequest === null
) {
// Ensure the suggestions are updated with the current input value.
this.updateSuggestions( value );
Expand Down

0 comments on commit 56e53df

Please sign in to comment.