Skip to content

Commit 6c4624b

Browse files
authored
Merge pull request #56396 from bernhardoj/fix/55485-prevent-autofocus-on-small-screen
Prevent autofocus/refocus on small screen on the report page
2 parents d720841 + 4e8007c commit 6c4624b

File tree

3 files changed

+7
-80
lines changed

3 files changed

+7
-80
lines changed

src/components/Composer/implementation/index.native.tsx

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ import type {FileObject} from '@components/AttachmentModal';
88
import type {ComposerProps} from '@components/Composer/types';
99
import type {AnimatedMarkdownTextInputRef} from '@components/RNMarkdownTextInput';
1010
import RNMarkdownTextInput from '@components/RNMarkdownTextInput';
11-
import useAutoFocusInput from '@hooks/useAutoFocusInput';
1211
import useMarkdownStyle from '@hooks/useMarkdownStyle';
13-
import useResetComposerFocus from '@hooks/useResetComposerFocus';
1412
import useStyleUtils from '@hooks/useStyleUtils';
1513
import useTheme from '@hooks/useTheme';
1614
import useThemeStyles from '@hooks/useThemeStyles';
1715
import {containsOnlyEmojis} from '@libs/EmojiUtils';
1816
import {splitExtensionFromFileName} from '@libs/fileDownload/FileUtils';
19-
import getPlatform from '@libs/getPlatform';
2017
import CONST from '@src/CONST';
2118

