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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useCallback, useState} from 'react';
import React, {useCallback, useState, forwardRef, useImperativeHandle} from 'react';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import MagicCodeInput from '../../../../../components/MagicCodeInput';
Expand Down Expand Up @@ -31,6 +31,7 @@ const defaultProps = {
function BaseTwoFactorAuthForm(props) {
const [formError, setFormError] = useState({});
const [twoFactorAuthCode, setTwoFactorAuthCode] = useState('');
const inputRef = React.useRef(null);

/**
* Handle text input and clear formError upon text change
Expand All @@ -53,6 +54,9 @@ function BaseTwoFactorAuthForm(props) {
* Check that all the form fields are valid, then trigger the submit callback
*/
const validateAndSubmitForm = useCallback(() => {
if (inputRef.current) {
inputRef.current.blur();
}
if (!twoFactorAuthCode.trim()) {
setFormError({twoFactorAuthCode: 'twoFactorAuthForm.error.pleaseFillTwoFactorAuth'});
return;
Expand All @@ -67,6 +71,12 @@ function BaseTwoFactorAuthForm(props) {
Session.validateTwoFactorAuth(twoFactorAuthCode);
}, [twoFactorAuthCode]);

useImperativeHandle(props.innerRef, () => ({
validateAndSubmitForm() {
validateAndSubmitForm();
},
}));

return (
<MagicCodeInput
autoComplete={props.autoComplete}
Expand All @@ -78,6 +88,7 @@ function BaseTwoFactorAuthForm(props) {
onChangeText={onTextInput}
onFulfill={validateAndSubmitForm}
errorText={formError.twoFactorAuthCode ? props.translate(formError.twoFactorAuthCode) : ErrorUtils.getLatestErrorMessage(props.account)}
ref={inputRef}
/>
);
}
Expand All @@ -90,4 +101,12 @@ export default compose(
withOnyx({
account: {key: ONYXKEYS.ACCOUNT},
}),
)(BaseTwoFactorAuthForm);
)(
forwardRef((props, ref) => (
<BaseTwoFactorAuthForm
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
)),
);
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import React from 'react';
import PropTypes from 'prop-types';
import BaseTwoFactorAuthForm from './BaseTwoFactorAuthForm';

function TwoFactorAuthForm() {
return <BaseTwoFactorAuthForm autoComplete="sms-otp" />;
const propTypes = {
/**
* A ref to forward to the Pressable
*/
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
};

const defaultProps = {
innerRef: () => {},
};

function TwoFactorAuthForm(props) {
return (
<BaseTwoFactorAuthForm
ref={props.innerRef}
autoComplete="sms-otp"
/>
);
}

TwoFactorAuthForm.propTypes = propTypes;
TwoFactorAuthForm.defaultProps = defaultProps;
TwoFactorAuthForm.displayName = 'TwoFactorAuthForm';

export default TwoFactorAuthForm;
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import React from 'react';
import PropTypes from 'prop-types';
import BaseTwoFactorAuthForm from './BaseTwoFactorAuthForm';

function TwoFactorAuthForm() {
return <BaseTwoFactorAuthForm autoComplete="one-time-code" />;
const propTypes = {
/**
* A ref to forward to the Pressable
*/
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
};

const defaultProps = {
innerRef: () => {},
};

function TwoFactorAuthForm(props) {
return (
<BaseTwoFactorAuthForm
ref={props.innerRef}
autoComplete="one-time-code"
/>
);
}

TwoFactorAuthForm.propTypes = propTypes;
TwoFactorAuthForm.defaultProps = defaultProps;
TwoFactorAuthForm.displayName = 'TwoFactorAuthForm';

export default TwoFactorAuthForm;
11 changes: 9 additions & 2 deletions src/pages/settings/Security/TwoFactorAuth/VerifyPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const defaultProps = {
};

function VerifyPage(props) {
const formRef = React.useRef(null);

useEffect(() => {
Session.clearAccountMessages();
}, []);
Expand Down Expand Up @@ -139,15 +141,20 @@ function VerifyPage(props) {
<Text style={styles.mt11}>{props.translate('twoFactorAuth.enterCode')}</Text>
</View>
<View style={[styles.mt3, styles.mh5]}>
<TwoFactorAuthForm />
<TwoFactorAuthForm innerRef={formRef} />
Comment thread
tienifr marked this conversation as resolved.
Resolved
</View>
</ScrollView>
<FixedFooter style={[styles.mtAuto, styles.pt2]}>
<Button
success
text={props.translate('common.next')}
isDisabled
isLoading={props.account.isLoading}
onPress={() => {
if (!formRef.current) {
return;
}
formRef.current.validateAndSubmitForm();
Comment thread
tienifr marked this conversation as resolved.
Resolved
}}
/>
</FixedFooter>
</FullPageOfflineBlockingView>
Expand Down