From 039974adcbf72faeb70bd0c40b9d1729aca27be4 Mon Sep 17 00:00:00 2001 From: Guilherme Rodz Date: Mon, 19 Feb 2024 17:25:10 -0300 Subject: [PATCH] fix(otp-input): prevent breaking `onFocus` when ref is null --- src/input.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/input.tsx b/src/input.tsx index 34b7964..19c5488 100644 --- a/src/input.tsx +++ b/src/input.tsx @@ -266,11 +266,13 @@ export const OTPInput = React.forwardRef( 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)