From 34e3d49807e6d519ed179b3ab9c0ac3830dd1f41 Mon Sep 17 00:00:00 2001 From: Allan Oliveira Date: Wed, 24 Apr 2024 09:43:38 -0300 Subject: [PATCH] feat: up auto focus --- package.json | 2 +- src/hooks/useRegisterFieldFocus.ts | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 303b3c2..2b26dbc 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "react", "typescript" ], - "version": "2.3.9", + "version": "2.3.10", "main": "./dist/index.cjs.js", "module": "./dist/index.esm.js", "types": "./dist/index.d.ts", diff --git a/src/hooks/useRegisterFieldFocus.ts b/src/hooks/useRegisterFieldFocus.ts index 84b913e..ce8e444 100644 --- a/src/hooks/useRegisterFieldFocus.ts +++ b/src/hooks/useRegisterFieldFocus.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ /* eslint-disable @typescript-eslint/no-explicit-any */ import { useCallback, useEffect } from 'react' @@ -9,6 +10,16 @@ export const useRegisterFieldFocus = (fieldName?: string | undefined) => { state => state.sequentialFieldNamesRef ) + useEffect(() => { + const t = setInterval(() => { + console.log(sequentialFieldNamesRef.current) + }, 1000) + + return () => { + clearInterval(t) + } + }, [sequentialFieldNamesRef]) + const autoFocusContextValue = AutoFocusContext.useContext() if (fieldName && autoFocusContextValue?.setFocus) { @@ -17,6 +28,9 @@ export const useRegisterFieldFocus = (fieldName?: string | undefined) => { const onKeyDown = useCallback( (e: any) => { + console.log('autoFocusContextValue', autoFocusContextValue) + console.log('sequentialFieldNamesRef', sequentialFieldNamesRef) + if ( !autoFocusContextValue?.setFocus || !Array.isArray(sequentialFieldNamesRef.current) @@ -31,13 +45,19 @@ export const useRegisterFieldFocus = (fieldName?: string | undefined) => { val => val === fieldName ) + console.log('fieldIndex', fieldIndex) + if (typeof fieldIndex === 'number' && fieldIndex !== -1) { const nextFieldNameIndex = fieldIndex + 1 const nextFieldName = sequentialFieldNamesRef.current[nextFieldNameIndex] + console.log('nextFieldName', nextFieldName) + if (nextFieldName) { autoFocusContextValue.trigger?.(fieldName).then(passed => { if (passed) { + console.log('called') + autoFocusContextValue.setFocus?.(nextFieldName) } })