11import React , { useCallback , useMemo , useState } from 'react' ;
22import { View } from 'react-native' ;
33import Button from '@components/Button' ;
4- import SelectionList from '@components/SelectionListWithSections ' ;
5- import SingleSelectListItem from '@components/SelectionListWithSections /SingleSelectListItem' ;
6- import type { ListItem } from '@components/SelectionListWithSections /types' ;
4+ import SelectionList from '@components/SelectionList ' ;
5+ import SingleSelectListItem from '@components/SelectionList/ListItem /SingleSelectListItem' ;
6+ import type { ListItem } from '@components/SelectionList /types' ;
77import Text from '@components/Text' ;
88import useDebouncedState from '@hooks/useDebouncedState' ;
99import useLocalize from '@hooks/useLocalize' ;
@@ -48,55 +48,37 @@ function SingleSelectPopup<T extends string>({label, value, items, closeOverlay,
4848 const [ selectedItem , setSelectedItem ] = useState ( value ) ;
4949 const [ searchTerm , debouncedSearchTerm , setSearchTerm ] = useDebouncedState ( '' ) ;
5050
51- const { sections , noResultsFound} = useMemo ( ( ) => {
51+ const { options , noResultsFound} = useMemo ( ( ) => {
5252 // If the selection is searchable, we push the initially selected item into its own section and display it at the top
5353 if ( isSearchable ) {
54- const initiallySelectedItemSection = value ?. text . toLowerCase ( ) . includes ( debouncedSearchTerm ?. toLowerCase ( ) )
54+ const initiallySelectedOption = value ?. text . toLowerCase ( ) . includes ( debouncedSearchTerm ?. toLowerCase ( ) )
5555 ? [ { text : value . text , keyForList : value . value , isSelected : selectedItem ?. value === value . value } ]
5656 : [ ] ;
57- const remainingItemsSection = items
57+ const remainingOptions = items
5858 . filter ( ( item ) => item ?. value !== value ?. value && item ?. text ?. toLowerCase ( ) . includes ( debouncedSearchTerm ?. toLowerCase ( ) ) )
5959 . map ( ( item ) => ( {
6060 text : item . text ,
6161 keyForList : item . value ,
6262 isSelected : selectedItem ?. value === item . value ,
6363 } ) ) ;
64- const isEmpty = ! initiallySelectedItemSection . length && ! remainingItemsSection . length ;
64+ const allOptions = [ ...initiallySelectedOption , ...remainingOptions ] ;
65+ const isEmpty = allOptions . length === 0 ;
6566 return {
66- sections : isEmpty
67- ? [ ]
68- : [
69- {
70- data : initiallySelectedItemSection ,
71- shouldShow : initiallySelectedItemSection . length > 0 ,
72- indexOffset : 0 ,
73- } ,
74- {
75- data : remainingItemsSection ,
76- shouldShow : remainingItemsSection . length > 0 ,
77- indexOffset : initiallySelectedItemSection . length ,
78- } ,
79- ] ,
67+ options : allOptions ,
8068 noResultsFound : isEmpty ,
8169 } ;
8270 }
8371
8472 return {
85- sections : [
86- {
87- data : items . map ( ( item ) => ( {
88- text : item . text ,
89- keyForList : item . value ,
90- isSelected : item . value === selectedItem ?. value ,
91- } ) ) ,
92- } ,
93- ] ,
73+ options : items . map ( ( item ) => ( {
74+ text : item . text ,
75+ keyForList : item . value ,
76+ isSelected : item . value === selectedItem ?. value ,
77+ } ) ) ,
9478 noResultsFound : false ,
9579 } ;
9680 } , [ isSearchable , items , value , selectedItem , debouncedSearchTerm ] ) ;
9781
98- const dataLength = useMemo ( ( ) => sections . flatMap ( ( section ) => section . data ) . length , [ sections ] ) ;
99-
10082 const updateSelectedItem = useCallback (
10183 ( item : ListItem ) => {
10284 const newItem = items . find ( ( i ) => i . value === item . keyForList ) ?? null ;
@@ -115,22 +97,29 @@ function SingleSelectPopup<T extends string>({label, value, items, closeOverlay,
11597 closeOverlay ( ) ;
11698 } , [ closeOverlay , onChange ] ) ;
11799
100+ const textInputOptions = useMemo (
101+ ( ) => ( {
102+ value : searchTerm ,
103+ label : isSearchable ? ( searchPlaceholder ?? translate ( 'common.search' ) ) : undefined ,
104+ onChangeText : setSearchTerm ,
105+ headerMessage : noResultsFound ? translate ( 'common.noResultsFound' ) : undefined ,
106+ } ) ,
107+ [ searchTerm , isSearchable , searchPlaceholder , translate , setSearchTerm , noResultsFound ] ,
108+ ) ;
109+
118110 return (
119111 < View style = { [ ! isSmallScreenWidth && styles . pv4 , styles . gap2 ] } >
120112 { isSmallScreenWidth && < Text style = { [ styles . textLabel , styles . textSupporting , styles . ph5 , styles . pv1 ] } > { label } </ Text > }
121113
122- < View style = { [ styles . getSelectionListPopoverHeight ( dataLength || 1 , windowHeight , isSearchable ?? false ) ] } >
114+ < View style = { [ styles . getSelectionListPopoverHeight ( options . length || 1 , windowHeight , isSearchable ?? false ) ] } >
123115 < SelectionList
116+ data = { options }
124117 shouldSingleExecuteRowSelect
125- sections = { sections }
126118 ListItem = { SingleSelectListItem }
127119 onSelectRow = { updateSelectedItem }
128- textInputValue = { searchTerm }
129- onChangeText = { setSearchTerm }
130- textInputLabel = { isSearchable ? ( searchPlaceholder ?? translate ( 'common.search' ) ) : undefined }
120+ textInputOptions = { textInputOptions }
131121 shouldUpdateFocusedIndex = { isSearchable }
132- initiallyFocusedOptionKey = { isSearchable ? value ?. value : undefined }
133- headerMessage = { noResultsFound ? translate ( 'common.noResultsFound' ) : undefined }
122+ initiallyFocusedItemKey = { isSearchable ? value ?. value : undefined }
134123 showLoadingPlaceholder = { ! noResultsFound }
135124 />
136125 </ View >
0 commit comments