Skip to content

Commit

Permalink
Merge pull request #47621 from dominictb/fix/44042-close-keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
cead22 authored Oct 11, 2024
2 parents 1220039 + 21d0f80 commit 2014ab8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
15 changes: 14 additions & 1 deletion src/libs/ReportActionComposeFocusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import navigationRef from './Navigation/navigationRef';
type FocusCallback = (shouldFocusForNonBlurInputOnTapOutside?: boolean) => void;

const composerRef: MutableRefObject<TextInput | null> = React.createRef<TextInput>();
const editComposerRef = React.createRef<TextInput>();

// There are two types of composer: general composer (edit composer) and main composer.
// The general composer callback will take priority if it exists.
const editComposerRef: MutableRefObject<TextInput | null> = React.createRef<TextInput>();
// There are two types of focus callbacks: priority and general
// Priority callback would take priority if it existed
let priorityFocusCallback: FocusCallback | null = null;
Expand Down Expand Up @@ -58,6 +61,7 @@ function focus(shouldFocusForNonBlurInputOnTapOutside?: boolean) {
*/
function clear(isPriorityCallback = false) {
if (isPriorityCallback) {
editComposerRef.current = null;
priorityFocusCallback = null;
} else {
focusCallback = null;
Expand All @@ -78,6 +82,14 @@ function isEditFocused(): boolean {
return !!editComposerRef.current?.isFocused();
}

/**
* Utility function to blur both main composer and edit composer.
*/
function blurAll(): void {
composerRef.current?.blur();
editComposerRef.current?.blur();
}

export default {
composerRef,
onComposerFocus,
Expand All @@ -86,4 +98,5 @@ export default {
isFocused,
editComposerRef,
isEditFocused,
blurAll,
};
2 changes: 2 additions & 0 deletions src/pages/home/report/ReportActionItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import Navigation from '@libs/Navigation/Navigation';
import Permissions from '@libs/Permissions';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import ReportActionComposeFocusManager from '@libs/ReportActionComposeFocusManager';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import SelectionScraper from '@libs/SelectionScraper';
Expand Down Expand Up @@ -338,6 +339,7 @@ function ReportActionItem({
return;
}

ReportActionComposeFocusManager.blurAll();
setIsContextMenuActive(true);
const selection = SelectionScraper.getCurrentSelection();
ReportActionContextMenu.showContextMenu(
Expand Down
27 changes: 19 additions & 8 deletions src/pages/home/report/ReportActionItemMessageEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ function ReportActionItemMessageEdit(
};
}, []);

useEffect(
// Remove focus callback on unmount to avoid stale callbacks
() => {
if (textInputRef.current) {
ReportActionComposeFocusManager.editComposerRef.current = textInputRef.current;
}
return () => {
if (ReportActionComposeFocusManager.editComposerRef.current !== textInputRef.current) {
return;
}
ReportActionComposeFocusManager.clear(true);
};
},
[],
);

// We consider the report action active if it's focused, its emoji picker is open or its context menu is open
const isActive = useCallback(
() => isFocusedRef.current || EmojiPickerAction.isActive(action.reportActionID) || ReportActionContextMenu.isActiveReportAction(action.reportActionID),
Expand All @@ -185,14 +201,6 @@ function ReportActionItemMessageEdit(
}, true);
}, [focus]);

useEffect(
// Remove focus callback on unmount to avoid stale callbacks
() => () => {
ReportActionComposeFocusManager.clear(true);
},
[],
);

useEffect(
() => {
if (isInitialMount.current) {
Expand Down Expand Up @@ -512,6 +520,9 @@ function ReportActionItemMessageEdit(
style={[styles.textInputCompose, styles.flex1, styles.bgTransparent]}
onFocus={() => {
setIsFocused(true);
if (textInputRef.current) {
ReportActionComposeFocusManager.editComposerRef.current = textInputRef.current;
}
InteractionManager.runAfterInteractions(() => {
requestAnimationFrame(() => {
reportScrollManager.scrollToIndex(index, true);
Expand Down

0 comments on commit 2014ab8

Please sign in to comment.