devfolioco / react-otp-input

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

follow up on PR #448 #452

Open ahmed-s-fatahallah opened 2 months ago

ahmed-s-fatahallah commented 2 months ago

Hello again,

I have submitted a PR Num (#448) before for fixing the bug where when you focus on any input box the value will always be added to the first input box even if you are focusing on another one, also I added a paste behavior where when you focus on any input and paste the value will be spread across all inputs because as I was mentioned in the previous PR, I felt it's kinda logical behavior. but thanks to you, you pointed out a bug where when pasting from the mobile browser clipboard autofill the values don't appear in the inputs.

Firstly, Thanks for your reply, I didn't notice this bug.

Secondly, I have fixed it by updating the otpValueRef current with the new value array after the input event triggers inside the if check you wrote to check if the user is pasting from auto fill so the otpValueRef gets the same value as pasted value so the input value gets updated with the entered value.

here is where it is updated:

        if (!hasInvalidInput) {
          handleOTPChange(valueArr);
          focusInput(numInputs - 1);
          otpValueRef.current = valueArr;
        }

Finally: you were checking if the value length is the same as the numInputs then do the logic otherwise do nothing. So, I thought I would enhance this by just splitting the value up to the numInputs limit and pasting the up to the limit to handle if the user is auto pasting longer or shorter value.

you can find the change here:

if (value.length > 1) {
        const valueArr = value.split('', numInputs);
        const hasInvalidInput = valueArr.some((cellInput) => !isInputValueValid(cellInput));
        if (!hasInvalidInput) {
          handleOTPChange(valueArr);
          focusInput(value.length - 1);
          otpValueRef.current = valueArr;
        }
      }

if you don't want this behavior you can revert it. it is on a separate commit.

Note: I tested the changes on a Mac, a Windows, and an Android phone but not on IOS because I don't own IOS devices.

akashnimare commented 1 month ago

!review this pr

pantomaxbot[bot] commented 1 month ago

Reviewed up to commit:12b4acf2edd225ce5ef8f3280124a27c6e0e2967