Open
Conversation
…selected or focused
…ValueRef to reflect on the input value
…ts number feat: Handle when the pasted value is longer than the inputs number style: Remove unused useRef import fix: Pasting fewer value than numInputs doesn't work
Member
|
!review this pr |
Comment on lines
+89
to
+91
| if (value.trim() === '') { | ||
| otpValueRef.current = Array(numInputs); | ||
| } |
There was a problem hiding this comment.
Resetting otpValueRef.current to an empty array on every render when value is an empty string can cause unnecessary re-renders and may not be efficient. Consider using a useEffect hook to handle this condition.
| .getData('text/plain') | ||
| .slice(0, numInputs - activeInput) | ||
| .split(''); | ||
| const pastedData = event.clipboardData.getData('text/plain').slice(0, numInputs).split(''); |
There was a problem hiding this comment.
pastedData.slice(0, numInputs) should be pastedData.slice(0, numInputs - activeInput) to ensure the pasted data fits within the remaining input fields.
| if (pos >= activeInput && pastedData.length > 0) { | ||
| otp[pos] = pastedData.shift() ?? ''; | ||
| if (pastedData.length > 0) { | ||
| currentOtpValue[pos] = pastedData.shift() ?? ''; |
There was a problem hiding this comment.
The loop should start from activeInput instead of 0 to ensure the pasted data starts from the currently focused input.
|
Reviewed up to commit:12b4acf2edd225ce5ef8f3280124a27c6e0e2967 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
otpValueRefcurrent 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:
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 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.