-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dac798a
commit 5324571
Showing
10 changed files
with
198 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import React from 'react' | ||
import { FieldValues, UseFormSetFocus, UseFormTrigger } from 'react-hook-form' | ||
|
||
import { createContext } from '@nexpy/react-easy-context-api' | ||
|
||
import { WithChildren } from 'types' | ||
|
||
type AutoFocusContextValue<FormValues extends FieldValues = any> = { | ||
trigger: UseFormTrigger<FormValues> | null | ||
setFocus: UseFormSetFocus<FormValues> | null | ||
} | ||
|
||
export const AutoFocusContext = createContext<AutoFocusContextValue>({ | ||
trigger: null, | ||
setFocus: null, | ||
} as AutoFocusContextValue) | ||
|
||
type AutoFocusProviderProps<FormValues extends FieldValues> = WithChildren<{ | ||
trigger: UseFormTrigger<FormValues> | ||
setFocus: UseFormSetFocus<FormValues> | ||
}> | ||
|
||
export const AutoFocus = <FormValues extends FieldValues>({ | ||
children, | ||
trigger, | ||
setFocus, | ||
}: AutoFocusProviderProps<FormValues>) => { | ||
return ( | ||
<AutoFocusContext.Provider | ||
value={{ | ||
trigger, | ||
setFocus, | ||
}} | ||
> | ||
{children} | ||
</AutoFocusContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React, { useRef, MutableRefObject } from 'react' | ||
|
||
import { createContext } from '@nexpy/react-easy-context-api' | ||
|
||
import { WithChildren } from 'types' | ||
|
||
type FieldNextFocusManagerContextValue = { | ||
sequentialFieldNamesRef: MutableRefObject<string[]> | ||
} | ||
|
||
export const FieldNextFocusManagerContext = | ||
createContext<FieldNextFocusManagerContextValue>( | ||
{} as FieldNextFocusManagerContextValue | ||
) | ||
|
||
export const FieldNextFocusManagerProvider = ({ children }: WithChildren) => { | ||
const sequentialFieldNamesRef = useRef<string[]>([]) | ||
|
||
return ( | ||
<FieldNextFocusManagerContext.Provider value={{ sequentialFieldNamesRef }}> | ||
{children} | ||
</FieldNextFocusManagerContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './useOnClickOutside' | ||
export * from './useMask' | ||
export * from './useRegisterFieldFocus' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* 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) => { | ||
const sequentialFieldNamesRef = FieldNextFocusManagerContext.useSelector( | ||
state => state.sequentialFieldNamesRef | ||
) | ||
|
||
const autoFocusContextValue = AutoFocusContext.useContext() | ||
|
||
if (fieldName && autoFocusContextValue?.setFocus) { | ||
sequentialFieldNamesRef.current.push(fieldName) | ||
} | ||
|
||
const onKeyDown = useCallback( | ||
(e: any) => { | ||
if ( | ||
!autoFocusContextValue?.setFocus || | ||
!Array.isArray(sequentialFieldNamesRef.current) | ||
) { | ||
return | ||
} | ||
|
||
if (e.key === 'Enter') { | ||
e.preventDefault() | ||
|
||
const fieldIndex = sequentialFieldNamesRef.current.findIndex( | ||
val => val === fieldName | ||
) | ||
|
||
if (typeof fieldIndex === 'number' && fieldIndex !== -1) { | ||
const nextFieldNameIndex = fieldIndex + 1 | ||
const nextFieldName = sequentialFieldNamesRef.current[nextFieldNameIndex] | ||
|
||
if (nextFieldName) { | ||
autoFocusContextValue.trigger?.(fieldName).then(passed => { | ||
if (passed) { | ||
autoFocusContextValue.setFocus?.(nextFieldName) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
[autoFocusContextValue, fieldName, sequentialFieldNamesRef] | ||
) | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (!autoFocusContextValue?.setFocus) { | ||
return | ||
} | ||
|
||
if (Array.isArray(sequentialFieldNamesRef.current)) { | ||
sequentialFieldNamesRef.current = sequentialFieldNamesRef.current.filter( | ||
val => val !== fieldName | ||
) | ||
} | ||
} | ||
}, [autoFocusContextValue?.setFocus, fieldName, sequentialFieldNamesRef]) | ||
|
||
return onKeyDown | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters