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

Validate that getMenuProps has set the ref correctly #525

Merged
merged 3 commits into from
Jul 29, 2018
Merged

Validate that getMenuProps has set the ref correctly #525

merged 3 commits into from
Jul 29, 2018

Conversation

alexandernanberg
Copy link
Collaborator

@alexandernanberg alexandernanberg commented Jul 22, 2018

What:
Fixes #490 by using the same approach as we do in getRootProps.

Why:
Because it could lead to unexpected behavior and to avoid confusion.

How:
I'm still unsure of what the best solution for this is. I would like to make the check in the render method as we do for the getRootProps, but I don't think that's possible because the ref hasn't set this._menuNode on the first render. I guess we could loop through all children but that seems unnecessary heavy/slow.

So instead I make the validation/check in componentDidUpdate, which looks to work fine.

Another problem I encountered was that there is no way to check if this._menuNode is a composite element or not, which makes it hard to use the same validation as we do for getRootProps.

I wanted you opinion on this before I continue and add tests and update the documentation. Is there another better way that I've not thought of?

Checklist:

  • Documentation
  • Tests
  • Ready to be merged
  • Added myself to contributors table N/A

Copy link
Member

@kentcdodds kentcdodds 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 solid. Could you write a test for this please?

Also, perhaps we should change our thrown errors to console.warn or console.error so we can remove them for the production build to make things smaller. Something to think about in the future.

@@ -983,6 +990,10 @@ class Downshift extends Component {
}

componentDidUpdate(prevProps, prevState) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add this to a componentDidMount as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep that would make sense!

@alexandernanberg
Copy link
Collaborator Author

Okay, I'll do that! Hopefully I'll have time before I go on vacation on Wednesday.

That would be a great idea, what do you think of using something like https://www.npmjs.com/package/warning? But I'll probably do this in another PR in that case

@kentcdodds
Copy link
Member

Sounds perfect 👍

@alexandernanberg alexandernanberg changed the title WIP throw an error if getMenuProps ref insn't set correctly Validate that getMenuProps has set the ref correctly Jul 23, 2018
@alexandernanberg
Copy link
Collaborator Author

Added tests and updated the getMenuProps section in the README.md now. Basically just copied the tests for getRootProps and changed them where it was needed

src/downshift.js Outdated
@@ -983,6 +994,10 @@ class Downshift extends Component {
}

componentDidUpdate(prevProps, prevState) {
if (this.getMenuProps.called && !this.getMenuProps.suppressRefError) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be:

if (
  this.getMenuProps.called &&
  !this.getMenuProps.suppressRefError &&
  preval`module.exports = process.env.BUILD_REACT_NATIVE !== 'true'`
) {

so this code can be eliminated for native. Same with the above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll fix that tomorrow! Hmm any reason why we dont do that for ’getRootProps’ as well? Im not so familiar with RN but why wouldnt we want to validate the ref?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just checked and the only place we're using _rootNode is for codepaths that react native doesn't need. So we could probably do the same thing for those.

I'm thinking about making a custom macro that will shorten things for us/make these kinds of branches easier to maintain. But that'll be for later :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay! I've pushed the latest changes now, and I think you should be able to use --autosquash to squash them together to one commit

I like the sound of that idea!

kentcdodds
kentcdodds previously approved these changes Jul 24, 2018
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Do you know why the build failed? Coverage issue? Maybe we should add an Istanbul ignore comment.

@alexandernanberg
Copy link
Collaborator Author

Hmm this is super weird... the error says that preval is not defined. Tried adding istanbul ignore comments locally but still get the same issue also🤔

@kentcdodds
Copy link
Member

:-( I'm afraid I don't have time to look into this at the moment :-/

@alexandernanberg
Copy link
Collaborator Author

No worries, I’m off a couple of days but will pick this up again when I get back!

@alexandernanberg
Copy link
Collaborator Author

alexandernanberg commented Jul 28, 2018

Looks like everything is working now, don't really know why /* istanbul ignore if */ didn't work but /* istanbul ignore next */ just before preval did 🤔

It did work locally but apparently not remotely.... I'll continue investigating

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@kentcdodds kentcdodds merged commit 653fe7d into downshift-js:master Jul 29, 2018
Rendez pushed a commit to Rendez/downshift that referenced this pull request Sep 30, 2018
…js#525)

* fix: validate that getMenuProps has set the ref correctly

* fixup! fix: validate that getMenuProps has set the ref correctly

* fixup! fix: validate that getMenuProps has set the ref correctly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

parent.contains is not a function when used with styled components
2 participants