Allow imperative calls to focus the otp input via ref
-
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
useImperativeHandleand 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.
Yeah @asenmitrev This makes sense can you open a PR for the same?
@prateek3255 here is the PR
@prateek3255 would you say this will be reviewed and merged at some point?