/
lizardking
/
react_project
Обзор
Документация
Войти
/
lizardking
/
react_project
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/features/refExample/model/useFocusTracker.ts
34 строки
1 KB
LizardKing
Task 5 - useRef
17 окт 2025, 03:34
17 окт 2025, 03:34
e989e42
Код
Авторство
О чём код?
import type { RefObject, FocusEvent } from 'react' import { useRef } from 'react' export function useFocusTracker( firstInputRef: RefObject<HTMLInputElement | null>, secondInputRef: RefObject<HTMLInputElement | null>, ) { const movesRef = useRef<number>(0) const handleFocusCapture = (event: FocusEvent<HTMLInputElement>) => { const from = event.relatedTarget as HTMLElement | null const to = event.currentTarget const isFromFirst = from === firstInputRef.current const isFromSecond = from === secondInputRef.current const isToFirst = to === firstInputRef.current const isToSecond = to === secondInputRef.current const cross = (isFromFirst && isToSecond) || (isFromSecond && isToFirst) if (cross) { movesRef.current += 1 // eslint-disable-next-line no-console console.log(`FocusTracker: moves=${movesRef.current}`) } } const focusFirstInput = () => { firstInputRef.current?.focus() // eslint-disable-next-line no-console console.log('FocusTracker: focus() → first input') } return { handleFocusCapture, focusFirstInput } }