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: Submit button hang in middle page #51318

Merged
merged 10 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
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 @@ -821,7 +822,7 @@ function MoneyRequestConfirmationList({
}, [routeError, isTypeSplit, shouldShowReadOnlySplits, debouncedFormError, formError, translate]);

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

Expand Down Expand Up @@ -873,7 +874,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
31 changes: 31 additions & 0 deletions src/hooks/useIsWindowHeightReducedByKeyboard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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(() => {
if (!isWindowHeightReducedByKeyboard && windowHeight < prevWindowHeight - 100) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add comments for this effect? Copying from PDFView/index.tsx should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added comment

toggleKeyboardOnSmallScreens(true);
} else if (isWindowHeightReducedByKeyboard && windowHeight > prevWindowHeight) {
toggleKeyboardOnSmallScreens(false);
}
}, [isWindowHeightReducedByKeyboard, prevWindowHeight, toggleKeyboardOnSmallScreens, windowHeight]);

return isWindowHeightReducedByKeyboard;
};

export default useIsWindowHeightReducedByKeyboard;
Loading