-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement a hook to delay auto focus on text inputs
- Loading branch information
Showing
3 changed files
with
32 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {useCallback, useRef} from 'react'; | ||
import {useFocusEffect} from '@react-navigation/native'; | ||
import CONST from '../CONST'; | ||
|
||
/** | ||
* Focus a text input when a screen is navigated to, after the specified time delay has elapsed. | ||
* | ||
* @param {Object} inputRef | ||
* @param {Number} [delay] | ||
*/ | ||
export default function useDelayedInputFocus(inputRef, delay = CONST.INPUT_FOCUS_DELAY) { | ||
const timeoutRef = useRef(null); | ||
|
||
useFocusEffect( | ||
useCallback(() => { | ||
timeoutRef.current = setTimeout(() => inputRef.current && inputRef.current.focus(), delay); | ||
return () => { | ||
if (!timeoutRef.current) { | ||
return; | ||
} | ||
clearTimeout(timeoutRef.current); | ||
}; | ||
}, [delay, inputRef]), | ||
); | ||
} |
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