Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Taxes - BNP input is not auto focused in add rate page. #52810

Merged
merged 1 commit into from
Nov 25, 2024
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
28 changes: 26 additions & 2 deletions src/components/AmountPicker/AmountSelectorModal.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, {useState} from 'react';
import {useFocusEffect} from '@react-navigation/native';
import React, {useCallback, useRef, useState} from 'react';
import {View} from 'react-native';
import AmountForm from '@components/AmountForm';
import Button from '@components/Button';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import Modal from '@components/Modal';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import CONST from '@src/CONST';
Expand All @@ -16,6 +18,28 @@ function AmountSelectorModal({value, description = '', onValueSelected, isVisibl
const styles = useThemeStyles();

const [currentValue, setValue] = useState(value);
const inputRef = useRef<BaseTextInputRef | null>(null);
const focusTimeoutRef = useRef<NodeJS.Timeout | null>(null);

const inputCallbackRef = (ref: BaseTextInputRef | null) => {
inputRef.current = ref;
};

useFocusEffect(
useCallback(() => {
focusTimeoutRef.current = setTimeout(() => {
if (inputRef.current && isVisible) {
inputRef.current.focus();
}
return () => {
if (!focusTimeoutRef.current || !isVisible) {
return;
}
clearTimeout(focusTimeoutRef.current);
};
}, CONST.ANIMATED_TRANSITION);
}, [isVisible, inputRef]),
);

return (
<Modal
Expand All @@ -42,9 +66,9 @@ function AmountSelectorModal({value, description = '', onValueSelected, isVisibl
<AmountForm
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
autoFocus
value={currentValue}
onInputChange={setValue}
ref={(ref) => inputCallbackRef(ref)}
/>
<Button
success
Expand Down
Loading