Skip to content

Commit

Permalink
feat: up auto focus
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanOliveiraM committed Apr 24, 2024
1 parent 3bcebf0 commit 34e3d49
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/useRegisterFieldFocus.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useCallback, useEffect } from 'react'

Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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)
}
})
Expand Down

0 comments on commit 34e3d49

Please sign in to comment.