-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathCountrySelectorModal.js
96 lines (81 loc) · 3.16 KB
/
CountrySelectorModal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import _ from 'underscore';
import React, {useMemo} from 'react';
import PropTypes from 'prop-types';
import CONST from '../../CONST';
import useLocalize from '../../hooks/useLocalize';
import HeaderWithBackButton from '../HeaderWithBackButton';
import SelectionListRadio from '../SelectionListRadio';
import Modal from '../Modal';
const propTypes = {
/** Whether the modal is visible */
isVisible: PropTypes.bool.isRequired,
/** Country value selected */
currentCountry: PropTypes.string,
/** Function to call when the user selects a Country */
onCountrySelected: PropTypes.func,
/** Function to call when the user closes the Country modal */
onClose: PropTypes.func,
/** The search value from the selection list */
searchValue: PropTypes.string.isRequired,
/** Function to call when the user types in the search input */
setSearchValue: PropTypes.func.isRequired,
};
const defaultProps = {
currentCountry: '',
onClose: () => {},
onCountrySelected: () => {},
};
function filterOptions(searchValue, data) {
const trimmedSearchValue = searchValue.trim();
if (trimmedSearchValue.length === 0) {
return [];
}
return _.filter(data, (country) => country.text.toLowerCase().includes(searchValue.toLowerCase()));
}
function CountrySelectorModal({currentCountry, isVisible, onClose, onCountrySelected, setSearchValue, searchValue}) {
const {translate} = useLocalize();
const countries = useMemo(
() =>
_.map(translate('allCountries'), (countryName, countryISO) => ({
value: countryISO,
keyForList: countryISO,
text: countryName,
isSelected: currentCountry === countryISO,
})),
[translate, currentCountry],
);
const filteredData = filterOptions(searchValue, countries);
const headerMessage = searchValue.trim() && !filteredData.length ? translate('common.noResultsFound') : '';
return (
<Modal
type={CONST.MODAL.MODAL_TYPE.RIGHT_DOCKED}
isVisible={isVisible}
onClose={onClose}
onModalHide={onClose}
hideModalContentWhileAnimating
useNativeDriver
>
<HeaderWithBackButton
title={translate('common.country')}
onBackButtonPress={onClose}
/>
<SelectionListRadio
headerMessage={headerMessage}
textInputLabel={translate('common.country')}
textInputPlaceholder={translate('countrySelectorModal.placeholderText')}
textInputValue={searchValue}
sections={[{data: filteredData, indexOffset: 0}]}
onSelectRow={onCountrySelected}
onChangeText={setSearchValue}
shouldFocusOnSelectRow
shouldHaveOptionSeparator
shouldDelayFocus
initiallyFocusedOptionKey={currentCountry}
/>
</Modal>
);
}
CountrySelectorModal.propTypes = propTypes;
CountrySelectorModal.defaultProps = defaultProps;
CountrySelectorModal.displayName = 'CountrySelectorModal';
export default CountrySelectorModal;