Skip to content
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

Merged
merged 10 commits into from
Aug 13, 2018

Conversation

roborourke
Copy link
Contributor

@roborourke roborourke commented Aug 7, 2018

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 the onChange() prop as well.

Example use cases:

  • Component can be used as a simple post picker
  • Retrieving the post title as well to update block attributes eg. link text
  • Fetching other / additional post data using the _links property of the search result object

How 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:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

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
Copy link
Contributor

@youknowriad youknowriad left a 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 = {} ) {
Copy link
Contributor

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).

Copy link
Contributor Author

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 );
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

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() 🤦‍♂️

@gziolo gziolo added the [Type] Enhancement A suggestion for improvement. label Aug 8, 2018
}
}
}
}

selectLink( link ) {
this.props.onChange( link );
selectLink( link, post ) {
Copy link
Contributor

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

Copy link
Contributor

@youknowriad youknowriad left a 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?

Copy link
Member

@tofumatt tofumatt left a 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`.
Copy link
Member

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?

Copy link
Contributor Author

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 a Popover or inline.

Copy link
Member

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`.
Copy link
Member

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 😄

Copy link
Contributor Author

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`.
Copy link
Member

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 in URLInputBUtton. If you are not conditionally rendering this component: set this to false.

Add some changes based on feedback from @tofumatt, much appreciated!
@tofumatt
Copy link
Member

Changes are good; thanks a bunch!

@youknowriad youknowriad merged commit fd6aa36 into WordPress:master Aug 13, 2018
@roborourke roborourke deleted the patch-1 branch August 14, 2018 10:59
@roborourke
Copy link
Contributor Author

@tofumatt @youknowriad thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants