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: 22850 task title is not focused after refresh #29203

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 40 additions & 0 deletions src/hooks/useAutoFocusInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {useFocusEffect} from '@react-navigation/native';
import {useState, useEffect, useRef, useCallback} from 'react';
import CONST from '../CONST';

export default function useAutoFocusInput() {
const [isInputInitialized, setIsInputInitialized] = useState(false);
const [screenTransitionEnd, setScreenTransitionEnd] = useState(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

as this is boolean, let's use this name - isScreenTransitionEnded

Copy link
Contributor

Choose a reason for hiding this comment

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

@tienifr bump ^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated @aimane-chnaif


const inputRef = useRef(null);
const focusTimeoutRef = useRef(null);

useEffect(() => {
if (!screenTransitionEnd || !isInputInitialized || !inputRef.current) {
return;
}
inputRef.current.focus();
}, [screenTransitionEnd, isInputInitialized]);

useFocusEffect(
useCallback(() => {
focusTimeoutRef.current = setTimeout(() => {
setScreenTransitionEnd(true);
}, CONST.ANIMATED_TRANSITION);
return () => {
if (!focusTimeoutRef.current) {
return;
}
clearTimeout(focusTimeoutRef.current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []),
);

const inputCallbackRef = (ref) => {
inputRef.current = ref;
setIsInputInitialized(true);
};

return {inputCallbackRef};
}
9 changes: 5 additions & 4 deletions src/pages/tasks/NewTaskDetailsPage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useEffect, useRef, useState} from 'react';
import React, {useEffect, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
Expand All @@ -17,6 +17,7 @@ import ROUTES from '../../ROUTES';
import * as Task from '../../libs/actions/Task';
import CONST from '../../CONST';
import * as Browser from '../../libs/Browser';
import useAutoFocusInput from '../../hooks/useAutoFocusInput';

const propTypes = {
/** Beta features list */
Expand All @@ -37,10 +38,11 @@ const defaultProps = {
};

function NewTaskDetailsPage(props) {
const inputRef = useRef();
const [taskTitle, setTaskTitle] = useState(props.task.title);
const [taskDescription, setTaskDescription] = useState(props.task.description || '');

const {inputCallbackRef} = useAutoFocusInput();

useEffect(() => {
setTaskTitle(props.task.title);
setTaskDescription(props.task.description || '');
Expand Down Expand Up @@ -74,7 +76,6 @@ function NewTaskDetailsPage(props) {
}
return (
<ScreenWrapper
onEntryTransitionEnd={() => inputRef.current && inputRef.current.focus()}
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={NewTaskDetailsPage.displayName}
Expand All @@ -95,7 +96,7 @@ function NewTaskDetailsPage(props) {
>
<View style={styles.mb5}>
<TextInput
ref={(el) => (inputRef.current = el)}
ref={inputCallbackRef}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT}
inputID="taskTitle"
label={props.translate('task.title')}
Expand Down
Loading