-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
URLInput: keep the search results label in sync with the results list #45806
Conversation
Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
Size Change: +19 B (0%) Total Size: 1.32 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch and nice work fixing it 🚀 Works well in my testing and makes a lot of sense, especially when reviewed commit by commit.
@@ -81,7 +81,6 @@ const LinkControlSearchInput = forwardRef( | |||
...props, | |||
instanceId, | |||
withCreateSuggestion, | |||
currentInputValue: value, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like something that needs to be mentioned in the CHANGELOG?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I now added info to block-editor
about the URLInput.renderSuggestions
having a new currentInputValue
parameter.
@@ -60,13 +60,14 @@ class URLInput extends Component { | |||
|
|||
this.suggestionNodes = []; | |||
|
|||
this.isUpdatingSuggestions = false; | |||
this.suggestionsRequest = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These component instance properties are always a recipe for weird bugs since React doesn't usually observe changes in them. Maybe we should ideally get rid of them all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.suggestionNodes
is used to store refs, it can't be in state and needs to stay ref-like.
this.suggestionsRequest
is used to prevent state updates from a request if another request has been issued in the meantime. It can't be in state because we need to set it to null
on unmount, so that responses arriving after unmount are ignored.
…n block-editor changelog
458a775
to
4459a77
Compare
Fixes a bug in
URLInput
that I discovered when updating unit tests for the component during the React 18 migration (#45235). When typing into the input field, it displays suggestions either for the empty value ("initial suggestions", list of recently updated pages):or for a non-empty value (the label is visually hidden in production, I made it visible for testing purposes):
The problem is that the label ("Recently updated" or "Search results for") could get out of sync with the list of results. If I change the value from
"act"
to""
, theURLInput
'svalue
prop changes immediately, and the label is immediately rerendered from "Search results for 'act'" to "Recently updated", but the results list changes only a while later, after a REST API fetch finishes. That means we're rendering a mis-labeled list in the meantime.It came up when working on a unit test, because the bug makes it impossible to catch the moment when the list get updated. This would be a great way to do it but it doesn't work:
After this patch, it will start working.
There are several other drive-by changes to
URLInput
, best reviewed commit by commit:shouldShowInitialSuggestions
method runs only on mount, incDM
, when the state is guaranteed to have initial values. Therefore we can remove the checks that look atthis.state
.onChange
handler doesn't need to callthis.updateSuggestions
. ThecDU
lifecycle will always do it when thevalue
prop changes.value
where we don't want to trigger suggestions search. In that case, cancel the outstanding REST request from a previous query, and setstate.suggestions
to[]
.this.isUpdatingSuggestions
field can be moved to state, because it is part of state. There was always asetState
call nearby when I was refactoring: I only needed to add new property to it, never add a new one.How to test:
The
initialSuggestions
feature is enabled only when editing a Custom Link (core/navigation-link
) inside the Navigation (core/navigation
) block, so you need to open the site editor and add a block structure like:Then you can play with editing the input and fetching the suggestions.