Skip to content

Commit

Permalink
Merge pull request #28646 from akinwale/task-27607
Browse files Browse the repository at this point in the history
fix: room name field does not automatically focus when returning from the chat option
  • Loading branch information
robertjchen authored Oct 5, 2023
2 parents eb5e1e8 + 7be57f5 commit 76c971b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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.ANIMATED_TRANSITION) {
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]),
);
}
9 changes: 8 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, useEffect, useCallback, useMemo} from 'react';
import React, {useState, useCallback, useEffect, 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';
import ValuePicker from '../../components/ValuePicker';

const propTypes = {
Expand Down Expand Up @@ -154,6 +155,11 @@ function WorkspaceNewRoomPage(props) {
[translate],
);

const roomNameInputRef = useRef(null);

// use a 600ms delay for delayed focus on the room name input field so that it works consistently on native iOS / Android
useDelayedInputFocus(roomNameInputRef, 600);

return (
<FullPageNotFoundView
shouldShow={!Permissions.canUsePolicyRooms(props.betas) || !workspaceOptions.length}
Expand Down Expand Up @@ -186,6 +192,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 76c971b

Please sign in to comment.