diff --git a/src/components/Search/SearchAutocompleteList.tsx b/src/components/Search/SearchAutocompleteList.tsx index 6c235589eec3..4efd5294418e 100644 --- a/src/components/Search/SearchAutocompleteList.tsx +++ b/src/components/Search/SearchAutocompleteList.tsx @@ -7,6 +7,7 @@ import SelectionListWithSections from '@components/SelectionList/SelectionListWi import type {Section, SelectionListWithSectionsHandle} from '@components/SelectionList/SelectionListWithSections/types'; import useAutocompleteSuggestions from '@hooks/useAutocompleteSuggestions'; +import useBottomSafeSafeAreaPaddingStyle from '@hooks/useBottomSafeSafeAreaPaddingStyle'; import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; import useDebounce from '@hooks/useDebounce'; import useDebouncedAccessibilityAnnouncement from '@hooks/useDebouncedAccessibilityAnnouncement'; @@ -186,6 +187,10 @@ function SearchAutocompleteList({ const styles = useThemeStyles(); const {translate, localeCompare} = useLocalize(); const {shouldUseNarrowLayout} = useResponsiveLayout(); + const contentContainerStyle = useBottomSafeSafeAreaPaddingStyle({ + addOfflineIndicatorBottomSafeAreaPadding: true, + style: styles.pb2, + }); const [betas] = useOnyx(ONYXKEYS.BETAS); const feedKeysWithCards = useFeedKeysWithAssignedCards(); @@ -730,7 +735,7 @@ function SearchAutocompleteList({ style={{ containerStyle: [styles.mh100], listStyle: [styles.ph2, styles.overscrollBehaviorContain], - contentContainerStyle: styles.pb2, + contentContainerStyle, listItemWrapperStyle: [styles.pr0, styles.pl0], sectionTitleStyles: styles.mhn2, }} diff --git a/src/components/Search/SearchRouter/SearchRouterPage/index.native.tsx b/src/components/Search/SearchRouter/SearchRouterPage/index.native.tsx index 3146057cd662..60a248bd5ab3 100644 --- a/src/components/Search/SearchRouter/SearchRouterPage/index.native.tsx +++ b/src/components/Search/SearchRouter/SearchRouterPage/index.native.tsx @@ -2,11 +2,56 @@ import ScreenWrapper from '@components/ScreenWrapper'; import SearchRouter from '@components/Search/SearchRouter/SearchRouter'; import {useSearchRouterActions, useSearchRouterState} from '@components/Search/SearchRouter/SearchRouterContext'; +import useKeyboardState from '@hooks/useKeyboardState'; +import useNetwork from '@hooks/useNetwork'; +import useSafeAreaInsets from '@hooks/useSafeAreaInsets'; +import useSafeAreaPaddings from '@hooks/useSafeAreaPaddings'; +import useStyleUtils from '@hooks/useStyleUtils'; +import useThemeStyles from '@hooks/useThemeStyles'; +import useWindowDimensions from '@hooks/useWindowDimensions'; + +import getKeyboardHeight from '@libs/getKeyboardHeight'; + import React from 'react'; +import {View} from 'react-native'; + +type GetAvailableHeightParams = { + isKeyboardOverlapping: boolean; + keyboardHeight: number; + keyboardActiveHeight: number; + bottomInset: number; + windowHeight: number; + paddingTop: number; +}; + +function getAvailableHeight({isKeyboardOverlapping, keyboardHeight, keyboardActiveHeight, bottomInset, windowHeight, paddingTop}: GetAvailableHeightParams): number | undefined { + const measuredKeyboardHeight = keyboardHeight || getKeyboardHeight(keyboardActiveHeight, bottomInset); + const effectiveKeyboardHeight = isKeyboardOverlapping ? measuredKeyboardHeight : 0; + return effectiveKeyboardHeight ? Math.max(windowHeight - effectiveKeyboardHeight - paddingTop, 0) : undefined; +} function SearchRouterPage() { const {closeSearchRouter} = useSearchRouterActions(); const {isSearchRouterDisplayed} = useSearchRouterState(); + const {isKeyboardActive, keyboardHeight, keyboardActiveHeight} = useKeyboardState(); + const {isOffline} = useNetwork(); + const {paddingTop} = useSafeAreaPaddings(); + const {bottom} = useSafeAreaInsets(); + const {windowHeight} = useWindowDimensions(); + const StyleUtils = useStyleUtils(); + const styles = useThemeStyles(); + const shouldConstrainForOfflineKeyboard = isOffline && isKeyboardActive; + // Keep the router between the top safe area and the keyboard; ScreenWrapper and the + // list already handle bottom safe-area and offline-indicator spacing. + // Clamp to zero so transient dimensions during animation never produce a negative height. + const availableHeight = getAvailableHeight({ + isKeyboardOverlapping: shouldConstrainForOfflineKeyboard, + keyboardHeight, + keyboardActiveHeight, + bottomInset: bottom, + windowHeight, + paddingTop, + }); return ( - + + + ); } export default SearchRouterPage; +export {getAvailableHeight}; diff --git a/tests/unit/SearchAutocompleteListTest.tsx b/tests/unit/SearchAutocompleteListTest.tsx index 46f86ad75ab0..f0f942c1ecfb 100644 --- a/tests/unit/SearchAutocompleteListTest.tsx +++ b/tests/unit/SearchAutocompleteListTest.tsx @@ -2,12 +2,14 @@ import {act, fireEvent, render, screen, waitFor} from '@testing-library/react-na import {LocaleContextProvider} from '@components/LocaleContextProvider'; import OnyxListItemProvider from '@components/OnyxListItemProvider'; +import ScreenWrapperOfflineIndicatorContext from '@components/ScreenWrapper/ScreenWrapperOfflineIndicatorContext'; import type {SearchQueryItem} from '@components/Search/SearchList/ListItem/SearchQueryListItem'; import SearchRouter from '@components/Search/SearchRouter/SearchRouter'; import Text from '@components/Text'; import type {PrivateIsArchivedMap} from '@hooks/usePrivateIsArchivedMap'; +import {setHasRadio} from '@libs/NetworkState'; import type * as OptionsListUtilsModule from '@libs/OptionsListUtils'; import * as OptionsListUtils from '@libs/OptionsListUtils'; import {createFilteredOptionList} from '@libs/OptionsListUtils'; @@ -21,8 +23,10 @@ import ROUTES from '@src/ROUTES'; import type {PersonalDetails, Report} from '@src/types/onyx'; import type * as NativeNavigation from '@react-navigation/native'; +import type ReactNative from 'react-native'; import React from 'react'; +import {StyleSheet} from 'react-native'; import Onyx from 'react-native-onyx'; import createCollection from '../utils/collections/createCollection'; @@ -32,6 +36,25 @@ import * as TestHelper from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; +const mockFlashListContentContainerStyles: Array['contentContainerStyle']> = []; + +jest.mock('@shopify/flash-list', () => { + const RN = jest.requireActual('react-native'); + return { + FlashList: ({data, contentContainerStyle, ...props}: React.ComponentProps) => { + mockFlashListContentContainerStyles.push(contentContainerStyle); + return ( + + ); + }, + }; +}); + jest.mock('lodash/debounce', () => jest.fn((fn: Record) => { // eslint-disable-next-line no-param-reassign @@ -145,7 +168,12 @@ const mockedReports = getMockedReports(10); const mockedBetas = Object.values(CONST.BETAS); const mockedPersonalDetails = getMockedPersonalDetails(10); const EMPTY_PRIVATE_IS_ARCHIVED_MAP: PrivateIsArchivedMap = {}; -const mockedOptions = createFilteredOptionList(mockedPersonalDetails, mockedReports, undefined, EMPTY_PRIVATE_IS_ARCHIVED_MAP, undefined, {conciergeReportID: undefined, isSearching: true}); +const mockedOptions = createFilteredOptionList(mockedPersonalDetails, mockedReports, undefined, EMPTY_PRIVATE_IS_ARCHIVED_MAP, undefined, { + conciergeReportID: undefined, + isSearching: true, +}); +const OFFLINE_INDICATOR_SAFE_AREA_CONTEXT_ENABLED = {addSafeAreaPadding: true}; +const OFFLINE_INDICATOR_SAFE_AREA_CONTEXT_DISABLED = {addSafeAreaPadding: false}; const mockOnClose = jest.fn((afterClose?: () => void) => { afterClose?.(); @@ -159,14 +187,18 @@ const fakeRecentReports = [ {reportID: '103', keyForList: '103', text: 'Charlie Report', alternateText: 'charlie alt', lastMessageText: 'hey'}, ]; -function SearchRouterWrapper({isSearchRouterDisplayed}: {isSearchRouterDisplayed?: boolean}) { +function SearchRouterWrapper({isSearchRouterDisplayed, addOfflineIndicatorSafeAreaPadding}: {isSearchRouterDisplayed?: boolean; addOfflineIndicatorSafeAreaPadding?: boolean}) { return ( - - - + + + + + ); } @@ -204,6 +236,7 @@ describe('SearchAutocompleteList', () => { }); afterEach(async () => { + setHasRadio(true); await act(async () => { await Onyx.clear(); }); @@ -251,6 +284,37 @@ describe('SearchAutocompleteList', () => { }); }); + it('should add offline indicator clearance to the list content', async () => { + mockUseNavigationSuggestions.mockReturnValue([ + { + text: 'Go to Inbox', + keyForList: 'inbox', + searchItemType: CONST.SEARCH.SEARCH_ROUTER_ITEM_TYPE.NAVIGATE, + action: jest.fn(), + }, + ]); + mockFlashListContentContainerStyles.length = 0; + render(); + await flushAllUpdates(); + + const getContentPaddingBottom = () => { + // FlashList records a style per render; use the latest numeric bottom padding. + return mockFlashListContentContainerStyles + .map((contentContainerStyle) => StyleSheet.flatten(contentContainerStyle)?.paddingBottom) + .findLast((paddingBottom) => typeof paddingBottom === 'number'); + }; + const onlinePaddingBottom = getContentPaddingBottom(); + + if (typeof onlinePaddingBottom !== 'number') { + throw new Error('Expected the Search Router list to have numeric bottom padding'); + } + + act(() => setHasRadio(false)); + await flushAllUpdates(); + + expect(getContentPaddingBottom()).toBe(onlinePaddingBottom + CONST.OFFLINE_INDICATOR_HEIGHT); + }); + it('should display Recent searches section when query is empty and recent searches exist', async () => { const timestampOne = '2024-01-01T00:00:00'; const timestampTwo = '2024-01-02T00:00:00'; diff --git a/tests/unit/SearchRouterPageTest.tsx b/tests/unit/SearchRouterPageTest.tsx new file mode 100644 index 000000000000..32669020f189 --- /dev/null +++ b/tests/unit/SearchRouterPageTest.tsx @@ -0,0 +1,55 @@ +import {getAvailableHeight} from '@components/Search/SearchRouter/SearchRouterPage/index.native'; + +describe('SearchRouterPage', () => { + it('does not constrain the height when the keyboard is not overlapping', () => { + expect( + getAvailableHeight({ + isKeyboardOverlapping: false, + keyboardHeight: 300, + keyboardActiveHeight: 300, + bottomInset: 0, + windowHeight: 800, + paddingTop: 20, + }), + ).toBeUndefined(); + }); + + it('uses the settled keyboard height', () => { + expect( + getAvailableHeight({ + isKeyboardOverlapping: true, + keyboardHeight: 300, + keyboardActiveHeight: 350, + bottomInset: 0, + windowHeight: 800, + paddingTop: 20, + }), + ).toBe(480); + }); + + it('falls back to the active keyboard height during animation', () => { + expect( + getAvailableHeight({ + isKeyboardOverlapping: true, + keyboardHeight: 0, + keyboardActiveHeight: 300, + bottomInset: 0, + windowHeight: 800, + paddingTop: 20, + }), + ).toBe(480); + }); + + it('clamps transient negative heights to zero', () => { + expect( + getAvailableHeight({ + isKeyboardOverlapping: true, + keyboardHeight: 500, + keyboardActiveHeight: 500, + bottomInset: 0, + windowHeight: 400, + paddingTop: 20, + }), + ).toBe(0); + }); +});