|
| 1 | +import React, {useMemo} from 'react'; |
| 2 | +import HeaderWithBackButton from '@components/HeaderWithBackButton'; |
| 3 | +import Modal from '@components/Modal'; |
| 4 | +import ScreenWrapper from '@components/ScreenWrapper'; |
| 5 | +import SelectionList from '@components/SelectionList'; |
| 6 | +import RadioListItem from '@components/SelectionList/RadioListItem'; |
| 7 | +import useDebouncedState from '@hooks/useDebouncedState'; |
| 8 | +import useLocalize from '@hooks/useLocalize'; |
| 9 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 10 | +import searchCountryOptions from '@libs/searchCountryOptions'; |
| 11 | +import type {CountryData} from '@libs/searchCountryOptions'; |
| 12 | +import StringUtils from '@libs/StringUtils'; |
| 13 | +import CONST from '@src/CONST'; |
| 14 | +import type {TranslationPaths} from '@src/languages/types'; |
| 15 | + |
| 16 | +type CountrySelectorModalProps = { |
| 17 | + /** Whether the modal is visible */ |
| 18 | + isVisible: boolean; |
| 19 | + |
| 20 | + /** Function to call when the user closes the business type selector modal */ |
| 21 | + onClose: () => void; |
| 22 | + |
| 23 | + /** Label to display on field */ |
| 24 | + label: string; |
| 25 | + |
| 26 | + /** Country selected */ |
| 27 | + currentCountry: string; |
| 28 | + |
| 29 | + /** Function to call when the user selects a country */ |
| 30 | + onCountrySelected: (value: CountryData) => void; |
| 31 | + |
| 32 | + /** Function to call when the user presses on the modal backdrop */ |
| 33 | + onBackdropPress?: () => void; |
| 34 | +}; |
| 35 | + |
| 36 | +function CountrySelectorModal({isVisible, currentCountry, onCountrySelected, onClose, label, onBackdropPress}: CountrySelectorModalProps) { |
| 37 | + const {translate} = useLocalize(); |
| 38 | + const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState(''); |
| 39 | + |
| 40 | + const countries = useMemo( |
| 41 | + () => |
| 42 | + Object.keys(CONST.ALL_COUNTRIES).map((countryISO) => { |
| 43 | + const countryName = translate(`allCountries.${countryISO}` as TranslationPaths); |
| 44 | + return { |
| 45 | + value: countryISO, |
| 46 | + keyForList: countryISO, |
| 47 | + text: countryName, |
| 48 | + isSelected: currentCountry === countryISO, |
| 49 | + searchValue: StringUtils.sanitizeString(`${countryISO}${countryName}`), |
| 50 | + }; |
| 51 | + }), |
| 52 | + [translate, currentCountry], |
| 53 | + ); |
| 54 | + |
| 55 | + const searchResults = searchCountryOptions(debouncedSearchValue, countries); |
| 56 | + const headerMessage = debouncedSearchValue.trim() && !searchResults.length ? translate('common.noResultsFound') : ''; |
| 57 | + |
| 58 | + const styles = useThemeStyles(); |
| 59 | + |
| 60 | + return ( |
| 61 | + <Modal |
| 62 | + type={CONST.MODAL.MODAL_TYPE.RIGHT_DOCKED} |
| 63 | + isVisible={isVisible} |
| 64 | + onClose={onClose} |
| 65 | + onModalHide={onClose} |
| 66 | + hideModalContentWhileAnimating |
| 67 | + useNativeDriver |
| 68 | + onBackdropPress={onBackdropPress} |
| 69 | + > |
| 70 | + <ScreenWrapper |
| 71 | + style={[styles.pb0]} |
| 72 | + includePaddingTop={false} |
| 73 | + includeSafeAreaPaddingBottom={false} |
| 74 | + testID={CountrySelectorModal.displayName} |
| 75 | + > |
| 76 | + <HeaderWithBackButton |
| 77 | + title={label} |
| 78 | + shouldShowBackButton |
| 79 | + onBackButtonPress={onClose} |
| 80 | + /> |
| 81 | + <SelectionList |
| 82 | + headerMessage={headerMessage} |
| 83 | + sections={[{data: searchResults}]} |
| 84 | + textInputValue={searchValue} |
| 85 | + textInputLabel={translate('common.search')} |
| 86 | + onChangeText={setSearchValue} |
| 87 | + onSelectRow={onCountrySelected} |
| 88 | + ListItem={RadioListItem} |
| 89 | + initiallyFocusedOptionKey={currentCountry} |
| 90 | + shouldSingleExecuteRowSelect |
| 91 | + shouldStopPropagation |
| 92 | + shouldUseDynamicMaxToRenderPerBatch |
| 93 | + /> |
| 94 | + </ScreenWrapper> |
| 95 | + </Modal> |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +CountrySelectorModal.displayName = 'CountrySelectorModal'; |
| 100 | + |
| 101 | +export default CountrySelectorModal; |
0 commit comments