devfolioco / react-otp-input

:heavy_check_mark: OTP Input Component for React
http://devfolioco.github.io/react-otp-input
MIT License
654 stars 424 forks source link

fix: android backspace not working #390

Closed suleymanbariseser closed 1 year ago

suleymanbariseser commented 1 year ago
prateek3255 commented 1 year ago

@suleymanbariseser, thanks for working on this. I think this code block is also not needed anymore, because this was added in v2 just to handle this case I believe

https://github.com/devfolioco/react-otp-input/blob/e7cc58e814fd8c7052e51c153e82754495b87271/src/index.tsx#L105-L113

Will give it a run, can you also check if any other cases in the handleKeyDown method need this as well?

prateek3255 commented 1 year ago

@suleymanbariseser Were you able to make the changes here?

suleymanbariseser commented 1 year ago

@prateek3255 sorry for the delay, I checked it and it seems this condition does not work also;

else if (event.key === otp[activeInput]) {
  event.preventDefault();
  focusInput(activeInput + 1);
}

However, I think it is not possible to solve this problem by checking event object because keyCode is always 229 and key is "Unidentified"

prateek3255 commented 1 year ago

Okay what could be the possible solution here?

suleymanbariseser commented 1 year ago

I think instead of comparing current value and pressed key, it would be better having a fallback condition which will move cursor to next input because it does not even work if I press a different key (on desktop browser) when I am focused at the end of the current input.

Screenshot 2023-04-11 at 23 05 08

It does not work because handleChange will be triggered with a value like "xc" and isInputValueValid will return as false so it will not move forward.

if (isInputValueValid(value)) {
  changeCodeAtFocus(value);
  focusInput(activeInput + 1);
}
divyanshu-srivastava-vs commented 1 year ago

This bug is also raised in react thanks for the solution @suleymanbariseser. Please merge this pr as soon as possible. Need to make patch for the same.

prateek3255 commented 1 year ago

Yeah you are right @suleymanbariseser when the input is selected, key and keyCode are unidentified and 229 respectively. So we'll have to figure out a different way handle this. But this PR at least partially solves the problem by selecting the last input on backspace. So bringing this in for the time being will open another issue for handling that edge case

prateek3255 commented 1 year ago

@all-contributors please add @suleymanbariseser for bug

allcontributors[bot] commented 1 year ago

@prateek3255

I've put up a pull request to add @suleymanbariseser! :tada: