diff --git a/src/components/SelectionList/BaseSelectionList.tsx b/src/components/SelectionList/BaseSelectionList.tsx index 86a9d8adbd76..b38e7fbbe0db 100644 --- a/src/components/SelectionList/BaseSelectionList.tsx +++ b/src/components/SelectionList/BaseSelectionList.tsx @@ -361,6 +361,9 @@ function BaseSelectionList( */ const selectRow = useCallback( (item: TItem, indexToFocus?: number) => { + if (!isFocused) { + return; + } // In single-selection lists we don't care about updating the focused index, because the list is closed after selecting an item if (canSelectMultiple) { if (sections.length > 1) { @@ -401,6 +404,7 @@ function BaseSelectionList( setFocusedIndex, onSelectRow, shouldPreventDefaultFocusOnSelectRow, + isFocused, ], ); diff --git a/src/pages/WorkspaceSwitcherPage/index.tsx b/src/pages/WorkspaceSwitcherPage/index.tsx index 493c0c6e2994..ba39c2789c38 100644 --- a/src/pages/WorkspaceSwitcherPage/index.tsx +++ b/src/pages/WorkspaceSwitcherPage/index.tsx @@ -1,4 +1,3 @@ -import {useIsFocused} from '@react-navigation/native'; import React, {useCallback, useMemo} from 'react'; import {useOnyx} from 'react-native-onyx'; import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; @@ -39,7 +38,6 @@ function WorkspaceSwitcherPage() { const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState(''); const {translate} = useLocalize(); const {activeWorkspaceID, setActiveWorkspaceID} = useActiveWorkspace(); - const isFocused = useIsFocused(); const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); const [reportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS); @@ -83,9 +81,6 @@ function WorkspaceSwitcherPage() { const selectPolicy = useCallback( (policyID?: string) => { - if (!isFocused) { - return; - } const newPolicyID = policyID === activeWorkspaceID ? undefined : policyID; Navigation.goBack(); @@ -93,7 +88,7 @@ function WorkspaceSwitcherPage() { // Therefore we delay switching the workspace until after back navigation, using the InteractionManager. switchPolicyAfterInteractions(newPolicyID, () => setActiveWorkspaceID(newPolicyID)); }, - [activeWorkspaceID, setActiveWorkspaceID, isFocused], + [activeWorkspaceID, setActiveWorkspaceID], ); const usersWorkspaces = useMemo(() => { diff --git a/tests/unit/BaseSelectionListTest.tsx b/tests/unit/BaseSelectionListTest.tsx index 673ea50b58d6..a1083c840f6f 100644 --- a/tests/unit/BaseSelectionListTest.tsx +++ b/tests/unit/BaseSelectionListTest.tsx @@ -1,3 +1,4 @@ +import * as NativeNavigation from '@react-navigation/native'; import {fireEvent, render, screen} from '@testing-library/react-native'; import BaseSelectionList from '@components/SelectionList/BaseSelectionList'; import RadioListItem from '@components/SelectionList/RadioListItem'; @@ -41,7 +42,15 @@ describe('BaseSelectionList', () => { ); } + it('should not trigger item press if screen is not focused', () => { + (NativeNavigation.useIsFocused as jest.Mock).mockReturnValue(false); + render(); + fireEvent.press(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}1`)); + expect(onSelectRowMock).toHaveBeenCalledTimes(0); + }); + it('should handle item press correctly', () => { + (NativeNavigation.useIsFocused as jest.Mock).mockReturnValue(true); render(); fireEvent.press(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}1`)); expect(onSelectRowMock).toHaveBeenCalledWith({ @@ -55,6 +64,7 @@ describe('BaseSelectionList', () => { ...section, isSelected: section.keyForList === '2', })); + (NativeNavigation.useIsFocused as jest.Mock).mockReturnValue(true); const {rerender} = render(); expect(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}1`)).toBeSelected(); rerender();