Skip to content

Commit

Permalink
fix(input): do not trigger provided onBlur when it's a syntethic event
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermerodz committed Oct 29, 2024
1 parent 363c66c commit ba1612c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
12 changes: 8 additions & 4 deletions packages/input-otp/src/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react'

import { REGEXP_ONLY_DIGITS } from './regexp'
import { syncTimeouts } from './sync-timeouts'
import { OTPInputProps, RenderProps } from './types'
import { OTPInputMetadata, OTPInputProps, RenderProps } from './types'
import { usePrevious } from './use-previous'
import { usePasswordManagerBadge } from './use-pwm-badge'

Expand Down Expand Up @@ -68,14 +68,13 @@ export const OTPInput = React.forwardRef<HTMLInputElement, OTPInputProps>(
typeof window !== 'undefined' &&
window?.CSS?.supports?.('-webkit-touch-callout', 'none'),
})
const inputMetadataRef = React.useRef<{
prev: [number | null, number | null, 'none' | 'forward' | 'backward']
}>({
const inputMetadataRef = React.useRef<OTPInputMetadata>({
prev: [
inputRef.current?.selectionStart,
inputRef.current?.selectionEnd,
inputRef.current?.selectionDirection,
],
willSyntethicBlur: false,
})
React.useImperativeHandle(ref, () => inputRef.current, [])
React.useEffect(() => {
Expand Down Expand Up @@ -270,6 +269,7 @@ export const OTPInput = React.forwardRef<HTMLInputElement, OTPInputProps>(
const pwmb = usePasswordManagerBadge({
containerRef,
inputRef,
inputMetadataRef,
pushPasswordManagerStrategy,
isFocused,
})
Expand Down Expand Up @@ -434,6 +434,10 @@ export const OTPInput = React.forwardRef<HTMLInputElement, OTPInputProps>(
props.onFocus?.(e)
}}
onBlur={e => {
if (inputMetadataRef.current.willSyntethicBlur) {
inputMetadataRef.current.willSyntethicBlur = false
return
}
setIsFocused(false)
props.onBlur?.(e)
}}
Expand Down
4 changes: 4 additions & 0 deletions packages/input-otp/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ export type OTPInputProps = OTPInputBaseProps &
children: React.ReactNode
}
)
export type OTPInputMetadata = {
prev: [number | null, number | null, 'none' | 'forward' | 'backward']
willSyntethicBlur: boolean
}
5 changes: 4 additions & 1 deletion packages/input-otp/src/use-pwm-badge.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { OTPInputProps } from './types'
import { OTPInputMetadata, OTPInputProps } from './types'

const PWM_BADGE_MARGIN_RIGHT = 18
const PWM_BADGE_SPACE_WIDTH_PX = 40
Expand All @@ -15,11 +15,13 @@ const PASSWORD_MANAGERS_SELECTORS = [
export function usePasswordManagerBadge({
containerRef,
inputRef,
inputMetadataRef,
pushPasswordManagerStrategy,
isFocused,
}: {
containerRef: React.RefObject<HTMLDivElement>
inputRef: React.RefObject<HTMLInputElement>
inputMetadataRef: React.RefObject<OTPInputMetadata>
pushPasswordManagerStrategy: OTPInputProps['pushPasswordManagerStrategy']
isFocused: boolean
}) {
Expand Down Expand Up @@ -104,6 +106,7 @@ export function usePasswordManagerBadge({
// to trigger a re-position of the badge.
if (!pwmMetadata.current.refocused && document.activeElement === input) {
const sel = [input.selectionStart, input.selectionEnd]
inputMetadataRef.current.willSyntethicBlur = true
input.blur()
input.focus()
// Recover the previous selection
Expand Down

0 comments on commit ba1612c

Please sign in to comment.