Skip to content

Commit

Permalink
implement a hook to delay auto focus on text inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
akinwale committed Oct 2, 2023
1 parent aabe4d6 commit 1a80a9e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const CONST = {
OUT: 'out',
},
ARROW_HIDE_DELAY: 3000,
INPUT_FOCUS_DELAY: 600,

API_ATTACHMENT_VALIDATIONS: {
// 24 megabytes in bytes, this is limit set on servers, do not update without wider internal discussion
Expand Down
25 changes: 25 additions & 0 deletions src/hooks/useDelayedInputFocus.js
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]),
);
}
7 changes: 6 additions & 1 deletion src/pages/workspace/WorkspaceNewRoomPage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState, useCallback, useMemo} from 'react';
import React, {useState, useCallback, useMemo, useRef} from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
Expand All @@ -25,6 +25,7 @@ import policyMemberPropType from '../policyMemberPropType';
import FullPageNotFoundView from '../../components/BlockingViews/FullPageNotFoundView';
import compose from '../../libs/compose';
import variables from '../../styles/variables';
import useDelayedInputFocus from '../../hooks/useDelayedInputFocus';

const propTypes = {
/** All reports shared with the user */
Expand Down Expand Up @@ -144,6 +145,9 @@ function WorkspaceNewRoomPage(props) {
[translate],
);

const roomNameInputRef = useRef(null);
useDelayedInputFocus(roomNameInputRef);

return (
<FullPageNotFoundView
shouldShow={!Permissions.canUsePolicyRooms(props.betas) || !workspaceOptions.length}
Expand Down Expand Up @@ -177,6 +181,7 @@ function WorkspaceNewRoomPage(props) {
>
<View style={styles.mb5}>
<RoomNameInput
ref={(el) => (roomNameInputRef.current = el)}
inputID="roomName"
isFocused={props.isFocused}
shouldDelayFocus
Expand Down

0 comments on commit 1a80a9e

Please sign in to comment.