@@ -9,12 +9,14 @@ import Text from '@components/Text';
99import { useCurrencyListState } from '@hooks/useCurrencyList' ;
1010import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails' ;
1111import useDebouncedState from '@hooks/useDebouncedState' ;
12+ import useInitialSelection from '@hooks/useInitialSelection' ;
1213import useLocalize from '@hooks/useLocalize' ;
1314import useOnyx from '@hooks/useOnyx' ;
1415import useThemeStyles from '@hooks/useThemeStyles' ;
1516import { getPlaidCountry } from '@libs/CardUtils' ;
1617import searchOptions from '@libs/searchOptions' ;
1718import type { Option } from '@libs/searchOptions' ;
19+ import moveInitialSelectionToTop from '@libs/SelectionListOrderUtils' ;
1820import StringUtils from '@libs/StringUtils' ;
1921import Navigation from '@navigation/Navigation' ;
2022import { clearAddNewPersonalCardFlow , setAddNewPersonalCardStepAndData } from '@userActions/PersonalCards' ;
@@ -41,6 +43,9 @@ function SelectCountryStep({disableAutoFocus}: {disableAutoFocus?: boolean}) {
4143 } ;
4244
4345 const [ currentCountry , setCurrentCountry ] = useState < string | undefined > ( getCountry ) ;
46+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
47+ const initialSelectedValue = useInitialSelection ( currentCountry || undefined , { resetOnFocus : true } ) ;
48+ const initialSelectedValues = initialSelectedValue ? [ initialSelectedValue ] : [ ] ;
4449 const [ hasError , setHasError ] = useState ( false ) ;
4550 const isUS = currentCountry === CONST . COUNTRY . US ;
4651
@@ -87,7 +92,9 @@ function SelectCountryStep({disableAutoFocus}: {disableAutoFocus?: boolean}) {
8792
8893 const countries = getCountries ( ) ;
8994
90- const searchResults = searchOptions ( debouncedSearchValue , countries ) ;
95+ const orderedCountries = moveInitialSelectionToTop ( countries , initialSelectedValues ) ;
96+ const filteredCountries = searchOptions ( debouncedSearchValue , debouncedSearchValue ? countries : orderedCountries ) ;
97+ const searchResults = filteredCountries . map ( ( country ) => ( { ...country , isSelected : currentCountry === country . value } ) ) ;
9198 const headerMessage = debouncedSearchValue . trim ( ) && ! searchResults . length ? translate ( 'common.noResultsFound' ) : '' ;
9299
93100 return (
@@ -114,15 +121,18 @@ function SelectCountryStep({disableAutoFocus}: {disableAutoFocus?: boolean}) {
114121 onChangeText : setSearchValue ,
115122 disableAutoFocus,
116123 } }
124+ searchValueForFocusSync = { debouncedSearchValue }
117125 confirmButtonOptions = { {
118126 onConfirm : submit ,
119127 showButton : true ,
120128 text : translate ( 'common.next' ) ,
121129 } }
122- initiallyFocusedItemKey = { currentCountry }
130+ initiallyFocusedItemKey = { initialSelectedValue }
123131 disableMaintainingScrollPosition
124132 shouldSingleExecuteRowSelect
125133 shouldUpdateFocusedIndex
134+ shouldScrollToFocusedIndex = { false }
135+ shouldScrollToFocusedIndexOnMount = { false }
126136 addBottomSafeAreaPadding
127137 shouldStopPropagation
128138 >
0 commit comments