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

Allow imperative calls to focus the otp input via ref #420

Open
asenmitrev opened this issue Sep 28, 2023 · 3 comments
Open

Allow imperative calls to focus the otp input via ref #420

asenmitrev opened this issue Sep 28, 2023 · 3 comments

Comments

@asenmitrev
Copy link

  • Describe the Feature Request
    A way to imperatively focus the code input at the active input position, rather than relying on just shouldAutoFocus.

Right now, to focus the input, one has to set shouldAutoFocus to true, as that internally focuses the first input. To focus again, one must reset shouldAutoFocus to false and then set it to true. There are many use cases where imperative calling of the focus() function might be required and this is a useful feature to have.

A separate problem is that this hack will only focus the input with index 0, not the activeInput.

  • Describe Preferred Solution
    We could use useImperativeHandle and pass a ref to the otp component, where we impement something like this:
useImperativeHandle(
  ref,
  () => ({
    focus() {
      inputRefs.current[activeInput]?.focus();
    },
  }),
  [activeInput],
);

If you consider this a viable solution I can open a PR with an implementation.

@prateek3255
Copy link
Collaborator

Yeah @asenmitrev This makes sense can you open a PR for the same?

@asenmitrev
Copy link
Author

@prateek3255 here is the PR

@asenmitrev
Copy link
Author

@prateek3255 would you say this will be reviewed and merged at some point?

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