diff --git a/src/components/Search/SearchPageHeader/SearchFiltersBar.tsx b/src/components/Search/SearchPageHeader/SearchFiltersBar.tsx index 17488ec90cae..67af7caae5e0 100644 --- a/src/components/Search/SearchPageHeader/SearchFiltersBar.tsx +++ b/src/components/Search/SearchPageHeader/SearchFiltersBar.tsx @@ -4,9 +4,7 @@ import {emailSelector} from '@selectors/Session'; import {searchResultsErrorSelector} from '@selectors/Snapshot'; import React, {useCallback, useContext, useEffect, useMemo, useRef} from 'react'; import type {ReactNode} from 'react'; -import {View} from 'react-native'; -// eslint-disable-next-line no-restricted-imports -import type {ScrollView as RNScrollView} from 'react-native'; +import {FlatList, View} from 'react-native'; import Button from '@components/Button'; import ButtonWithDropdownMenu from '@components/ButtonWithDropdownMenu'; import type {DropdownOption} from '@components/ButtonWithDropdownMenu/types'; @@ -15,7 +13,6 @@ import {KYCWallContext} from '@components/KYCWall/KYCWallContext'; import type {PaymentMethodType} from '@components/KYCWall/types'; import {LockedAccountContext} from '@components/LockedAccountModalProvider'; import {usePersonalDetails} from '@components/OnyxListItemProvider'; -import ScrollView from '@components/ScrollView'; import type {SearchDateValues} from '@components/Search/FilterComponents/DatePresetFilterBase'; import DateSelectPopup from '@components/Search/FilterDropdowns/DateSelectPopup'; import type {PopoverComponentProps} from '@components/Search/FilterDropdowns/DropdownButton'; @@ -48,6 +45,7 @@ import {getActiveAdminWorkspaces, getAllTaxRates, isPaidGroupPolicy} from '@libs import {isExpenseReport} from '@libs/ReportUtils'; import {buildFilterFormValuesFromQuery, buildQueryStringFromFilterFormValues, isFilterSupported, isSearchDatePreset} from '@libs/SearchQueryUtils'; import {getDatePresets, getFeedOptions, getGroupByOptions, getGroupCurrencyOptions, getHasOptions, getStatusOptions, getTypeOptions, getWithdrawalTypeOptions} from '@libs/SearchUIUtils'; +import shouldAdjustScroll from '@libs/shouldAdjustScroll'; import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -86,7 +84,7 @@ function SearchFiltersBar({ latestBankItems, }: SearchFiltersBarProps) { const isFocused = useIsFocused(); - const scrollRef = useRef(null); + const scrollRef = useRef>(null); const currentPolicy = usePolicy(currentSelectedPolicyID); const [isUserValidated] = useOnyx(ONYXKEYS.ACCOUNT, {selector: isUserValidatedSelector, canBeMissing: true}); // type, groupBy and status values are not guaranteed to respect the ts type as they come from user input @@ -737,6 +735,52 @@ function SearchFiltersBar({ }); }, [filterFormValues, filters, typeFiltersKeys]); + const adjustScroll = useCallback((info: {distanceFromEnd: number}) => { + // Workaround for a known React Native bug on Android (https://github.com/facebook/react-native/issues/27504): + // When the FlatList is scrolled to the end and the last item is deleted, a blank space is left behind. + // To fix this, we detect when onEndReached is triggered due to an item deletion, + // and programmatically scroll to the end to fill the space. + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + if (!shouldAdjustScroll || info.distanceFromEnd > 0) { + return; + } + scrollRef.current?.scrollToEnd(); + }, []); + + const renderFilterItem = useCallback( + // eslint-disable-next-line react/no-unused-prop-types + ({item}: {item: FilterItem}) => ( + + ), + [], + ); + + const filterButtonText = useMemo( + () => translate('search.filtersHeader') + (hiddenSelectedFilters.length > 0 ? ` (${hiddenSelectedFilters.length})` : ''), + [translate, hiddenSelectedFilters.length], + ); + + const renderListFooter = useCallback( + () => ( +