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 6f5e822 commit fc8d7fa
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 37 deletions.
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.11",
"version": "2.3.12",
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
"types": "./dist/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const AsyncSelect = <FormType extends FieldValues>(props: AsyncSelectProps<FormT
}
}, [isSearchable, loadOptions])

const onKeyDownAuto = useRegisterFieldFocus(name)
const onKeyDownAuto = useRegisterFieldFocus(name, disabled)

return (
<RootContainer
Expand Down
2 changes: 1 addition & 1 deletion src/components/molecules/Fields/Field/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const Field = forwardRef<HTMLInputElement, FieldProps>((props, ref) => {
setIsFocused(false)
}

const onKeyDown = useRegisterFieldFocus(name)
const onKeyDown = useRegisterFieldFocus(name, disabled)

return (
<RootContainer
Expand Down
2 changes: 1 addition & 1 deletion src/components/molecules/Fields/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const Select = <FormType extends FieldValues>(props: SelectProps<FormType>) => {
[isMulti, reactSelectStyles, selectedColor, styleMode]
)

const onKeyDownAuto = useRegisterFieldFocus(name)
const onKeyDownAuto = useRegisterFieldFocus(name, disabled)

return (
<RootContainer
Expand Down
52 changes: 19 additions & 33 deletions src/hooks/useRegisterFieldFocus.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useCallback, useEffect } from 'react'

import { AutoFocusContext } from 'contexts/AutoFocusContext'
import { FieldNextFocusManagerContext } from 'contexts/FieldNextFocusManagerContext'

export const useRegisterFieldFocus = (fieldName?: string | undefined) => {
export const useRegisterFieldFocus = (
fieldName: string | undefined,
isDisabled: boolean | undefined
) => {
const sequentialFieldNamesRef = FieldNextFocusManagerContext.useSelector(
state => state.sequentialFieldNamesRef
)

useEffect(() => {
const t = setInterval(() => {
console.log(sequentialFieldNamesRef.current)
}, 1000)

return () => {
clearInterval(t)
}
}, [sequentialFieldNamesRef])

const autoFocusContextValue = AutoFocusContext.useContext()

const onKeyDown = useCallback(
(e: any) => {
console.log('autoFocusContextValue', autoFocusContextValue)
console.log('sequentialFieldNamesRef', sequentialFieldNamesRef)

if (
!autoFocusContextValue?.setFocus ||
!Array.isArray(sequentialFieldNamesRef.current)
!Array.isArray(sequentialFieldNamesRef.current) ||
isDisabled
) {
return
}
Expand All @@ -41,48 +31,44 @@ 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)
}
})
}
}
}
},
[autoFocusContextValue, fieldName, sequentialFieldNamesRef]
[autoFocusContextValue, fieldName, isDisabled, sequentialFieldNamesRef]
)

useEffect(() => {
if (fieldName && autoFocusContextValue?.setFocus) {
sequentialFieldNamesRef.current.push(fieldName)
}

return () => {
if (!autoFocusContextValue?.setFocus) {
return
}

const clear = () => {
if (Array.isArray(sequentialFieldNamesRef.current)) {
sequentialFieldNamesRef.current = sequentialFieldNamesRef.current.filter(
val => val !== fieldName
)
}
}

if (isDisabled) {
return clear
}

if (fieldName && autoFocusContextValue?.setFocus) {
sequentialFieldNamesRef.current.push(fieldName)
}

return clear

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [autoFocusContextValue?.setFocus])
}, [autoFocusContextValue?.setFocus, isDisabled])

return onKeyDown
}

0 comments on commit fc8d7fa

Please sign in to comment.