Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: room name field does not automatically focus when returning from the chat option #28646

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

Comment on lines +160 to +161
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akinwale Is there a particular reason for 600ms timing here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Loading