Skip to content

Commit 41fd828

Browse files
authored
Merge pull request Expensify#63817 from nkdengineer/fix/63463
fix: not auto scroll when an error message appear
2 parents e6a731b + ec8478b commit 41fd828

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

src/components/BookTravelButton.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import HybridAppModule from '@expensify/react-native-hybrid-app';
22
import {Str} from 'expensify-common';
33
import type {ReactElement} from 'react';
4-
import React, {useCallback, useContext, useState} from 'react';
4+
import React, {useCallback, useContext, useEffect, useState} from 'react';
55
import {useOnyx} from 'react-native-onyx';
66
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
77
import useLocalize from '@hooks/useLocalize';
@@ -33,6 +33,9 @@ type BookTravelButtonProps = {
3333

3434
/** Whether to render the error message below the button */
3535
shouldRenderErrorMessageBelowButton?: boolean;
36+
37+
/** Function to set the shouldScrollToBottom state */
38+
setShouldScrollToBottom?: (shouldScrollToBottom: boolean) => void;
3639
};
3740

3841
const navigateToAcceptTerms = (domain: string, isUserValidated?: boolean) => {
@@ -45,7 +48,7 @@ const navigateToAcceptTerms = (domain: string, isUserValidated?: boolean) => {
4548
Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHOD_VERIFY_ACCOUNT.getRoute(Navigation.getActiveRoute(), ROUTES.TRAVEL_TCS.getRoute(domain)));
4649
};
4750

48-
function BookTravelButton({text, shouldRenderErrorMessageBelowButton = false}: BookTravelButtonProps) {
51+
function BookTravelButton({text, shouldRenderErrorMessageBelowButton = false, setShouldScrollToBottom}: BookTravelButtonProps) {
4952
const styles = useThemeStyles();
5053
const StyleUtils = useStyleUtils();
5154
const {translate} = useLocalize();
@@ -74,6 +77,13 @@ function BookTravelButton({text, shouldRenderErrorMessageBelowButton = false}: B
7477
const hidePreventionModal = () => setPreventionModalVisibility(false);
7578
const hideVerificationModal = () => setVerificationModalVisibility(false);
7679

80+
useEffect(() => {
81+
if (!errorMessage) {
82+
return;
83+
}
84+
setShouldScrollToBottom?.(true);
85+
}, [errorMessage, setShouldScrollToBottom]);
86+
7787
const bookATrip = useCallback(() => {
7888
setErrorMessage('');
7989

src/pages/Travel/ManageTrips.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import React from 'react';
2-
import {Linking, View} from 'react-native';
1+
import React, {useEffect, useRef, useState} from 'react';
2+
// eslint-disable-next-line no-restricted-imports
3+
import type {ScrollView as RNScrollView} from 'react-native';
4+
import {InteractionManager, Linking, View} from 'react-native';
35
import BookTravelButton from '@components/BookTravelButton';
46
import Button from '@components/Button';
57
import type {FeatureListItem} from '@components/FeatureList';
@@ -28,13 +30,33 @@ function ManageTrips() {
2830
const styles = useThemeStyles();
2931
const {shouldUseNarrowLayout} = useResponsiveLayout();
3032
const {translate} = useLocalize();
33+
const [shouldScrollToBottom, setShouldScrollToBottom] = useState(false);
3134

3235
const navigateToBookTravelDemo = () => {
3336
Linking.openURL(CONST.BOOK_TRAVEL_DEMO_URL);
3437
};
3538

39+
const scrollViewRef = useRef<RNScrollView>(null);
40+
41+
const scrollToBottom = () => {
42+
InteractionManager.runAfterInteractions(() => {
43+
scrollViewRef.current?.scrollToEnd({animated: true});
44+
});
45+
};
46+
47+
useEffect(() => {
48+
if (!shouldScrollToBottom) {
49+
return;
50+
}
51+
scrollToBottom();
52+
setShouldScrollToBottom(false);
53+
}, [shouldScrollToBottom]);
54+
3655
return (
37-
<ScrollView contentContainerStyle={styles.pt3}>
56+
<ScrollView
57+
contentContainerStyle={styles.pt3}
58+
ref={scrollViewRef}
59+
>
3860
<View style={[styles.flex1, shouldUseNarrowLayout ? styles.workspaceSectionMobile : styles.workspaceSection]}>
3961
<FeatureList
4062
menuItems={tripsFeatures}
@@ -57,6 +79,7 @@ function ManageTrips() {
5779
<BookTravelButton
5880
text={translate('travel.bookTravel')}
5981
shouldRenderErrorMessageBelowButton
82+
setShouldScrollToBottom={setShouldScrollToBottom}
6083
/>
6184
</>
6285
}

0 commit comments

Comments
 (0)