Skip to content
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
1 change: 1 addition & 0 deletions src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ const CONST = {
ANIMATED_HIGHLIGHT_END_DELAY: 800,
ANIMATED_HIGHLIGHT_END_DURATION: 2000,
ANIMATED_TRANSITION: 300,
KEYBOARD_RESTORATION_FLAG_RESET_DELAY: 100,
SIDE_PANEL_ANIMATED_TRANSITION: 300,
ANIMATED_TRANSITION_FROM_VALUE: 100,
ANIMATED_PROGRESS_BAR_DELAY: 300,
Expand Down
5 changes: 3 additions & 2 deletions src/components/Form/FormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {InteractionManager} from 'react-native';
import type {StyleProp, TextInputSubmitEditingEvent, ViewStyle} from 'react-native';
import {useInputBlurActions} from '@components/InputBlurContext';
import type {LocalizedTranslate} from '@components/LocaleContextProvider';
import {getIsRestoringKeyboardFocus} from '@components/TextInput';
Comment thread
TaduJR marked this conversation as resolved.
Resolved
import useDebounceNonReactive from '@hooks/useDebounceNonReactive';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
Expand Down Expand Up @@ -419,8 +420,8 @@ function FormProvider({
return;
}
setTouchedInput(inputID);
// We don't validate the form on blur in case the current screen is not focused
if (shouldValidateOnBlur && isFocusedRef.current) {
// Skip validation if the screen is not focused or keyboard focus is being restored (Android mWeb)
Comment thread
TaduJR marked this conversation as resolved.
Resolved
if (shouldValidateOnBlur && isFocusedRef.current && !getIsRestoringKeyboardFocus()) {
Comment thread
TaduJR marked this conversation as resolved.
Resolved
onValidate(inputValues, !hasServerError);
}
}, VALIDATE_DELAY);
Expand Down
6 changes: 6 additions & 0 deletions src/components/TextInput/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ function TextInput({ref, navigation, ...props}: BaseTextInputProps) {
);
}

// No-op on native — keyboard restoration is only needed on mobile Chrome (web)
function getIsRestoringKeyboardFocus() {
return false;
}

export default TextInput;
export {getIsRestoringKeyboardFocus};
58 changes: 49 additions & 9 deletions src/components/TextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import useThemeStyles from '@hooks/useThemeStyles';
import {isMobileChrome} from '@libs/Browser';
import DomUtils from '@libs/DomUtils';
import Visibility from '@libs/Visibility';
import CONST from '@src/CONST';
import BaseTextInput from './BaseTextInput';
import type {BaseTextInputProps} from './BaseTextInput/types';
import * as styleConst from './styleConst';

type RemoveVisibilityListener = () => void;
let isRestoringKeyboardFocus = false;

Comment thread
TaduJR marked this conversation as resolved.
Resolved
function getIsRestoringKeyboardFocus() {
return isRestoringKeyboardFocus;
}

function TextInput({ref, ...props}: BaseTextInputProps) {
const styles = useThemeStyles();
const textInputRef = useRef<HTMLFormElement | null>(null);
const removeVisibilityListenerRef = useRef<RemoveVisibilityListener>(null);
const removeVisibilityListenerRef = useRef<(() => void) | null>(null);

useEffect(() => {
Comment thread
TaduJR marked this conversation as resolved.
Resolved
let removeVisibilityListener = removeVisibilityListenerRef.current;
if (props.disableKeyboard) {
textInputRef.current?.setAttribute('inputmode', 'none');
}
Expand All @@ -25,19 +29,54 @@ function TextInput({ref, ...props}: BaseTextInputProps) {
textInputRef.current?.setAttribute('name', props.name);
Comment thread
TaduJR marked this conversation as resolved.
Resolved
}

// On mobile Chrome, restore keyboard after returning from background via blur/refocus with a delay.
// See: https://developer.android.com/develop/ui/views/touch-and-input/keyboard-input/visibility#ShowReliably
let focusTimeoutId: ReturnType<typeof setTimeout>;
let flagResetTimeoutId: ReturnType<typeof setTimeout>;

const restoreKeyboardFocus = () => {
if (isRestoringKeyboardFocus || !textInputRef.current || DomUtils.getActiveElement() !== textInputRef.current) {
return;
}

const inputElement = textInputRef.current;
isRestoringKeyboardFocus = true;

inputElement.blur();
focusTimeoutId = setTimeout(() => {
if (DomUtils.getActiveElement() !== document.body) {
Comment thread
TaduJR marked this conversation as resolved.
Resolved
isRestoringKeyboardFocus = false;
return;
}
inputElement.focus();
flagResetTimeoutId = setTimeout(() => {
isRestoringKeyboardFocus = false;
}, CONST.KEYBOARD_RESTORATION_FLAG_RESET_DELAY);
}, CONST.ANIMATED_TRANSITION);
Comment thread
TaduJR marked this conversation as resolved.
Resolved
};

if (typeof window !== 'undefined' && isMobileChrome()) {
window.addEventListener('focus', restoreKeyboardFocus);
}

let removeVisibilityListener = removeVisibilityListenerRef.current;
removeVisibilityListener = Visibility.onVisibilityChange(() => {
Comment thread
TaduJR marked this conversation as resolved.
Resolved
if (!isMobileChrome() || !Visibility.isVisible() || !textInputRef.current || DomUtils.getActiveElement() !== textInputRef.current) {
if (!isMobileChrome() || !Visibility.isVisible()) {
return;
}
textInputRef.current.blur();
textInputRef.current.focus();
restoreKeyboardFocus();
});

return () => {
Comment thread
TaduJR marked this conversation as resolved.
Resolved
if (!removeVisibilityListener) {
return;
clearTimeout(focusTimeoutId);
clearTimeout(flagResetTimeoutId);
isRestoringKeyboardFocus = false;
if (typeof window !== 'undefined') {
window.removeEventListener('focus', restoreKeyboardFocus);
}
if (removeVisibilityListener) {
removeVisibilityListener();
}
removeVisibilityListener();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down Expand Up @@ -78,3 +117,4 @@ function TextInput({ref, ...props}: BaseTextInputProps) {
}

export default TextInput;
export {getIsRestoringKeyboardFocus};
Loading