Skip to content

Commit 039974a

Browse files
committed
fix(otp-input): prevent breaking onFocus when ref is null
1 parent 95531c8 commit 039974a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/input.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,13 @@ export const OTPInput = React.forwardRef<HTMLInputElement, OTPInputProps>(
266266
props.onKeyUp?.(e)
267267
}}
268268
onFocus={e => {
269-
const start = Math.min(inputRef.current.value.length, maxLength - 1)
270-
const end = inputRef.current.value.length
271-
inputRef.current?.setSelectionRange(start, end)
272-
setMirrorSelectionStart(start)
273-
setMirrorSelectionEnd(end)
269+
if (inputRef.current) {
270+
const start = Math.min(inputRef.current.value.length, maxLength - 1)
271+
const end = inputRef.current.value.length
272+
inputRef.current?.setSelectionRange(start, end)
273+
setMirrorSelectionStart(start)
274+
setMirrorSelectionEnd(end)
275+
}
274276
setIsFocused(true)
275277

276278
props.onFocus?.(e)

0 commit comments

Comments
 (0)