Skip to content
Merged
Changes from all 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
67 changes: 28 additions & 39 deletions src/components/Search/FilterDropdowns/SingleSelectPopup.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, {useCallback, useMemo, useState} from 'react';
import {View} from 'react-native';
import Button from '@components/Button';
import SelectionList from '@components/SelectionListWithSections';
import SingleSelectListItem from '@components/SelectionListWithSections/SingleSelectListItem';
import type {ListItem} from '@components/SelectionListWithSections/types';
import SelectionList from '@components/SelectionList';
import SingleSelectListItem from '@components/SelectionList/ListItem/SingleSelectListItem';
import type {ListItem} from '@components/SelectionList/types';
import Text from '@components/Text';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -48,55 +48,37 @@ function SingleSelectPopup<T extends string>({label, value, items, closeOverlay,
const [selectedItem, setSelectedItem] = useState(value);
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState('');

const {sections, noResultsFound} = useMemo(() => {
const {options, noResultsFound} = useMemo(() => {
// If the selection is searchable, we push the initially selected item into its own section and display it at the top
if (isSearchable) {
const initiallySelectedItemSection = value?.text.toLowerCase().includes(debouncedSearchTerm?.toLowerCase())
const initiallySelectedOption = value?.text.toLowerCase().includes(debouncedSearchTerm?.toLowerCase())
? [{text: value.text, keyForList: value.value, isSelected: selectedItem?.value === value.value}]
: [];
const remainingItemsSection = items
const remainingOptions = items
.filter((item) => item?.value !== value?.value && item?.text?.toLowerCase().includes(debouncedSearchTerm?.toLowerCase()))
.map((item) => ({
text: item.text,
keyForList: item.value,
isSelected: selectedItem?.value === item.value,
}));
const isEmpty = !initiallySelectedItemSection.length && !remainingItemsSection.length;
const allOptions = [...initiallySelectedOption, ...remainingOptions];
const isEmpty = allOptions.length === 0;
return {
sections: isEmpty
? []
: [
{
data: initiallySelectedItemSection,
shouldShow: initiallySelectedItemSection.length > 0,
indexOffset: 0,
},
{
data: remainingItemsSection,
shouldShow: remainingItemsSection.length > 0,
indexOffset: initiallySelectedItemSection.length,
},
],
options: allOptions,
noResultsFound: isEmpty,
};
}

return {
sections: [
{
data: items.map((item) => ({
text: item.text,
keyForList: item.value,
isSelected: item.value === selectedItem?.value,
})),
},
],
options: items.map((item) => ({
text: item.text,
keyForList: item.value,
isSelected: item.value === selectedItem?.value,
})),
noResultsFound: false,
};
}, [isSearchable, items, value, selectedItem, debouncedSearchTerm]);

const dataLength = useMemo(() => sections.flatMap((section) => section.data).length, [sections]);

const updateSelectedItem = useCallback(
(item: ListItem) => {
const newItem = items.find((i) => i.value === item.keyForList) ?? null;
Expand All @@ -115,22 +97,29 @@ function SingleSelectPopup<T extends string>({label, value, items, closeOverlay,
closeOverlay();
}, [closeOverlay, onChange]);

const textInputOptions = useMemo(
() => ({
value: searchTerm,
label: isSearchable ? (searchPlaceholder ?? translate('common.search')) : undefined,
onChangeText: setSearchTerm,
headerMessage: noResultsFound ? translate('common.noResultsFound') : undefined,
}),
[searchTerm, isSearchable, searchPlaceholder, translate, setSearchTerm, noResultsFound],
);

return (
<View style={[!isSmallScreenWidth && styles.pv4, styles.gap2]}>
{isSmallScreenWidth && <Text style={[styles.textLabel, styles.textSupporting, styles.ph5, styles.pv1]}>{label}</Text>}

<View style={[styles.getSelectionListPopoverHeight(dataLength || 1, windowHeight, isSearchable ?? false)]}>
<View style={[styles.getSelectionListPopoverHeight(options.length || 1, windowHeight, isSearchable ?? false)]}>
<SelectionList
data={options}
shouldSingleExecuteRowSelect
sections={sections}
ListItem={SingleSelectListItem}
onSelectRow={updateSelectedItem}
textInputValue={searchTerm}
onChangeText={setSearchTerm}
textInputLabel={isSearchable ? (searchPlaceholder ?? translate('common.search')) : undefined}
textInputOptions={textInputOptions}
shouldUpdateFocusedIndex={isSearchable}
initiallyFocusedOptionKey={isSearchable ? value?.value : undefined}
headerMessage={noResultsFound ? translate('common.noResultsFound') : undefined}
initiallyFocusedItemKey={isSearchable ? value?.value : undefined}
showLoadingPlaceholder={!noResultsFound}
/>
</View>
Expand Down