From dbc139ddc598818e8db2e06be9ca4e9f10af9374 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:06:11 +0700 Subject: [PATCH 01/19] Add getNavigationActionForReplay to replay an overlay back at the root Clicking outside the RHP sends a back with no target from the RHP's root-level route, which the root navigator handles by closing the RHP. When a discard prompt sends it again after Discard, react-navigation hands it to the innermost navigator instead, which only closes the top screen. The helper targets the root so the replay closes the whole RHP, and leaves other actions unchanged. --- .../helpers/getNavigationActionForReplay.ts | 35 +++++++++++++ .../getNavigationActionForReplayTest.ts | 49 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/libs/Navigation/helpers/getNavigationActionForReplay.ts create mode 100644 tests/unit/Navigation/getNavigationActionForReplayTest.ts diff --git a/src/libs/Navigation/helpers/getNavigationActionForReplay.ts b/src/libs/Navigation/helpers/getNavigationActionForReplay.ts new file mode 100644 index 000000000000..47481b9f03a9 --- /dev/null +++ b/src/libs/Navigation/helpers/getNavigationActionForReplay.ts @@ -0,0 +1,35 @@ +/** + * Prepares a navigation that a "Discard changes?" prompt blocked, so sending it again after Discard does what it first did. + */ +import navigationRef from '@libs/Navigation/navigationRef'; + +import CONST from '@src/CONST'; + +import type {NavigationAction} from '@react-navigation/native'; + +const BACK_ACTION_TYPES = new Set([CONST.NAVIGATION.ACTION_TYPE.GO_BACK, CONST.NAVIGATION.ACTION_TYPE.POP]); + +/** + * Makes Discard close the whole RHP when the user clicked outside it. + * + * Clicking outside the RHP sends a "go back" with no target, which the top-level navigator handles by closing the RHP. After the + * user picks Discard, that action is sent again, but this time react-navigation hands it to the innermost navigator, which only + * closes the top screen and leaves the RHP open. Targeting the top-level navigator makes it close the RHP, like the original click. + * An action with no target that came from one of the top-level navigator's own routes is how that click is recognized. Other + * actions, such as a back sent from a screen inside the RHP, were handled by that screen's own stack, which gets the replay + * too, so they're returned unchanged. The copy keeps react-navigation's hidden marker that stops the same screen from blocking it again. + */ +function getNavigationActionForReplay(action: NavigationAction): NavigationAction { + if (action.target || !action.source || !BACK_ACTION_TYPES.has(action.type)) { + return action; + } + + const rootState = navigationRef.isReady() ? navigationRef.getRootState() : undefined; + if (!rootState?.routes.some((route) => route.key === action.source)) { + return action; + } + + return {...action, target: rootState.key}; +} + +export default getNavigationActionForReplay; diff --git a/tests/unit/Navigation/getNavigationActionForReplayTest.ts b/tests/unit/Navigation/getNavigationActionForReplayTest.ts new file mode 100644 index 000000000000..12a67324b5a4 --- /dev/null +++ b/tests/unit/Navigation/getNavigationActionForReplayTest.ts @@ -0,0 +1,49 @@ +import getNavigationActionForReplay from '@libs/Navigation/helpers/getNavigationActionForReplay'; + +let mockIsNavigationReady = true; +jest.mock('@libs/Navigation/navigationRef', () => ({ + __esModule: true, + default: { + isReady: () => mockIsNavigationReady, + getRootState: () => ({key: 'root', routes: [{key: 'tab-navigator'}, {key: 'rhp-route'}]}), + }, +})); + +describe('getNavigationActionForReplay', () => { + beforeEach(() => { + mockIsNavigationReady = true; + }); + + it('targets the root navigator for a back without a target, sent from a root-level route', () => { + // Given the RHP overlay's GO_BACK without a target, sent from the root-level RHP route + const action = {type: 'GO_BACK', source: 'rhp-route'}; + + // When preparing it for replay through the container + const actionForReplay = getNavigationActionForReplay(action); + + // Then it targets the root, which closed the whole RHP the first time + expect(actionForReplay).toEqual({type: 'GO_BACK', source: 'rhp-route', target: 'root'}); + }); + + it.each([ + ['a targeted action', {type: 'POP', source: 'rhp-route', target: 'money-request-stack'}], + ['an action without a source', {type: 'GO_BACK'}], + ['an action that is not a back', {type: 'NAVIGATE', source: 'rhp-route'}], + ['an action sent from a nested screen', {type: 'GO_BACK', source: 'confirmation-route'}], + ])('replays %s unchanged', (_, action) => { + // Given an action the deepest navigator handles the same way on replay + // When preparing it for replay + // Then it's left as is + expect(getNavigationActionForReplay(action)).toBe(action); + }); + + it('replays the action unchanged before navigation is ready', () => { + // Given a navigation container that isn't ready + mockIsNavigationReady = false; + const action = {type: 'GO_BACK', source: 'rhp-route'}; + + // When preparing an overlay back for replay + // Then it's left as is + expect(getNavigationActionForReplay(action)).toBe(action); + }); +}); From 18489dc5174d08abb5cd80f713796eb0ed9b6e94 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:06:55 +0700 Subject: [PATCH 02/19] Close the whole RHP when Discard follows a click outside it on web After the user picks Discard, the web discard prompt sends the blocked navigation again through getNavigationActionForReplay, so a click outside the RHP closes the whole RHP instead of only the guarded screen on top. --- .../useDiscardChangesConfirmation/index.ts | 3 +- .../useDiscardChangesConfirmation.test.ts | 42 ++++++++++++++++--- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/hooks/useDiscardChangesConfirmation/index.ts b/src/hooks/useDiscardChangesConfirmation/index.ts index eb5409f63715..b0f3ef5b0753 100644 --- a/src/hooks/useDiscardChangesConfirmation/index.ts +++ b/src/hooks/useDiscardChangesConfirmation/index.ts @@ -4,6 +4,7 @@ import useBeforeRemove from '@hooks/useBeforeRemove'; import useConfirmModal from '@hooks/useConfirmModal'; import useLocalize from '@hooks/useLocalize'; +import getNavigationActionForReplay from '@libs/Navigation/helpers/getNavigationActionForReplay'; import setNavigationActionToMicrotaskQueue from '@libs/Navigation/helpers/setNavigationActionToMicrotaskQueue'; import navigationRef from '@libs/Navigation/navigationRef'; import {useRegisterTabSwitchGuard} from '@libs/Navigation/TabSwitchGuardContext'; @@ -57,7 +58,7 @@ function useDiscardChangesConfirmation({ return; } shouldNavigateBack.current = true; - navigationRef.current?.dispatch(blockedNavigationAction.current); + navigationRef.current?.dispatch(getNavigationActionForReplay(blockedNavigationAction.current)); shouldNavigateBack.current = false; }; diff --git a/tests/unit/hooks/useDiscardChangesConfirmation.test.ts b/tests/unit/hooks/useDiscardChangesConfirmation.test.ts index 7d457f80d2fe..cf99febcea7e 100644 --- a/tests/unit/hooks/useDiscardChangesConfirmation.test.ts +++ b/tests/unit/hooks/useDiscardChangesConfirmation.test.ts @@ -3,8 +3,14 @@ import {act, renderHook} from '@testing-library/react-native'; import type {DiscardChangesConfirmation} from '@hooks/useDiscardChangesConfirmation/types'; import type UseDiscardChangesConfirmationOptions from '@hooks/useDiscardChangesConfirmation/types'; +type MockNavigationAction = { + type: string; + source?: string; + target?: string; +}; + type MockBeforeRemoveEvent = { - data: {action: {type: string}}; + data: {action: MockNavigationAction}; defaultPrevented: boolean; preventDefault: () => void; }; @@ -61,6 +67,8 @@ jest.mock('@libs/Navigation/navigationRef', () => ({ get current() { return {dispatch: mockNavigationDispatch, goBack: jest.fn()}; }, + isReady: () => true, + getRootState: () => ({key: 'root', routes: [{key: 'rhp-route'}]}), }, })); @@ -75,9 +83,9 @@ const dispatchPopstate = () => { }); }; -const createBeforeRemoveEvent = (type: string): MockBeforeRemoveEvent => { +const createBeforeRemoveEvent = (type: string, action: Omit = {}): MockBeforeRemoveEvent => { const event: MockBeforeRemoveEvent = { - data: {action: {type}}, + data: {action: {type, ...action}}, defaultPrevented: false, preventDefault: () => { event.defaultPrevented = true; @@ -86,8 +94,8 @@ const createBeforeRemoveEvent = (type: string): MockBeforeRemoveEvent => { return event; }; -const invokeBeforeRemove = (type: string): MockBeforeRemoveEvent => { - const event = createBeforeRemoveEvent(type); +const invokeBeforeRemove = (type: string, action?: Omit): MockBeforeRemoveEvent => { + const event = createBeforeRemoveEvent(type, action); act(() => { mockBeforeRemoveCallback?.(event); }); @@ -313,6 +321,30 @@ describe('useDiscardChangesConfirmation (web)', () => { expect(afterSave.defaultPrevented).toBe(true); expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); }); + + it('replays a click outside the RHP at the root navigator on Discard, so the whole RHP closes', async () => { + // Given a dirty screen stacked in the RHP + renderDiscardHook(() => true); + + // When the user clicks outside the RHP, which sends a GO_BACK without a target from the root-level RHP route, and picks Discard + invokeBeforeRemove('GO_BACK', {source: 'rhp-route'}); + await resolveModalWith('CONFIRM'); + + // Then the replay targets the root, which closes the whole RHP instead of only the top screen + expect(mockNavigationDispatch).toHaveBeenCalledWith({type: 'GO_BACK', source: 'rhp-route', target: 'root'}); + }); + + it('replays other blocked navigations unchanged on Discard', async () => { + // Given a dirty screen + renderDiscardHook(() => true); + + // When a targeted pop, such as a header back, is blocked and the user picks Discard + invokeBeforeRemove('POP', {source: 'some-screen', target: 'rhp-stack'}); + await resolveModalWith('CONFIRM'); + + // Then it is sent again as it was, since the navigator it targets handles it the same way + expect(mockNavigationDispatch).toHaveBeenCalledWith({type: 'POP', source: 'some-screen', target: 'rhp-stack'}); + }); }); describe('popstate guards', () => { From 7fade8c0d1837f560642295467d46e0e42de0d87 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:07:41 +0700 Subject: [PATCH 03/19] Close the whole RHP when Discard follows a click outside it on native The native discard prompt sends the blocked navigation again through getNavigationActionForReplay, both after Discard and when it lets the navigation through without asking, so a click outside the RHP closes the whole RHP instead of only the guarded screen on top. --- .../index.native.ts | 5 +-- ...seDiscardChangesConfirmationNative.test.ts | 35 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/hooks/useDiscardChangesConfirmation/index.native.ts b/src/hooks/useDiscardChangesConfirmation/index.native.ts index 28e199ccc11c..70a84f95726e 100644 --- a/src/hooks/useDiscardChangesConfirmation/index.native.ts +++ b/src/hooks/useDiscardChangesConfirmation/index.native.ts @@ -3,6 +3,7 @@ import {ModalActions} from '@components/Modal/Global/ModalContext'; import useConfirmModal from '@hooks/useConfirmModal'; import useLocalize from '@hooks/useLocalize'; +import getNavigationActionForReplay from '@libs/Navigation/helpers/getNavigationActionForReplay'; import navigationRef from '@libs/Navigation/navigationRef'; import {useRegisterTabSwitchGuard} from '@libs/Navigation/TabSwitchGuardContext'; @@ -61,7 +62,7 @@ function useDiscardChangesConfirmation({ const confirmNavigation = () => { isReplayingBlockedNavigation.current = true; if (blockedNavigationAction.current) { - navigationRef.current?.dispatch(blockedNavigationAction.current); + navigationRef.current?.dispatch(getNavigationActionForReplay(blockedNavigationAction.current)); blockedNavigationAction.current = undefined; } else { navigationRef.current?.goBack(); @@ -77,7 +78,7 @@ function useDiscardChangesConfirmation({ usePreventRemove(shouldPreventRemove, ({data}: {data: {action: NavigationAction}}) => { // The action delivered here carries react-navigation's visited-routes marker, so re-dispatching it skips this screen's prevention if (isReplayingBlockedNavigation.current || !hasUnsavedChanges()) { - navigationRef.current?.dispatch(data.action); + navigationRef.current?.dispatch(getNavigationActionForReplay(data.action)); return; } if (isDiscardModalOpen.current) { diff --git a/tests/unit/hooks/useDiscardChangesConfirmationNative.test.ts b/tests/unit/hooks/useDiscardChangesConfirmationNative.test.ts index 58b15963c8b3..a11deb8b0d38 100644 --- a/tests/unit/hooks/useDiscardChangesConfirmationNative.test.ts +++ b/tests/unit/hooks/useDiscardChangesConfirmationNative.test.ts @@ -7,7 +7,9 @@ import type {HardwareBackPressEvent} from '@src/types/utils/ReactNativeCompat'; import {BackHandler} from 'react-native'; -type MockBeforeRemoveEvent = {data: {action: {type: string}}}; +type MockNavigationAction = {type: string; source?: string; target?: string}; + +type MockBeforeRemoveEvent = {data: {action: MockNavigationAction}}; const mockHardwareBackPressEvent: HardwareBackPressEvent = {type: 'hardwareBackPress', timeStamp: 0}; @@ -56,6 +58,8 @@ jest.mock('@libs/Navigation/navigationRef', () => ({ get current() { return {dispatch: mockNavigationDispatch, goBack: mockNavigationGoBack}; }, + isReady: () => true, + getRootState: () => ({key: 'root', routes: [{key: 'rhp-route'}]}), }, })); @@ -243,9 +247,9 @@ describe('useDiscardChangesConfirmation (native)', () => { }); describe('usePreventRemove (removal case: header back, in-app pop)', () => { - const invokeBeforeRemove = (type: string) => { + const invokeBeforeRemove = (type: string, action: Omit = {}) => { act(() => { - mockPreventRemoveCallback?.({data: {action: {type}}}); + mockPreventRemoveCallback?.({data: {action: {type, ...action}}}); }); }; @@ -291,5 +295,30 @@ describe('useDiscardChangesConfirmation (native)', () => { expect(mockNavigationDispatch).not.toHaveBeenCalled(); expect(mockNavigationGoBack).toHaveBeenCalledTimes(1); }); + + it('replays a click outside the RHP at the root navigator on Discard, so the whole RHP closes', async () => { + // Given a dirty screen stacked in the RHP + renderDiscardHook(() => true); + + // When the user clicks outside the RHP, which sends a GO_BACK without a target from the root-level RHP route, and picks Discard + invokeBeforeRemove('GO_BACK', {source: 'rhp-route'}); + await resolveModalWith('CONFIRM'); + + // Then the replay targets the root, which closes the whole RHP instead of only the top screen + expect(mockNavigationDispatch).toHaveBeenCalledWith({type: 'GO_BACK', source: 'rhp-route', target: 'root'}); + }); + + it('lets a click outside the RHP close the whole RHP while the prompt is suppressed', () => { + // Given a dirty screen, so prevention stays armed, whose prompt is suppressed during a save + const {result} = renderDiscardHook(() => true); + act(() => result.current.suppressDiscardPrompt()); + + // When the user clicks outside the RHP + invokeBeforeRemove('GO_BACK', {source: 'rhp-route'}); + + // Then the close goes through at the root, so the whole RHP closes instead of only the top screen + expect(mockShowConfirmModal).not.toHaveBeenCalled(); + expect(mockNavigationDispatch).toHaveBeenCalledWith({type: 'GO_BACK', source: 'rhp-route', target: 'root'}); + }); }); }); From 2967c571280435169b157efe20a7a228ae05cc7b Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:19:18 +0700 Subject: [PATCH 04/19] Add MoneyRequestTestNavigator to drive create expense steps through real navigators It renders a tab navigator, the RHP and the MoneyRequest stack with real navigators, so tests can open steps, click outside the RHP and read where the user ends up. Each step renders the component a test passes in. --- tests/utils/MoneyRequestTestNavigator.tsx | 163 ++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 tests/utils/MoneyRequestTestNavigator.tsx diff --git a/tests/utils/MoneyRequestTestNavigator.tsx b/tests/utils/MoneyRequestTestNavigator.tsx new file mode 100644 index 000000000000..a1328d72bede --- /dev/null +++ b/tests/utils/MoneyRequestTestNavigator.tsx @@ -0,0 +1,163 @@ +/** + * Renders a real navigation tree with a tab navigator, the right modal navigator (RHP) and the create expense + * (MoneyRequest) stack inside it, so tests can drive steps of a create expense flow through the real navigators. + * Each step renders the component a test passes in, or nothing. + */ + +import {act, render} from '@testing-library/react-native'; + +import createRootStackNavigator from '@libs/Navigation/AppNavigator/createRootStackNavigator'; +import createSplitNavigator from '@libs/Navigation/AppNavigator/createSplitNavigator'; +import navigationRef from '@libs/Navigation/navigationRef'; +import type {MoneyRequestNavigatorParamList, RightModalNavigatorParamList, TabNavigatorParamList} from '@libs/Navigation/types'; + +import createPlatformStackNavigator from '@navigation/PlatformStackNavigation/createPlatformStackNavigator'; + +import CONST from '@src/CONST'; +import NAVIGATORS from '@src/NAVIGATORS'; +import SCREENS from '@src/SCREENS'; + +import type {NavigationProp, ParamListBase} from '@react-navigation/native'; +import type {TupleToUnion} from 'type-fest'; + +import {createBottomTabNavigator} from '@react-navigation/bottom-tabs'; +import {NavigationContainer, useNavigation} from '@react-navigation/native'; +import React, {useEffect} from 'react'; + +/** The create expense steps this tree registers */ +const MONEY_REQUEST_TEST_SCREENS = [SCREENS.MONEY_REQUEST.CREATE, SCREENS.MONEY_REQUEST.DYNAMIC_STEP_DESTINATION, SCREENS.MONEY_REQUEST.DYNAMIC_STEP_TIME] as const; + +type MoneyRequestTestScreen = TupleToUnion; + +type RenderMoneyRequestNavigationParams = { + /** The steps open in the create expense stack, from the first one to the one on top */ + openSteps: MoneyRequestTestScreen[]; + + /** The component each step renders. Steps left out render nothing. */ + stepComponents?: Partial>; + + /** The params every open step gets */ + stepParams?: Record; +}; + +const RootStack = createRootStackNavigator(); +const TabNav = createBottomTabNavigator(); +const RightModalNavigatorStack = createSplitNavigator(); +const MoneyRequestStack = createPlatformStackNavigator(); + +function EmptyScreen() { + return null; +} + +/** The navigation of the RHP route in the root stack. Clicking outside the RHP calls `goBack` on it. */ +let rhpNavigation: NavigationProp | undefined; + +let stepComponentsForRender: Partial> = {}; + +function TestMoneyRequestNavigator() { + return ( + + {MONEY_REQUEST_TEST_SCREENS.map((screenName) => ( + stepComponentsForRender[screenName] ?? EmptyScreen} + /> + ))} + + ); +} + +function TestRightModalNavigator() { + const navigation = useNavigation(); + + useEffect(() => { + rhpNavigation = navigation; + return () => { + rhpNavigation = undefined; + }; + }, [navigation]); + + return ( + + + + ); +} + +function TestTabNavigator() { + return ( + + + + ); +} + +function renderMoneyRequestNavigation({openSteps, stepComponents = {}, stepParams = {}}: RenderMoneyRequestNavigationParams) { + stepComponentsForRender = stepComponents; + return render( + ({name, params: stepParams}))}, + }, + ], + }, + }, + ], + }} + > + + + + + , + ); +} + +/** Sends the same action as clicking the overlay outside the RHP: a back without a target from the RHP route */ +function clickOutsideRHP() { + act(() => { + rhpNavigation?.goBack(); + }); +} + +function getRootRouteNames() { + return navigationRef.current?.getRootState().routes.map((route) => route.name); +} + +function getMoneyRequestStepNames() { + return navigationRef.current + ?.getRootState() + .routes.find((route) => route.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR) + ?.state?.routes.at(0) + ?.state?.routes.map((route) => route.name); +} + +export {renderMoneyRequestNavigation, clickOutsideRHP, getRootRouteNames, getMoneyRequestStepNames}; +export type {MoneyRequestTestScreen}; From abea713e3562b6b0f83de29b2967f5a77890b9bd Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:19:19 +0700 Subject: [PATCH 05/19] Test with real navigators that Discard after a click outside closes the whole RHP Runs the web and native discard prompts on a step stacked in the RHP. Clicking outside or pressing Esc and picking Discard closes the whole RHP, Cancel keeps everything open, and going back from the step and picking Discard only leaves that step. --- .../navigation/DiscardChangesReplayTests.tsx | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 tests/navigation/DiscardChangesReplayTests.tsx diff --git a/tests/navigation/DiscardChangesReplayTests.tsx b/tests/navigation/DiscardChangesReplayTests.tsx new file mode 100644 index 000000000000..60ca4e2d2b64 --- /dev/null +++ b/tests/navigation/DiscardChangesReplayTests.tsx @@ -0,0 +1,136 @@ +import {act, waitFor} from '@testing-library/react-native'; + +import type {DiscardChangesConfirmation} from '@hooks/useDiscardChangesConfirmation/types'; +import type UseDiscardChangesConfirmationOptions from '@hooks/useDiscardChangesConfirmation/types'; +import useResponsiveLayout from '@hooks/useResponsiveLayout'; + +import getIsNarrowLayout from '@libs/getIsNarrowLayout'; +import Navigation from '@libs/Navigation/Navigation'; +import navigationRef from '@libs/Navigation/navigationRef'; + +import CONST from '@src/CONST'; +import NAVIGATORS from '@src/NAVIGATORS'; +import SCREENS from '@src/SCREENS'; + +import type * as ReactNavigation from '@react-navigation/native'; + +import type * as MockUseConfirmModalUtil from '../utils/mockUseConfirmModal'; + +import {MockModalActions, mockShowConfirmModal, resetMockConfirmModal, resolveShowConfirmModal} from '../utils/mockUseConfirmModal'; +import {clickOutsideRHP, getMoneyRequestStepNames, getRootRouteNames, renderMoneyRequestNavigation} from '../utils/MoneyRequestTestNavigator'; + +// The shared mock stubs `usePreventRemove`, which the native hook relies on, so this test uses the real module +jest.mock('@react-navigation/native', () => jest.requireActual('@react-navigation/native')); +jest.mock('@hooks/useResponsiveLayout', () => jest.fn()); +jest.mock('@libs/getIsNarrowLayout', () => jest.fn()); + +jest.mock('@hooks/useConfirmModal', () => { + const {default: mockUseConfirmModal} = jest.requireActual('../utils/mockUseConfirmModal'); + return mockUseConfirmModal; +}); + +jest.mock('@components/Modal/Global/ModalContext', () => { + const {createMockModalContextModule} = jest.requireActual('../utils/mockUseConfirmModal'); + return createMockModalContextModule(); +}); + +jest.mock('@hooks/useLocalize', () => ({ + __esModule: true, + default: () => ({translate: (key: string) => key}), +})); + +type DiscardHookModule = {default: (options: UseDiscardChangesConfirmationOptions) => DiscardChangesConfirmation}; + +// Jest resolves platform extensions native-first, so each implementation is loaded explicitly to run the same scenarios on both +const useWebDiscardChangesConfirmation = jest.requireActual('@hooks/useDiscardChangesConfirmation/index.ts').default; +const useNativeDiscardChangesConfirmation = jest.requireActual('@hooks/useDiscardChangesConfirmation/index.native.ts').default; + +describe.each([ + ['web', useWebDiscardChangesConfirmation], + ['native', useNativeDiscardChangesConfirmation], +])('Discarding changes on a step stacked in the RHP (%s)', (_, useDiscardChangesConfirmation) => { + function DirtyStep() { + useDiscardChangesConfirmation({getHasUnsavedChanges: () => true}); + return null; + } + + function renderDirtyStepOverFirstStep() { + renderMoneyRequestNavigation({ + openSteps: [SCREENS.MONEY_REQUEST.CREATE, SCREENS.MONEY_REQUEST.DYNAMIC_STEP_DESTINATION], + stepComponents: {[SCREENS.MONEY_REQUEST.DYNAMIC_STEP_DESTINATION]: DirtyStep}, + stepParams: {action: CONST.IOU.ACTION.CREATE, iouType: CONST.IOU.TYPE.CREATE, transactionID: '1', reportID: '1'}, + }); + } + + beforeEach(() => { + resetMockConfirmModal(); + jest.mocked(getIsNarrowLayout).mockReturnValue(true); + jest.mocked(useResponsiveLayout).mockReturnValue({...CONST.NAVIGATION_TESTS.DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE, shouldUseNarrowLayout: true}); + }); + + it('closes the whole RHP when the user clicks outside it and picks Discard', async () => { + // Given a step with unsaved changes stacked on another step in the RHP + renderDirtyStepOverFirstStep(); + + // When the user clicks outside the RHP + clickOutsideRHP(); + + // Then the RHP stays open while the user is asked + expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); + expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR, NAVIGATORS.RIGHT_MODAL_NAVIGATOR]); + + // When the user picks Discard + resolveShowConfirmModal({action: MockModalActions.CONFIRM}); + + // Then the whole RHP closes, as the click asked, instead of only the step on top. The web hook sends the navigation + // again on the next animation frame, so wait for it. + await waitFor(() => expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR])); + }); + + it('keeps the RHP and the step open when the user cancels', async () => { + // Given a step with unsaved changes stacked on another step in the RHP + renderDirtyStepOverFirstStep(); + + // When the user clicks outside the RHP and cancels the prompt + clickOutsideRHP(); + expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); + await act(async () => { + resolveShowConfirmModal({action: MockModalActions.CLOSE}); + }); + + // Then nothing closes, so the changes are still there + expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR, NAVIGATORS.RIGHT_MODAL_NAVIGATOR]); + expect(getMoneyRequestStepNames()).toEqual([SCREENS.MONEY_REQUEST.CREATE, SCREENS.MONEY_REQUEST.DYNAMIC_STEP_DESTINATION]); + }); + + it('closes the whole RHP when the user presses Esc and picks Discard', async () => { + // Given a step with unsaved changes stacked on another step in the RHP + renderDirtyStepOverFirstStep(); + + // When the user presses Esc, which dismisses the modal, and picks Discard + act(() => { + Navigation.dismissModal(); + }); + expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); + resolveShowConfirmModal({action: MockModalActions.CONFIRM}); + + // Then the whole RHP closes + await waitFor(() => expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR])); + }); + + it('only leaves the step when the user goes back from it and picks Discard', async () => { + // Given a step with unsaved changes stacked on another step in the RHP + renderDirtyStepOverFirstStep(); + + // When the user goes back from the step, which the step's own stack handles, and picks Discard + act(() => { + navigationRef.current?.goBack(); + }); + expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); + resolveShowConfirmModal({action: MockModalActions.CONFIRM}); + + // Then only the step closes and the RHP stays on the step below it + await waitFor(() => expect(getMoneyRequestStepNames()).toEqual([SCREENS.MONEY_REQUEST.CREATE])); + expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR, NAVIGATORS.RIGHT_MODAL_NAVIGATOR]); + }); +}); From 08a8cea6ffb37bbb005f3b772be941bfd77821b4 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:26:44 +0700 Subject: [PATCH 06/19] Fix the RHP navigation type and drop an unused export in MoneyRequestTestNavigator useNavigation returns a navigation whose getState can be undefined, which isn't a NavigationProp, so typecheck failed. The harness only calls goBack on it, so the type now only asks for that. The step name type was exported but never imported, which knip flagged. --- tests/utils/MoneyRequestTestNavigator.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/utils/MoneyRequestTestNavigator.tsx b/tests/utils/MoneyRequestTestNavigator.tsx index a1328d72bede..76529baebadd 100644 --- a/tests/utils/MoneyRequestTestNavigator.tsx +++ b/tests/utils/MoneyRequestTestNavigator.tsx @@ -17,7 +17,6 @@ import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; import SCREENS from '@src/SCREENS'; -import type {NavigationProp, ParamListBase} from '@react-navigation/native'; import type {TupleToUnion} from 'type-fest'; import {createBottomTabNavigator} from '@react-navigation/bottom-tabs'; @@ -50,7 +49,7 @@ function EmptyScreen() { } /** The navigation of the RHP route in the root stack. Clicking outside the RHP calls `goBack` on it. */ -let rhpNavigation: NavigationProp | undefined; +let rhpNavigation: Pick, 'goBack'> | undefined; let stepComponentsForRender: Partial> = {}; @@ -160,4 +159,3 @@ function getMoneyRequestStepNames() { } export {renderMoneyRequestNavigation, clickOutsideRHP, getRootRouteNames, getMoneyRequestStepNames}; -export type {MoneyRequestTestScreen}; From fe8801813f9a01a1a6347396a86d794bda54d452 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:38:20 +0700 Subject: [PATCH 07/19] Type the RHP navigation as a NavigationProp goBack in MoneyRequestTestNavigator ReturnType of useNavigation resolves its generic to unknown, so picking goBack from it failed typecheck. The RHP navigation is now typed as the goBack of NavigationProp, which the value useNavigation returns satisfies. --- tests/utils/MoneyRequestTestNavigator.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/utils/MoneyRequestTestNavigator.tsx b/tests/utils/MoneyRequestTestNavigator.tsx index 76529baebadd..b91cd62d9518 100644 --- a/tests/utils/MoneyRequestTestNavigator.tsx +++ b/tests/utils/MoneyRequestTestNavigator.tsx @@ -17,6 +17,7 @@ import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; import SCREENS from '@src/SCREENS'; +import type {NavigationProp, ParamListBase} from '@react-navigation/native'; import type {TupleToUnion} from 'type-fest'; import {createBottomTabNavigator} from '@react-navigation/bottom-tabs'; @@ -49,7 +50,7 @@ function EmptyScreen() { } /** The navigation of the RHP route in the root stack. Clicking outside the RHP calls `goBack` on it. */ -let rhpNavigation: Pick, 'goBack'> | undefined; +let rhpNavigation: Pick, 'goBack'> | undefined; let stepComponentsForRender: Partial> = {}; From c818714b924cb74f3c75a82a98b8b5d6b62aa52d Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:43:21 +0700 Subject: [PATCH 08/19] Close the whole RHP on Discard only for pages that opt in on web Only the create expense flow should close as a whole. Other pages, like the agent avatar page, keep going back to the previous page after a click outside and Discard, as on main. --- .../useDiscardChangesConfirmation/index.ts | 4 +++- .../useDiscardChangesConfirmation/types.ts | 5 ++++ .../useDiscardChangesConfirmation.test.ts | 23 +++++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/hooks/useDiscardChangesConfirmation/index.ts b/src/hooks/useDiscardChangesConfirmation/index.ts index b0f3ef5b0753..829b6fbb0543 100644 --- a/src/hooks/useDiscardChangesConfirmation/index.ts +++ b/src/hooks/useDiscardChangesConfirmation/index.ts @@ -33,6 +33,7 @@ function useDiscardChangesConfirmation({ onVisibilityChange, onConfirm, onTabSwitchDiscard, + shouldCloseRHPOnDiscard = false, }: UseDiscardChangesConfirmationOptions): DiscardChangesConfirmation { const route = useRoute(); const {translate} = useLocalize(); @@ -58,7 +59,8 @@ function useDiscardChangesConfirmation({ return; } shouldNavigateBack.current = true; - navigationRef.current?.dispatch(getNavigationActionForReplay(blockedNavigationAction.current)); + const blockedAction = blockedNavigationAction.current; + navigationRef.current?.dispatch(shouldCloseRHPOnDiscard ? getNavigationActionForReplay(blockedAction) : blockedAction); shouldNavigateBack.current = false; }; diff --git a/src/hooks/useDiscardChangesConfirmation/types.ts b/src/hooks/useDiscardChangesConfirmation/types.ts index fa42ee5b0d13..b862fc2ec7e9 100644 --- a/src/hooks/useDiscardChangesConfirmation/types.ts +++ b/src/hooks/useDiscardChangesConfirmation/types.ts @@ -5,6 +5,11 @@ type UseDiscardChangesConfirmationOptions = { onVisibilityChange?: (visible: boolean) => void; onConfirm?: () => void | Promise; onTabSwitchDiscard?: () => void | Promise; + /** + * When the user clicks outside the RHP and picks Discard, close the whole RHP, as that click does when there's nothing to discard, + * instead of only this page. Going back still closes only this page. The create expense steps set it, so discarding closes the whole flow. + */ + shouldCloseRHPOnDiscard?: boolean; }; type DiscardChangesConfirmation = { diff --git a/tests/unit/hooks/useDiscardChangesConfirmation.test.ts b/tests/unit/hooks/useDiscardChangesConfirmation.test.ts index cf99febcea7e..963871d689b7 100644 --- a/tests/unit/hooks/useDiscardChangesConfirmation.test.ts +++ b/tests/unit/hooks/useDiscardChangesConfirmation.test.ts @@ -106,7 +106,8 @@ describe('useDiscardChangesConfirmation (web)', () => { let historyGoSpy: jest.SpyInstance; let resolveModal: ((result: {action: string}) => void) | undefined; - const renderDiscardHook = (getHasUnsavedChanges: () => boolean) => renderHook(() => useDiscardChangesConfirmation({getHasUnsavedChanges})); + const renderDiscardHook = (getHasUnsavedChanges: () => boolean, shouldCloseRHPOnDiscard?: boolean) => + renderHook(() => useDiscardChangesConfirmation({getHasUnsavedChanges, shouldCloseRHPOnDiscard})); const resolveModalWith = async (action: string) => { await act(async () => { @@ -323,8 +324,8 @@ describe('useDiscardChangesConfirmation (web)', () => { }); it('replays a click outside the RHP at the root navigator on Discard, so the whole RHP closes', async () => { - // Given a dirty screen stacked in the RHP - renderDiscardHook(() => true); + // Given a dirty create expense step stacked in the RHP, which closes the whole RHP on Discard + renderDiscardHook(() => true, true); // When the user clicks outside the RHP, which sends a GO_BACK without a target from the root-level RHP route, and picks Discard invokeBeforeRemove('GO_BACK', {source: 'rhp-route'}); @@ -334,10 +335,22 @@ describe('useDiscardChangesConfirmation (web)', () => { expect(mockNavigationDispatch).toHaveBeenCalledWith({type: 'GO_BACK', source: 'rhp-route', target: 'root'}); }); - it('replays other blocked navigations unchanged on Discard', async () => { - // Given a dirty screen + it('replays a click outside the RHP unchanged on Discard by default, so only the screen closes', async () => { + // Given a dirty screen stacked in the RHP outside the create expense flow, such as the agent avatar page renderDiscardHook(() => true); + // When the user clicks outside the RHP and picks Discard + invokeBeforeRemove('GO_BACK', {source: 'rhp-route'}); + await resolveModalWith('CONFIRM'); + + // Then it is sent again as it was, so the user goes back to the previous page as before + expect(mockNavigationDispatch).toHaveBeenCalledWith({type: 'GO_BACK', source: 'rhp-route'}); + }); + + it('replays other blocked navigations unchanged on Discard', async () => { + // Given a dirty create expense step, which closes the whole RHP on Discard after a click outside + renderDiscardHook(() => true, true); + // When a targeted pop, such as a header back, is blocked and the user picks Discard invokeBeforeRemove('POP', {source: 'some-screen', target: 'rhp-stack'}); await resolveModalWith('CONFIRM'); From bc3dbbb23625a4f9751c4befd9ef93fedd94298e Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:43:45 +0700 Subject: [PATCH 09/19] Close the whole RHP on Discard only for pages that opt in on native Same as on web: other pages keep going back to the previous page after a click outside and Discard. --- .../index.native.ts | 7 ++++-- ...seDiscardChangesConfirmationNative.test.ts | 23 +++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/hooks/useDiscardChangesConfirmation/index.native.ts b/src/hooks/useDiscardChangesConfirmation/index.native.ts index 70a84f95726e..aaff5b5bd1d7 100644 --- a/src/hooks/useDiscardChangesConfirmation/index.native.ts +++ b/src/hooks/useDiscardChangesConfirmation/index.native.ts @@ -25,6 +25,7 @@ function useDiscardChangesConfirmation({ onVisibilityChange, onConfirm, onTabSwitchDiscard, + shouldCloseRHPOnDiscard = false, }: UseDiscardChangesConfirmationOptions): DiscardChangesConfirmation { const route = useRoute(); const {translate} = useLocalize(); @@ -47,6 +48,8 @@ function useDiscardChangesConfirmation({ useRegisterTabSwitchGuard(route.name, hasUnsavedChanges, onTabSwitchDiscard, onCancel); + const getActionToReplay = (action: NavigationAction) => (shouldCloseRHPOnDiscard ? getNavigationActionForReplay(action) : action); + const showDiscardModal = (blockedAction?: NavigationAction) => { blockedNavigationAction.current = blockedAction; isDiscardModalOpen.current = true; @@ -62,7 +65,7 @@ function useDiscardChangesConfirmation({ const confirmNavigation = () => { isReplayingBlockedNavigation.current = true; if (blockedNavigationAction.current) { - navigationRef.current?.dispatch(getNavigationActionForReplay(blockedNavigationAction.current)); + navigationRef.current?.dispatch(getActionToReplay(blockedNavigationAction.current)); blockedNavigationAction.current = undefined; } else { navigationRef.current?.goBack(); @@ -78,7 +81,7 @@ function useDiscardChangesConfirmation({ usePreventRemove(shouldPreventRemove, ({data}: {data: {action: NavigationAction}}) => { // The action delivered here carries react-navigation's visited-routes marker, so re-dispatching it skips this screen's prevention if (isReplayingBlockedNavigation.current || !hasUnsavedChanges()) { - navigationRef.current?.dispatch(getNavigationActionForReplay(data.action)); + navigationRef.current?.dispatch(getActionToReplay(data.action)); return; } if (isDiscardModalOpen.current) { diff --git a/tests/unit/hooks/useDiscardChangesConfirmationNative.test.ts b/tests/unit/hooks/useDiscardChangesConfirmationNative.test.ts index a11deb8b0d38..9d99080755b9 100644 --- a/tests/unit/hooks/useDiscardChangesConfirmationNative.test.ts +++ b/tests/unit/hooks/useDiscardChangesConfirmationNative.test.ts @@ -73,7 +73,8 @@ describe('useDiscardChangesConfirmation (native)', () => { const removeSubscription = jest.fn(); let resolveModal: ((result: {action: string}) => void) | undefined; - const renderDiscardHook = (getHasUnsavedChanges: () => boolean) => renderHook(() => useDiscardChangesConfirmation({getHasUnsavedChanges})); + const renderDiscardHook = (getHasUnsavedChanges: () => boolean, shouldCloseRHPOnDiscard?: boolean) => + renderHook(() => useDiscardChangesConfirmation({getHasUnsavedChanges, shouldCloseRHPOnDiscard})); const pressHardwareBack = (): boolean | null | undefined => { let consumed: boolean | null | undefined; @@ -297,8 +298,8 @@ describe('useDiscardChangesConfirmation (native)', () => { }); it('replays a click outside the RHP at the root navigator on Discard, so the whole RHP closes', async () => { - // Given a dirty screen stacked in the RHP - renderDiscardHook(() => true); + // Given a dirty create expense step stacked in the RHP, which closes the whole RHP on Discard + renderDiscardHook(() => true, true); // When the user clicks outside the RHP, which sends a GO_BACK without a target from the root-level RHP route, and picks Discard invokeBeforeRemove('GO_BACK', {source: 'rhp-route'}); @@ -308,9 +309,21 @@ describe('useDiscardChangesConfirmation (native)', () => { expect(mockNavigationDispatch).toHaveBeenCalledWith({type: 'GO_BACK', source: 'rhp-route', target: 'root'}); }); + it('replays a click outside the RHP unchanged on Discard by default, so only the screen closes', async () => { + // Given a dirty screen stacked in the RHP outside the create expense flow, such as the agent avatar page + renderDiscardHook(() => true); + + // When the user clicks outside the RHP and picks Discard + invokeBeforeRemove('GO_BACK', {source: 'rhp-route'}); + await resolveModalWith('CONFIRM'); + + // Then it is sent again as it was, so the user goes back to the previous page as before + expect(mockNavigationDispatch).toHaveBeenCalledWith({type: 'GO_BACK', source: 'rhp-route'}); + }); + it('lets a click outside the RHP close the whole RHP while the prompt is suppressed', () => { - // Given a dirty screen, so prevention stays armed, whose prompt is suppressed during a save - const {result} = renderDiscardHook(() => true); + // Given a dirty create expense step, so prevention stays armed, whose prompt is suppressed during a save + const {result} = renderDiscardHook(() => true, true); act(() => result.current.suppressDiscardPrompt()); // When the user clicks outside the RHP From d6023f609db3f3e994a0b32f17fd7ba7ccafd1a6 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:44:11 +0700 Subject: [PATCH 10/19] Test with real navigators that Discard after a click outside only closes a page outside the create expense flow --- .../navigation/DiscardChangesReplayTests.tsx | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/navigation/DiscardChangesReplayTests.tsx b/tests/navigation/DiscardChangesReplayTests.tsx index 60ca4e2d2b64..36d3d0227dc7 100644 --- a/tests/navigation/DiscardChangesReplayTests.tsx +++ b/tests/navigation/DiscardChangesReplayTests.tsx @@ -49,15 +49,22 @@ describe.each([ ['web', useWebDiscardChangesConfirmation], ['native', useNativeDiscardChangesConfirmation], ])('Discarding changes on a step stacked in the RHP (%s)', (_, useDiscardChangesConfirmation) => { + /** A create expense step with unsaved changes, which closes the whole RHP on Discard */ function DirtyStep() { + useDiscardChangesConfirmation({getHasUnsavedChanges: () => true, shouldCloseRHPOnDiscard: true}); + return null; + } + + /** A page with unsaved changes outside the create expense flow, such as the agent avatar page */ + function DirtyPageOutsideCreateFlow() { useDiscardChangesConfirmation({getHasUnsavedChanges: () => true}); return null; } - function renderDirtyStepOverFirstStep() { + function renderDirtyStepOverFirstStep(dirtyStep = DirtyStep) { renderMoneyRequestNavigation({ openSteps: [SCREENS.MONEY_REQUEST.CREATE, SCREENS.MONEY_REQUEST.DYNAMIC_STEP_DESTINATION], - stepComponents: {[SCREENS.MONEY_REQUEST.DYNAMIC_STEP_DESTINATION]: DirtyStep}, + stepComponents: {[SCREENS.MONEY_REQUEST.DYNAMIC_STEP_DESTINATION]: dirtyStep}, stepParams: {action: CONST.IOU.ACTION.CREATE, iouType: CONST.IOU.TYPE.CREATE, transactionID: '1', reportID: '1'}, }); } @@ -87,6 +94,20 @@ describe.each([ await waitFor(() => expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR])); }); + it('only closes a page outside the create expense flow when the user clicks outside the RHP and picks Discard', async () => { + // Given a page with unsaved changes outside the create expense flow, stacked on another page in the RHP + renderDirtyStepOverFirstStep(DirtyPageOutsideCreateFlow); + + // When the user clicks outside the RHP and picks Discard + clickOutsideRHP(); + expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); + resolveShowConfirmModal({action: MockModalActions.CONFIRM}); + + // Then only that page closes and the RHP stays on the page below it, as before + await waitFor(() => expect(getMoneyRequestStepNames()).toEqual([SCREENS.MONEY_REQUEST.CREATE])); + expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR, NAVIGATORS.RIGHT_MODAL_NAVIGATOR]); + }); + it('keeps the RHP and the step open when the user cancels', async () => { // Given a step with unsaved changes stacked on another step in the RHP renderDirtyStepOverFirstStep(); From e7431437e6259d8fe04922e004ca7d0b656eabb8 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:44:21 +0700 Subject: [PATCH 11/19] Close the whole RHP on Discard from the Amount step while creating an expense --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 36fe61d399c8..59e1bafa3f8c 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -150,6 +150,7 @@ function IOURequestStepAmount({ const baselineAmount = transactionAmount ? convertToFrontendAmountAsString(transactionAmount, decimals) : ''; const {suppressDiscardPrompt} = useDiscardChangesConfirmation({ + shouldCloseRHPOnDiscard: isCreateAction, getHasUnsavedChanges: () => getAmountHasUnsavedChanges({ typedAmount: typedAmount ?? baselineAmount, From c6f852ccc27fdac48d9a621665b5246403384def Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:44:22 +0700 Subject: [PATCH 12/19] Close the whole RHP on Discard from the Hours step while creating an expense --- src/pages/iou/request/step/IOURequestStepHours.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/iou/request/step/IOURequestStepHours.tsx b/src/pages/iou/request/step/IOURequestStepHours.tsx index dfeac1033e17..ba5f8d2c6226 100644 --- a/src/pages/iou/request/step/IOURequestStepHours.tsx +++ b/src/pages/iou/request/step/IOURequestStepHours.tsx @@ -96,6 +96,7 @@ function IOURequestStepHours({ }, [selectedTab, committedCount]); const {suppressDiscardPrompt} = useDiscardChangesConfirmation({ + shouldCloseRHPOnDiscard: action === CONST.IOU.ACTION.CREATE, getHasUnsavedChanges: () => getStringFieldHasUnsavedChanges(typedCount, committedCount, isEmbeddedInStartPage), onCancel: () => { focusTimeoutRef.current = setTimeout(() => textInputRef.current?.focus(), CONST.ANIMATED_TRANSITION); From 3609e24e0bb933469d1c00921c72a6c498bfda18 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:44:23 +0700 Subject: [PATCH 13/19] Close the whole RHP on Discard from the map Distance step while creating an expense --- src/pages/iou/request/step/DynamicIOURequestStepDistance.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/iou/request/step/DynamicIOURequestStepDistance.tsx b/src/pages/iou/request/step/DynamicIOURequestStepDistance.tsx index 1da17f4cfbe5..acb59eb107da 100644 --- a/src/pages/iou/request/step/DynamicIOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/DynamicIOURequestStepDistance.tsx @@ -228,6 +228,7 @@ function DynamicIOURequestStepDistance({ }; const {suppressDiscardPrompt} = useDiscardChangesConfirmation({ + shouldCloseRHPOnDiscard: action === CONST.IOU.ACTION.CREATE, getHasUnsavedChanges: () => { const typedManualDistance = manualDistanceValue ? roundToTwoDecimalPlaces(parseFloat(manualDistanceValue)) : undefined; const manualDistanceChanged = manualDistanceValue !== undefined && typedManualDistance !== currentDistance; From 8f382536edfc66555548dc7c825d51ebed53d7a1 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:44:23 +0700 Subject: [PATCH 14/19] Close the whole RHP on Discard from the Map distance tab while creating an expense --- src/pages/iou/request/step/IOURequestStepDistanceMap.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/iou/request/step/IOURequestStepDistanceMap.tsx b/src/pages/iou/request/step/IOURequestStepDistanceMap.tsx index 12ff396036c9..853bcc4f2f5d 100644 --- a/src/pages/iou/request/step/IOURequestStepDistanceMap.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistanceMap.tsx @@ -136,6 +136,7 @@ function IOURequestStepDistanceMap({ const shouldShowNotFoundPage = useShowNotFoundPageInIOUStep(action, iouType, reportActionID, report, transaction); const {suppressDiscardPrompt} = useDiscardChangesConfirmation({ + shouldCloseRHPOnDiscard: action === CONST.IOU.ACTION.CREATE, getHasUnsavedChanges: () => doesMoneyRequestDraftHaveUserInput(transaction), }); From 48e7b0a56e1f667ffb4ca61eac5b2acc7465a32e Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:44:24 +0700 Subject: [PATCH 15/19] Close the whole RHP on Discard from the manual Distance step while creating an expense --- .../iou/request/step/DynamicIOURequestStepDistanceManual.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/iou/request/step/DynamicIOURequestStepDistanceManual.tsx b/src/pages/iou/request/step/DynamicIOURequestStepDistanceManual.tsx index 0f1e725b318c..fac5c41b8f02 100644 --- a/src/pages/iou/request/step/DynamicIOURequestStepDistanceManual.tsx +++ b/src/pages/iou/request/step/DynamicIOURequestStepDistanceManual.tsx @@ -185,6 +185,7 @@ function DynamicIOURequestStepDistanceManual({ const [typedDistance, setTypedDistance] = useState(committedDistance); const {suppressDiscardPrompt} = useDiscardChangesConfirmation({ + shouldCloseRHPOnDiscard: action === CONST.IOU.ACTION.CREATE, getHasUnsavedChanges: () => getStringFieldHasUnsavedChanges(typedDistance, committedDistance, isCreatingNewRequest), onCancel: () => { focusTimeoutRef.current = setTimeout(() => textInput.current?.focus(), CONST.ANIMATED_TRANSITION); From bf1afdc12fcb42cef4fc366cb5138b117d8a2f12 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:44:24 +0700 Subject: [PATCH 16/19] Close the whole RHP on Discard from the Odometer step while creating an expense --- src/pages/iou/request/step/IOURequestStepDistanceOdometer.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/iou/request/step/IOURequestStepDistanceOdometer.tsx b/src/pages/iou/request/step/IOURequestStepDistanceOdometer.tsx index bb685d1e1a35..b6580a1814e9 100644 --- a/src/pages/iou/request/step/IOURequestStepDistanceOdometer.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistanceOdometer.tsx @@ -597,6 +597,7 @@ function IOURequestStepDistanceOdometer({ }; useDiscardChangesConfirmation({ + shouldCloseRHPOnDiscard: action === CONST.IOU.ACTION.CREATE, getHasUnsavedChanges, onCancel: restoreLastInputFocus, onConfirm: isEditingConfirmation From fdd6ee60698cf7d9570f547906f4edbf86df9592 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:05:22 +0700 Subject: [PATCH 17/19] Simplify comments --- .../Navigation/helpers/getNavigationActionForReplay.ts | 9 +++------ tests/navigation/DiscardChangesReplayTests.tsx | 3 +-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/libs/Navigation/helpers/getNavigationActionForReplay.ts b/src/libs/Navigation/helpers/getNavigationActionForReplay.ts index 47481b9f03a9..c9c984b8fb3f 100644 --- a/src/libs/Navigation/helpers/getNavigationActionForReplay.ts +++ b/src/libs/Navigation/helpers/getNavigationActionForReplay.ts @@ -12,12 +12,9 @@ const BACK_ACTION_TYPES = new Set([CONST.NAVIGATION.ACTION_TYPE.GO_BACK, /** * Makes Discard close the whole RHP when the user clicked outside it. * - * Clicking outside the RHP sends a "go back" with no target, which the top-level navigator handles by closing the RHP. After the - * user picks Discard, that action is sent again, but this time react-navigation hands it to the innermost navigator, which only - * closes the top screen and leaves the RHP open. Targeting the top-level navigator makes it close the RHP, like the original click. - * An action with no target that came from one of the top-level navigator's own routes is how that click is recognized. Other - * actions, such as a back sent from a screen inside the RHP, were handled by that screen's own stack, which gets the replay - * too, so they're returned unchanged. The copy keeps react-navigation's hidden marker that stops the same screen from blocking it again. + * That click sends a "go back" with no target from the RHP's route, so the root closes the RHP. Sent again as is, the innermost + * navigator handles it and only closes the top screen, so it's aimed at the root instead. Other actions are returned unchanged. + * The copy keeps react-navigation's marker that stops the same screen from blocking it again. */ function getNavigationActionForReplay(action: NavigationAction): NavigationAction { if (action.target || !action.source || !BACK_ACTION_TYPES.has(action.type)) { diff --git a/tests/navigation/DiscardChangesReplayTests.tsx b/tests/navigation/DiscardChangesReplayTests.tsx index 36d3d0227dc7..cdd6f0c771ed 100644 --- a/tests/navigation/DiscardChangesReplayTests.tsx +++ b/tests/navigation/DiscardChangesReplayTests.tsx @@ -89,8 +89,7 @@ describe.each([ // When the user picks Discard resolveShowConfirmModal({action: MockModalActions.CONFIRM}); - // Then the whole RHP closes, as the click asked, instead of only the step on top. The web hook sends the navigation - // again on the next animation frame, so wait for it. + // Then the whole RHP closes, as the click asked, instead of only the step on top. await waitFor(() => expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR])); }); From 9877c800e5c674db95e70ac62dbea423aa2d0dbe Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:29:34 +0700 Subject: [PATCH 18/19] Simplify comments --- src/hooks/useDiscardChangesConfirmation/types.ts | 4 ++-- src/libs/Navigation/helpers/getNavigationActionForReplay.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hooks/useDiscardChangesConfirmation/types.ts b/src/hooks/useDiscardChangesConfirmation/types.ts index b862fc2ec7e9..9bfec1261711 100644 --- a/src/hooks/useDiscardChangesConfirmation/types.ts +++ b/src/hooks/useDiscardChangesConfirmation/types.ts @@ -6,8 +6,8 @@ type UseDiscardChangesConfirmationOptions = { onConfirm?: () => void | Promise; onTabSwitchDiscard?: () => void | Promise; /** - * When the user clicks outside the RHP and picks Discard, close the whole RHP, as that click does when there's nothing to discard, - * instead of only this page. Going back still closes only this page. The create expense steps set it, so discarding closes the whole flow. + * When the user clicks outside the RHP and picks Discard, close the whole RHP instead of only the focused page. + * Going back still closes only this page. */ shouldCloseRHPOnDiscard?: boolean; }; diff --git a/src/libs/Navigation/helpers/getNavigationActionForReplay.ts b/src/libs/Navigation/helpers/getNavigationActionForReplay.ts index c9c984b8fb3f..773db928058a 100644 --- a/src/libs/Navigation/helpers/getNavigationActionForReplay.ts +++ b/src/libs/Navigation/helpers/getNavigationActionForReplay.ts @@ -1,5 +1,5 @@ /** - * Prepares a navigation that a "Discard changes?" prompt blocked, so sending it again after Discard does what it first did. + * Prepares a navigation that a "Discard changes?" prompt blocked, so sending it again after Discard resumes it. */ import navigationRef from '@libs/Navigation/navigationRef'; From 12eb6b5efdca81fdfd3c672e03b1ee0192216c22 Mon Sep 17 00:00:00 2001 From: gijoe0295 <153004152+gijoe0295@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:02:55 +0700 Subject: [PATCH 19/19] Remove shouldCloseRHPOnDiscard from Amount step --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 59e1bafa3f8c..36fe61d399c8 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -150,7 +150,6 @@ function IOURequestStepAmount({ const baselineAmount = transactionAmount ? convertToFrontendAmountAsString(transactionAmount, decimals) : ''; const {suppressDiscardPrompt} = useDiscardChangesConfirmation({ - shouldCloseRHPOnDiscard: isCreateAction, getHasUnsavedChanges: () => getAmountHasUnsavedChanges({ typedAmount: typedAmount ?? baselineAmount,