Skip to content

Commit

Permalink
Merge pull request #37929 from FitseTLT/fix-auto-focus-on-reply-in-th…
Browse files Browse the repository at this point in the history
…read

Fix - mWeb auto focus on reply in thread
  • Loading branch information
marcaaron authored Apr 5, 2024
2 parents ddb83f2 + 6fd4517 commit 0e6dba4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
21 changes: 14 additions & 7 deletions src/components/Composer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,20 @@ function Composer(
disabled={isDisabled}
onKeyPress={handleKeyPress}
onFocus={(e) => {
ReportActionComposeFocusManager.onComposerFocus(() => {
if (!textInput.current) {
return;
}

textInput.current.focus();
});
if (isReportActionCompose) {
ReportActionComposeFocusManager.onComposerFocus(null);
} else {
// While a user edits a comment, if they open the LHN menu, we want to ensure that
// the focus returns to the message edit composer after they click on a menu item (e.g. mark as read).
// To achieve this, we re-assign the focus callback here.
ReportActionComposeFocusManager.onComposerFocus(() => {
if (!textInput.current) {
return;
}

textInput.current.focus();
});
}

props.onFocus?.(e);
}}
Expand Down
8 changes: 4 additions & 4 deletions src/libs/ReportActionComposeFocusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {TextInput} from 'react-native';
import ROUTES from '@src/ROUTES';
import Navigation from './Navigation/Navigation';

type FocusCallback = () => void;
type FocusCallback = (shouldFocusForNonBlurInputOnTapOutside?: boolean) => void;

const composerRef = React.createRef<TextInput>();
const editComposerRef = React.createRef<TextInput>();
Expand All @@ -18,7 +18,7 @@ let mainComposerFocusCallback: FocusCallback | null = null;
*
* @param callback callback to register
*/
function onComposerFocus(callback: FocusCallback, isMainComposer = false) {
function onComposerFocus(callback: FocusCallback | null, isMainComposer = false) {
if (isMainComposer) {
mainComposerFocusCallback = callback;
} else {
Expand All @@ -29,7 +29,7 @@ function onComposerFocus(callback: FocusCallback, isMainComposer = false) {
/**
* Request focus on the ReportActionComposer
*/
function focus() {
function focus(shouldFocusForNonBlurInputOnTapOutside?: boolean) {
/** Do not trigger the refocusing when the active route is not the report route, */
if (!Navigation.isActiveRoute(ROUTES.REPORT_WITH_ID.getRoute(Navigation.getTopmostReportId() ?? ''))) {
return;
Expand All @@ -40,7 +40,7 @@ function focus() {
return;
}

mainComposerFocusCallback();
mainComposerFocusCallback(shouldFocusForNonBlurInputOnTapOutside);
return;
}

Expand Down
7 changes: 6 additions & 1 deletion src/pages/home/report/ContextMenu/ContextMenuActions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import type {MutableRefObject} from 'react';
import React from 'react';
import {InteractionManager} from 'react-native';
// eslint-disable-next-line no-restricted-imports
import type {GestureResponderEvent, Text, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
Expand Down Expand Up @@ -200,7 +201,11 @@ const ContextMenuActions: ContextMenuAction[] = [
onPress: (closePopover, {reportAction, reportID}) => {
if (closePopover) {
hideContextMenu(false, () => {
ReportActionComposeFocusManager.focus();
InteractionManager.runAfterInteractions(() => {
// Normally the focus callback of the main composer doesn't focus when willBlurTextInputOnTapOutside
// is false, so we need to pass true here to override this condition.
ReportActionComposeFocusManager.focus(true);
});
Report.navigateToAndOpenChildReport(reportAction?.childReportID ?? '0', reportAction, reportID);
});
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ function ComposerWithSuggestions(

const setUpComposeFocusManager = useCallback(() => {
// This callback is used in the contextMenuActions to manage giving focus back to the compose input.
ReportActionComposeFocusManager.onComposerFocus(() => {
if (!willBlurTextInputOnTapOutside || !isFocused) {
ReportActionComposeFocusManager.onComposerFocus((shouldFocusForNonBlurInputOnTapOutside = false) => {
if ((!willBlurTextInputOnTapOutside && !shouldFocusForNonBlurInputOnTapOutside) || !isFocused) {
return;
}

Expand Down

0 comments on commit 0e6dba4

Please sign in to comment.