Skip to content

Commit

Permalink
Merge pull request #51318 from truph01/fix/50663
Browse files Browse the repository at this point in the history
fix: Submit button hang in middle page
  • Loading branch information
thienlnam authored Nov 13, 2024
2 parents e6b4167 + 9c4e662 commit 4df913e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/components/MoneyRequestConfirmationList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useFocusEffect, useIsFocused} from '@react-navigation/native';
import lodashIsEqual from 'lodash/isEqual';
import React, {memo, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import React, {memo, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import {InteractionManager, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
Expand Down Expand Up @@ -46,6 +46,7 @@ import type {SectionListDataType} from './SelectionList/types';
import UserListItem from './SelectionList/UserListItem';
import SettlementButton from './SettlementButton';
import Text from './Text';
import {KeyboardStateContext} from './withKeyboardState';

type MoneyRequestConfirmationListProps = {
/** Callback to inform parent modal of success */
Expand Down Expand Up @@ -194,7 +195,7 @@ function MoneyRequestConfirmationList({
const styles = useThemeStyles();
const {translate, toLocaleDigit} = useLocalize();
const currentUserPersonalDetails = useCurrentUserPersonalDetails();

const {isKeyboardShown, isWindowHeightReducedByKeyboard} = useContext(KeyboardStateContext);
const isTypeRequest = iouType === CONST.IOU.TYPE.SUBMIT;
const isTypeSplit = iouType === CONST.IOU.TYPE.SPLIT;
const isTypeSend = iouType === CONST.IOU.TYPE.PAY;
Expand Down Expand Up @@ -824,7 +825,7 @@ function MoneyRequestConfirmationList({
}, [routeError, isTypeSplit, shouldShowReadOnlySplits, debouncedFormError, formError, translate]);

const footerContent = useMemo(() => {
if (isReadOnly) {
if (isReadOnly || isKeyboardShown || isWindowHeightReducedByKeyboard) {
return;
}

Expand Down Expand Up @@ -876,7 +877,20 @@ function MoneyRequestConfirmationList({
{button}
</>
);
}, [isReadOnly, iouType, confirm, bankAccountRoute, iouCurrencyCode, policyID, splitOrRequestOptions, styles.ph1, styles.mb2, errorMessage]);
}, [
isReadOnly,
iouType,
confirm,
bankAccountRoute,
iouCurrencyCode,
policyID,
splitOrRequestOptions,
styles.ph1,
styles.mb2,
errorMessage,
isKeyboardShown,
isWindowHeightReducedByKeyboard,
]);

const listFooterContent = (
<MoneyRequestConfirmationListFooter
Expand Down
8 changes: 7 additions & 1 deletion src/components/withKeyboardState.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {ComponentType, ForwardedRef, ReactElement, RefAttributes} from 'react';
import React, {createContext, forwardRef, useEffect, useMemo, useState} from 'react';
import {Keyboard} from 'react-native';
import useIsWindowHeightReducedByKeyboard from '@hooks/useIsWindowHeightReducedByKeyboard';
import getComponentDisplayName from '@libs/getComponentDisplayName';
import type ChildrenProps from '@src/types/utils/ChildrenProps';

Expand All @@ -10,6 +11,9 @@ type KeyboardStateContextValue = {

/** Height of the keyboard in pixels */
keyboardHeight: number;

/** Whether window height is smaller than usual due to the keyboard being open */
isWindowHeightReducedByKeyboard?: boolean;
};

const KeyboardStateContext = createContext<KeyboardStateContextValue>({
Expand All @@ -19,6 +23,7 @@ const KeyboardStateContext = createContext<KeyboardStateContextValue>({

function KeyboardStateProvider({children}: ChildrenProps): ReactElement | null {
const [keyboardHeight, setKeyboardHeight] = useState(0);
const isWindowHeightReducedByKeyboard = useIsWindowHeightReducedByKeyboard();

useEffect(() => {
const keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', (e) => {
Expand All @@ -38,8 +43,9 @@ function KeyboardStateProvider({children}: ChildrenProps): ReactElement | null {
() => ({
keyboardHeight,
isKeyboardShown: keyboardHeight !== 0,
isWindowHeightReducedByKeyboard,
}),
[keyboardHeight],
[keyboardHeight, isWindowHeightReducedByKeyboard],
);
return <KeyboardStateContext.Provider value={contextValue}>{children}</KeyboardStateContext.Provider>;
}
Expand Down
37 changes: 37 additions & 0 deletions src/hooks/useIsWindowHeightReducedByKeyboard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {useCallback, useEffect, useState} from 'react';
import usePrevious from '@hooks/usePrevious';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useWindowDimensions from '@hooks/useWindowDimensions';

const useIsWindowHeightReducedByKeyboard = () => {
const [isWindowHeightReducedByKeyboard, setIsWindowHeightReducedByKeyboard] = useState(false);
const {windowHeight} = useWindowDimensions();
const prevWindowHeight = usePrevious(windowHeight);
const {shouldUseNarrowLayout} = useResponsiveLayout();
const toggleKeyboardOnSmallScreens = useCallback(
(isKBOpen: boolean) => {
if (!shouldUseNarrowLayout) {
return;
}
setIsWindowHeightReducedByKeyboard(isKBOpen);
},
[shouldUseNarrowLayout],
);
useEffect(() => {
// Use window height changes to toggle the keyboard. To maintain keyboard state
// on all platforms we also use focus/blur events. So we need to make sure here
// that we avoid redundant keyboard toggling.
// Minus 100px is needed to make sure that when the internet connection is
// disabled in android chrome and a small 'No internet connection' text box appears,
// we do not take it as a sign to open the keyboard
if (!isWindowHeightReducedByKeyboard && windowHeight < prevWindowHeight - 100) {
toggleKeyboardOnSmallScreens(true);
} else if (isWindowHeightReducedByKeyboard && windowHeight > prevWindowHeight) {
toggleKeyboardOnSmallScreens(false);
}
}, [isWindowHeightReducedByKeyboard, prevWindowHeight, toggleKeyboardOnSmallScreens, windowHeight]);

return isWindowHeightReducedByKeyboard;
};

export default useIsWindowHeightReducedByKeyboard;

0 comments on commit 4df913e

Please sign in to comment.