Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {useFocusEffect} from '@react-navigation/native';
import React, {forwardRef, useCallback, useImperativeHandle, useRef, useState} from 'react';
import type {ForwardedRef} from 'react';
import {useOnyx} from 'react-native-onyx';
import type {AutoCompleteVariant, MagicCodeInputHandle} from '@components/MagicCodeInput';
import MagicCodeInput from '@components/MagicCodeInput';
import useLocalize from '@hooks/useLocalize';
import {isMobileSafari} from '@libs/Browser';
import {getLatestErrorMessage} from '@libs/ErrorUtils';
import {isValidTwoFactorCode} from '@libs/ValidationUtils';
import {clearAccountMessages, toggleTwoFactorAuth, validateTwoFactorAuth} from '@userActions/Session';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {BaseTwoFactorAuthFormRef} from './types';

Expand Down Expand Up @@ -77,8 +80,32 @@ function BaseTwoFactorAuthForm({autoComplete, validateInsteadOfDisable}: BaseTwo
}
inputRef.current.focus();
},
focusLastSelected() {
if (!inputRef.current) {
return;
}
setTimeout(() => {
inputRef.current?.focusLastSelected();
}, CONST.ANIMATED_TRANSITION);
},
}));

useFocusEffect(
useCallback(() => {
if (!inputRef.current) {
return;
}
// Keyboard won't show if we focus the input with a delay, so we need to focus immediately.
if (!isMobileSafari()) {
setTimeout(() => {
inputRef.current?.focusLastSelected();
}, CONST.ANIMATED_TRANSITION);
} else {
inputRef.current?.focusLastSelected();
}
}, []),
);

return (
<MagicCodeInput
autoComplete={autoComplete}
Expand All @@ -88,6 +115,7 @@ function BaseTwoFactorAuthForm({autoComplete, validateInsteadOfDisable}: BaseTwo
onFulfill={validateAndSubmitForm}
errorText={formError.twoFactorAuthCode ?? getLatestErrorMessage(account)}
ref={inputRef}
autoFocus={false}
/>
);
}
Expand Down
Loading