Skip to content

Commit

Permalink
fix(otp-input): prevent breaking onFocus when ref is null
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermerodz committed Feb 19, 2024
1 parent 95531c8 commit 039974a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,13 @@ export const OTPInput = React.forwardRef<HTMLInputElement, OTPInputProps>(
props.onKeyUp?.(e)
}}
onFocus={e => {
const start = Math.min(inputRef.current.value.length, maxLength - 1)
const end = inputRef.current.value.length
inputRef.current?.setSelectionRange(start, end)
setMirrorSelectionStart(start)
setMirrorSelectionEnd(end)
if (inputRef.current) {
const start = Math.min(inputRef.current.value.length, maxLength - 1)
const end = inputRef.current.value.length
inputRef.current?.setSelectionRange(start, end)
setMirrorSelectionStart(start)
setMirrorSelectionEnd(end)
}
setIsFocused(true)

props.onFocus?.(e)
Expand Down

0 comments on commit 039974a

Please sign in to comment.