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

Set focus to Datetimepicker programmatically #80

Closed
izzappel opened this issue Nov 26, 2019 · 7 comments
Closed

Set focus to Datetimepicker programmatically #80

izzappel opened this issue Nov 26, 2019 · 7 comments
Labels
enhancement New feature or request

Comments

@izzappel
Copy link

Is there a way to set the focus to the Datetimepicker programmatically?

I tried to get the ref of the Datetimepicker wrapper and call focus on that one. But it's not enough. I think I would need to set the focus to the first input element. But I do not have the ref of that.

const reactDatePickerRef = useRef(null);

useEffect(() => {
  if (reactDatePickerRef.current && reactDatePickerRef.current.wrapper) {
    // is not enough, need to call `focus` on first input element
    reactDatePickerRef.current.wrapper.focus();
  }
}, []);

return <ReactDateTimePicker ref={reactDatePickerRef} />;

E.g. having a prop firstInputRef for getting the ref to be able to call focus() would be already helpful.

@izzappel izzappel changed the title Focus Datetimepicker programmatically Set focus to Datetimepicker programmatically Nov 26, 2019
@wojtekmaj
Copy link
Owner

Hmm, that's a good one! I tried to continue your way of thinking and used reactDatePickerRef.current.wrapper.querySelector('.react-date-picker__inputGroup__input').focus(); to find a first focusable input. But I think we can both agree it's a horrible way of doing things? Also an undocumented one so I can't guarantee it to be working in future non-breaking versions.

Maybe we should add autoFocus prop to match the native input a little better?

@izzappel
Copy link
Author

izzappel commented Nov 27, 2019

Exactly. I do not really want to rely on undocumented features.
Having an autoFocus would already be helpful.

But if I could choose I would still like the solution for having the freedom to move the focus to the Datepicker by myself.

One scenario that I have in my mind:

  • I have a date filter for which I use the Datepicker. There is a shortcut (e.g. Cmd+F) which moves the focus to that filter input so that the user can filter her/his view by just using the keyboard.
    In this case the autoFocus would not be good enough.

@wojtekmaj
Copy link
Owner

Hmmm, still I think autoFocus will be worth implementing since it's perfectly applicable.

Related:
wojtekmaj/react-date-picker#208
wojtekmaj/react-time-picker#62

@izzappel
Copy link
Author

👍 Cool! autoFocus helps already a lot! 👏
Thank you.

wojtekmaj added a commit that referenced this issue Nov 28, 2019
@wojtekmaj
Copy link
Owner

Out in v2.8.0.

@BobbyWalker
Copy link

Couldn't you give the option of passing in an onFocus property, then in your internal code on when you capture the onFocus event, check
if(this.props.hasOwnProperty('onFocus')) this.props.onFocus(e)
And let them do other things they want to do?

@wojtekmaj
Copy link
Owner

I think you're describing a different problem @BobbyWalker. onFocus would allow people to do things when the user manually focuses the field, and this thread is about allowing autoFocus property to be passed correctly to first input.

@wojtekmaj wojtekmaj added the enhancement New feature or request label Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants