Skip to content

Commit

Permalink
Revert "fix(input): do not trigger provided onBlur when it's a syntet…
Browse files Browse the repository at this point in the history
…hic event"

This reverts commit ba1612c.
  • Loading branch information
guilhermerodz committed Oct 30, 2024
1 parent ba1612c commit 7203ec4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 16 deletions.
12 changes: 4 additions & 8 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 { OTPInputMetadata, OTPInputProps, RenderProps } from './types'
import { OTPInputProps, RenderProps } from './types'
import { usePrevious } from './use-previous'
import { usePasswordManagerBadge } from './use-pwm-badge'

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

const PWM_BADGE_MARGIN_RIGHT = 18
const PWM_BADGE_SPACE_WIDTH_PX = 40
Expand All @@ -15,13 +15,11 @@ 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 @@ -106,7 +104,6 @@ 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 7203ec4

Please sign in to comment.