2219
const excludeNoStyles: Array<keyof MarkdownStyle> = [];
@@ -29,7 +26,6 @@ function Composer(
2926
isDisabled = false,
3027
maxLines,
3128
isComposerFullSize = false,
32-
autoFocus = false,
3329
style,
3430
// On native layers we like to have the Text Input not focused so the
3531
// user can read new chats without the keyboard in the way of the view.
@@ -42,22 +38,12 @@ function Composer(
4238
ref: ForwardedRef<TextInput>,
4339
) {
4440
const textInput = useRef<AnimatedMarkdownTextInputRef | null>(null);
45-
const {isFocused, shouldResetFocusRef} = useResetComposerFocus(textInput);
4641
const textContainsOnlyEmojis = useMemo(() => containsOnlyEmojis(value ?? ''), [value]);
4742
const theme = useTheme();
4843
const markdownStyle = useMarkdownStyle(value, !isGroupPolicyReport ? excludeReportMentionStyle : excludeNoStyles);
4944
const styles = useThemeStyles();
5045
const StyleUtils = useStyleUtils();
5146

52-
const {inputCallbackRef, inputRef: autoFocusInputRef} = useAutoFocusInput();
53-
54-
useEffect(() => {
55-
if (autoFocus === !!autoFocusInputRef.current) {
56-
return;
57-
}
58-
inputCallbackRef(autoFocus ? textInput.current : null);
59-
}, [autoFocus, inputCallbackRef, autoFocusInputRef]);
60-
6147
useEffect(() => {
6248
if (!textInput.current || !textInput.current.setSelection || !selection || isComposerFullSize) {
6349
return;
@@ -88,10 +74,6 @@ function Composer(
8874
return;
8975
}
9076

91-
if (autoFocus) {
92-
inputCallbackRef(el);
93-
}
94-
9577
// This callback prop is used by the parent component using the constructor to
9678
// get a ref to the inner textInput element e.g. if we do
9779
// <constructor ref={el => this.textInput = el} /> this will not
@@ -141,22 +123,10 @@ function Composer(
141123
textAlignVertical="center"
142124
style={[composerStyle, maxHeightStyle]}
143125
markdownStyle={markdownStyle}
144-
// /*
145-
// There are cases in hybird app on android that screen goes up when there is autofocus on keyboard. (e.g. https://github.com/Expensify/App/issues/53185)
146-
// Workaround for this issue is to maunally focus keyboard after it's acutally rendered which is done by useAutoFocusInput hook.
147-
// */
148-
autoFocus={getPlatform() !== 'android' ? autoFocus : false}
149126
/* eslint-disable-next-line react/jsx-props-no-spreading */
150127
{...props}
151128
readOnly={isDisabled}
152129
onPaste={pasteFile}
153-
onBlur={(e) => {
154-
if (!isFocused) {
155-
// eslint-disable-next-line react-compiler/react-compiler
156-
shouldResetFocusRef.current = true; // detect the input is blurred when the page is hidden
157-
}
158-
props?.onBlur?.(e);
159-
}}
160130
onClear={onClear}
161131
/>
162132
);

src/hooks/useResetComposerFocus.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
TextInputKeyPressEventData,
1212
TextInputScrollEventData,
1313
} from 'react-native';
14-
import {AppState, DeviceEventEmitter, findNodeHandle, InteractionManager, Keyboard, NativeModules, View} from 'react-native';
14+
import {DeviceEventEmitter, findNodeHandle, InteractionManager, NativeModules, View} from 'react-native';
1515
import {useFocusedInputHandler} from 'react-native-keyboard-controller';
1616
import type {OnyxEntry} from 'react-native-onyx';
1717
import {useOnyx} from 'react-native-onyx';
@@ -180,6 +180,10 @@ const debouncedBroadcastUserIsTyping = lodashDebounce(
180180

181181
const willBlurTextInputOnTapOutside = willBlurTextInputOnTapOutsideFunc();
182182

183+
// We want consistent auto focus behavior on input between native and mWeb so we have some auto focus management code that will
184+
// prevent auto focus for mobile device
185+
const shouldFocusInputOnScreenFocus = canFocusInputOnScreenFocus();
186+
183187
/**
184188
* This component holds the value and selection state.
185189
* If a component really needs access to these state values it should be put here.
@@ -259,7 +263,7 @@ function ComposerWithSuggestions(
259263
const {shouldUseNarrowLayout} = useResponsiveLayout();
260264
const maxComposerLines = shouldUseNarrowLayout ? CONST.COMPOSER.MAX_LINES_SMALL_SCREEN : CONST.COMPOSER.MAX_LINES;
261265

262-
const shouldAutoFocus = !modal?.isVisible && shouldShowComposeInput && areAllModalsHidden() && isFocused && !didHideComposerInput;
266+
const shouldAutoFocus = shouldFocusInputOnScreenFocus && !modal?.isVisible && shouldShowComposeInput && areAllModalsHidden() && isFocused && !didHideComposerInput;
263267

264268
const valueRef = useRef(value);
265269
valueRef.current = value;
@@ -654,14 +658,7 @@ function ComposerWithSuggestions(
654658
// We want to focus or refocus the input when a modal has been closed or the underlying screen is refocused.
655659
// We avoid doing this on native platforms since the software keyboard popping
656660
// open creates a jarring and broken UX.
657-
if (
658-
!(
659-
(willBlurTextInputOnTapOutside || (shouldAutoFocus && canFocusInputOnScreenFocus())) &&
660-
!isNextModalWillOpenRef.current &&
661-
!isModalVisible &&
662-
(!!prevIsModalVisible || !prevIsFocused)
663-
)
664-
) {
661+
if (!((willBlurTextInputOnTapOutside || shouldAutoFocus) && !isNextModalWillOpenRef.current && !isModalVisible && (!!prevIsModalVisible || !prevIsFocused))) {
665662
return;
666663
}
667664

@@ -722,21 +719,6 @@ function ComposerWithSuggestions(
722719
tag.set(findNodeHandle(textInputRef.current) ?? -1);
723720
}, [tag]);
724721

725-
useEffect(() => {
726-
const appStateSubscription = AppState.addEventListener('change', (nextAppState) => {
727-
if (!nextAppState.match(/inactive|background/)) {
728-
focus(true);
729-
return;
730-
}
731-
732-
Keyboard.dismiss();
733-
});
734-
735-
return () => {
736-
appStateSubscription.remove();
737-
};
738-
}, [focus]);
739-
740722
useFocusedInputHandler(
741723
{
742724
onSelectionChange: (event) => {

0 commit comments

Comments
 (0)