-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: Pass the the search result object to props.onChange #8662
Conversation
This component is great however the number of use cases it can support increases a huge amount if it makes the search result object available to the `onChange()` prop as well. Example use cases: - Retrieving the post title, ID and post type, as well as URL to update block attributes - Fetching other / additional post data using the `_links` property of the search result object
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.
LGTM overall, I left some small comments and I'd appreciate other opinions on whether this is a good idea or not. cc @WordPress/gutenberg-core
@@ -131,9 +131,9 @@ class URLInput extends Component { | |||
this.suggestionsRequest = request; | |||
} | |||
|
|||
onChange( event ) { | |||
onChange( event, post = {} ) { |
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.
I guess this post argument is useless because it's always undefined anyway (default 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.
Not totally sure I follow your meaning, do you mean leave out the default value or change the call to this.props.onChange()
to this.props.onChange( ...arguments )
instead? I felt this was a bit more readable and the documentation would be simpler if the type was always consistent.
const inputValue = event.target.value; | ||
this.props.onChange( inputValue ); | ||
this.props.onChange( inputValue, post ); |
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.
Should we make the post
argument to the onChange
prop nullable and just avoid passing it here? If I understand properly this is a special case where don't select anything from the suggestions.
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 is the only place this.props.onChange()
is called, not passing it here would remove the intended change wouldn't it?
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.
oh my bad, thought selectLink()
called this.onChange()
🤦♂️
} | ||
} | ||
} | ||
} | ||
|
||
selectLink( link ) { | ||
this.props.onChange( link ); | ||
selectLink( link, post ) { |
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.
Should we remove the first argument from this method, as it's already in post.url
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.
LGTM 👍 Anyone for another opinion here?
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.
I think this is a solid change but there are documentation tweaks I think are worth doing before merging. I can make them in a few hours if no one can get to them beforehand–if they're made before I get the chance I'll re-review and merge when I'm back 😄
|
||
# `URLInput` | ||
|
||
Renders the URL input normally wrapped by `URLInputButton`. |
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.
What does this mean? Oh, wait, I think I see. I think this should be something like:
Renders a URL input field; this component should typically be wrapped in a
URLInputButton
component.
right?
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.
I think saying this might be an improvement:
Renders the URL input field used by the
URLInputButton
component. It can be used directly to display the input field in different ways such as in aPopover
or inline.
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.
Agreed, that's great 👍
|
||
### `onChange( url: String, ?post: Object ): Function` | ||
|
||
*Required.* Called when the value changes. This is the same as the `onChange` prop described above for `URLInputButton`. |
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.
Saying "this is the same" is a bit confusing to me–does that mean it's functionally the same or it's literally passed from URLInputButton
to this component? If it's just that it behaves similarly I think it's worth duplicating the documentation because it's annoying to have to hunt around a file for docs 😄
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.
It's literally passed directly from the URLInputButton
component. But I think there's no harm in duplicating it for the sake of ease as you say 👍
|
||
### `autoFocus: Boolean` | ||
|
||
*Optional.* By default, the input will gain focus when it is rendered as typically it is used in combination with a `Popover` in `URLInputBUtton`. If you are rendering the component all the time set this to `false`. |
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.
A comma after "rendered" would improve readability here. I also think you could tweak the second sentence:
By default, the input will gain focus when it is rendered, as typically it is used in combination with a
Popover
inURLInputBUtton
. If you are not conditionally rendering this component: set this tofalse
.
Add some changes based on feedback from @tofumatt, much appreciated!
Changes are good; thanks a bunch! |
@tofumatt @youknowriad thank you! |
Description
The
URLInput
component is great however the number of use cases it can support increases a huge amount if it makes the search result object available to theonChange()
prop as well.Example use cases:
_links
property of the search result objectHow has this been tested?
I made a copy of the component with the proposed modifications locally and used it as a post picker. I updated not only the URL but was able to set other attributes such as a post title and fetch the post's featured image.
Types of changes
New feature (non-breaking change which adds functionality)
Checklist: