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

Setting custom component results in focus error after selecting a date (this.input ref is null) #746

Closed
fc opened this issue Jul 6, 2018 · 3 comments

Comments

@fc
Copy link

fc commented Jul 6, 2018

Steps to reproduce:
Click within the input field, then select a date. Notice the errors appearing in the console.

Here's the repro:
https://codesandbox.io/s/jp60wk9jj3

Error:

Uncaught TypeError: Cannot read property 'focus' of null
    at DayPickerInput.handleOverlayFocus (DayPickerInput.js:390)

Although in my local development environment I get this slightly different error:

DayPickerInput.js:390 Uncaught TypeError: this.input.focus is not a function
    at DayPickerInput.handleOverlayFocus (DayPickerInput.js:390)

In either case, it looks like the ref is null at this line:
https://github.com/gpbl/react-day-picker/blob/master/src/DayPickerInput.js#L371

Setting keepFocus={false} works around the issue.

@gpbl
Copy link
Owner

gpbl commented Jul 7, 2018

Hi @fc yes your component should support a focus method (see docs) or otherwise better set keepFocus to false.

This is not the first time someone is confused by this, I think I should make that feature optional...

@fc
Copy link
Author

fc commented Jul 7, 2018

Thanks!

And curiously enough I did not realize styled-components do not expose the focus method so for those who may come across this in the future, here are docs on setting up a styled-component to expose focus:
https://github.com/styled-components/styled-components/blob/master/docs/tips-and-tricks.md#refs-to-dom-nodes

@gpbl
Copy link
Owner

gpbl commented Jul 7, 2018

Oh I see! Thanks for the heads up about styles-components, I wasn’t aware of that.

kimamula pushed a commit to kimamula/react-day-picker that referenced this issue Aug 17, 2022
kimamula pushed a commit to kimamula/react-day-picker that referenced this issue Aug 17, 2022
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

No branches or pull requests

2 participants