Skip to content

Commit d051341

Browse files
authored
Merge pull request #24002 from joh42/fix/23593
2 parents 55eccde + f588696 commit d051341

File tree

7 files changed

+49
-53
lines changed

7 files changed

+49
-53
lines changed

src/components/MagicCodeInput.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ const propTypes = {
2727
/** Should the input auto focus */
2828
autoFocus: PropTypes.bool,
2929

30-
/** Whether we should wait before focusing the TextInput, useful when using transitions */
31-
shouldDelayFocus: PropTypes.bool,
32-
3330
/** Error text to display */
3431
errorText: PropTypes.string,
3532

@@ -58,7 +55,6 @@ const defaultProps = {
5855
value: undefined,
5956
name: '',
6057
autoFocus: true,
61-
shouldDelayFocus: false,
6258
errorText: '',
6359
shouldSubmitOnComplete: true,
6460
innerRef: null,
@@ -141,28 +137,6 @@ function MagicCodeInput(props) {
141137
// eslint-disable-next-line react-hooks/exhaustive-deps
142138
}, [props.value, props.shouldSubmitOnComplete]);
143139

144-
useEffect(() => {
145-
if (!props.autoFocus) {
146-
return;
147-
}
148-
149-
let focusTimeout = null;
150-
if (props.shouldDelayFocus) {
151-
focusTimeout = setTimeout(() => inputRefs.current[0].focus(), CONST.ANIMATED_TRANSITION);
152-
} else {
153-
inputRefs.current[0].focus();
154-
}
155-
156-
return () => {
157-
if (!focusTimeout) {
158-
return;
159-
}
160-
clearTimeout(focusTimeout);
161-
};
162-
// We only want this to run on mount
163-
// eslint-disable-next-line react-hooks/exhaustive-deps
164-
}, []);
165-
166140
/**
167141
* Callback for the onFocus event, updates the indexes
168142
* of the currently focused input.
@@ -290,7 +264,7 @@ function MagicCodeInput(props) {
290264
<View style={[StyleSheet.absoluteFillObject, styles.w100, isMobileSafari ? styles.bgTransparent : styles.opacity0]}>
291265
<TextInput
292266
ref={(ref) => (inputRefs.current[index] = ref)}
293-
autoFocus={index === 0 && props.autoFocus && !props.shouldDelayFocus}
267+
autoFocus={index === 0 && props.autoFocus}
294268
inputMode="numeric"
295269
textContentType="oneTimeCode"
296270
name={props.name}

src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class ContactMethodDetailsPage extends Component {
103103
this.state = {
104104
isDeleteModalOpen: false,
105105
};
106+
107+
this.validateCodeFormRef = React.createRef();
106108
}
107109

108110
componentDidMount() {
@@ -217,7 +219,7 @@ class ContactMethodDetailsPage extends Component {
217219
const isFailedAddContactMethod = Boolean(lodashGet(loginData, 'errorFields.addedLogin'));
218220

219221
return (
220-
<ScreenWrapper>
222+
<ScreenWrapper onEntryTransitionEnd={() => this.validateCodeFormRef.current && this.validateCodeFormRef.current.focus()}>
221223
<HeaderWithBackButton
222224
title={formattedContactMethod}
223225
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS)}
@@ -251,6 +253,7 @@ class ContactMethodDetailsPage extends Component {
251253
contactMethod={contactMethod}
252254
hasMagicCodeBeenSent={hasMagicCodeBeenSent}
253255
loginList={this.props.loginList}
256+
ref={this.validateCodeFormRef}
254257
/>
255258
</View>
256259
)}

src/pages/settings/Profile/Contacts/ValidateCodeForm/BaseValidateCodeForm.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useCallback, useState, useEffect, useRef} from 'react';
1+
import React, {useCallback, useState, useEffect, useRef, useImperativeHandle} from 'react';
22
import {View} from 'react-native';
33
import PropTypes from 'prop-types';
44
import _ from 'underscore';
@@ -16,7 +16,6 @@ import * as User from '../../../../../libs/actions/User';
1616
import Button from '../../../../../components/Button';
1717
import DotIndicatorMessage from '../../../../../components/DotIndicatorMessage';
1818
import * as Session from '../../../../../libs/actions/Session';
19-
import shouldDelayFocus from '../../../../../libs/shouldDelayFocus';
2019
import Text from '../../../../../components/Text';
2120
import {withNetwork} from '../../../../../components/OnyxProvider';
2221
import PressableWithFeedback from '../../../../../components/Pressable/PressableWithFeedback';
@@ -51,6 +50,9 @@ const propTypes = {
5150
pendingFields: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)),
5251
}).isRequired,
5352

53+
/** Forwarded inner ref */
54+
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
55+
5456
/* Onyx Props */
5557

5658
/** The details about the account that the user is signing in with */
@@ -65,6 +67,7 @@ const propTypes = {
6567

6668
const defaultProps = {
6769
account: {},
70+
innerRef: () => {},
6871
};
6972

7073
function BaseValidateCodeForm(props) {
@@ -74,6 +77,15 @@ function BaseValidateCodeForm(props) {
7477
const inputValidateCodeRef = useRef();
7578
const validateLoginError = ErrorUtils.getEarliestErrorField(loginData, 'validateLogin');
7679

80+
useImperativeHandle(props.innerRef, () => ({
81+
focus() {
82+
if (!inputValidateCodeRef.current) {
83+
return;
84+
}
85+
inputValidateCodeRef.current.focus();
86+
},
87+
}));
88+
7789
useEffect(() => {
7890
if (!props.hasMagicCodeBeenSent) {
7991
return;
@@ -138,8 +150,7 @@ function BaseValidateCodeForm(props) {
138150
errorText={formError.validateCode ? props.translate(formError.validateCode) : ErrorUtils.getLatestErrorMessage(props.account)}
139151
hasError={!_.isEmpty(validateLoginError)}
140152
onFulfill={validateAndSubmitForm}
141-
autoFocus
142-
shouldDelayFocus={shouldDelayFocus}
153+
autoFocus={false}
143154
/>
144155
<OfflineWithFeedback
145156
pendingAction={lodashGet(loginData, 'pendingFields.validateCodeSent', null)}

src/pages/settings/Profile/Contacts/ValidateCodeForm/index.android.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import React from 'react';
1+
import React, {forwardRef} from 'react';
22
import BaseValidateCodeForm from './BaseValidateCodeForm';
33

4-
function ValidateCodeForm(props) {
5-
return (
6-
<BaseValidateCodeForm
7-
autoComplete="sms-otp"
8-
// eslint-disable-next-line react/jsx-props-no-spreading
9-
{...props}
10-
/>
11-
);
12-
}
4+
const ValidateCodeForm = forwardRef((props, ref) => (
5+
<BaseValidateCodeForm
6+
autoComplete="sms-otp"
7+
// eslint-disable-next-line react/jsx-props-no-spreading
8+
{...props}
9+
innerRef={ref}
10+
/>
11+
));
1312

1413
ValidateCodeForm.displayName = 'ValidateCodeForm';
1514

src/pages/settings/Profile/Contacts/ValidateCodeForm/index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import React from 'react';
1+
import React, {forwardRef} from 'react';
22
import BaseValidateCodeForm from './BaseValidateCodeForm';
33

4-
function ValidateCodeForm(props) {
5-
return (
6-
<BaseValidateCodeForm
7-
autoComplete="one-time-code"
8-
// eslint-disable-next-line react/jsx-props-no-spreading
9-
{...props}
10-
/>
11-
);
12-
}
4+
const ValidateCodeForm = forwardRef((props, ref) => (
5+
<BaseValidateCodeForm
6+
autoComplete="one-time-code"
7+
// eslint-disable-next-line react/jsx-props-no-spreading
8+
{...props}
9+
innerRef={ref}
10+
/>
11+
));
1312

1413
ValidateCodeForm.displayName = 'ValidateCodeForm';
1514

src/pages/settings/Security/TwoFactorAuth/TwoFactorAuthForm/BaseTwoFactorAuthForm.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ function BaseTwoFactorAuthForm(props) {
7575
validateAndSubmitForm() {
7676
validateAndSubmitForm();
7777
},
78+
focus() {
79+
if (!inputRef.current) {
80+
return;
81+
}
82+
inputRef.current.focus();
83+
},
7884
}));
7985

8086
return (
@@ -89,6 +95,7 @@ function BaseTwoFactorAuthForm(props) {
8995
onFulfill={validateAndSubmitForm}
9096
errorText={formError.twoFactorAuthCode ? props.translate(formError.twoFactorAuthCode) : ErrorUtils.getLatestErrorMessage(props.account)}
9197
ref={inputRef}
98+
autoFocus={false}
9299
/>
93100
);
94101
}

src/pages/settings/Security/TwoFactorAuth/VerifyPage.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ function VerifyPage(props) {
9393
}
9494

9595
return (
96-
<ScreenWrapper shouldShowOfflineIndicator={false}>
96+
<ScreenWrapper
97+
shouldShowOfflineIndicator={false}
98+
onEntryTransitionEnd={() => formRef.current && formRef.current.focus()}
99+
>
97100
<HeaderWithBackButton
98101
title={props.translate('twoFactorAuth.headerTitle')}
99102
stepCounter={{

0 commit comments

Comments
 (0)