forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountrySelector.tsx
75 lines (63 loc) · 2.71 KB
/
CountrySelector.tsx
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
import {useIsFocused} from '@react-navigation/native';
import React, {forwardRef, useEffect, useRef} from 'react';
import type {ForwardedRef} from 'react';
import type {View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import type {MaybePhraseKey} from '@libs/Localize';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import type {Country} from '@src/CONST';
import ROUTES from '@src/ROUTES';
import MenuItemWithTopDescription from './MenuItemWithTopDescription';
type CountrySelectorProps = {
/** Form error text. e.g when no country is selected */
errorText?: MaybePhraseKey;
/** Callback called when the country changes. */
onInputChange?: (value?: string) => void;
/** Current selected country */
value?: Country | '';
/** inputID used by the Form component */
// eslint-disable-next-line react/no-unused-prop-types
inputID: string;
/** Callback to call when the picker modal is dismissed */
onBlur?: () => void;
};
function CountrySelector({errorText = '', value: countryCode, onInputChange = () => {}, onBlur}: CountrySelectorProps, ref: ForwardedRef<View>) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const title = countryCode ? translate(`allCountries.${countryCode}`) : '';
const countryTitleDescStyle = title.length === 0 ? styles.textNormal : null;
const didOpenContrySelector = useRef(false);
const isFocused = useIsFocused();
useEffect(() => {
if (!isFocused || !didOpenContrySelector.current) {
return;
}
didOpenContrySelector.current = false;
onBlur?.();
}, [isFocused, onBlur]);
useEffect(() => {
// This will cause the form to revalidate and remove any error related to country name
onInputChange(countryCode);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [countryCode]);
return (
<MenuItemWithTopDescription
shouldShowRightIcon
title={title}
ref={ref}
descriptionTextStyle={countryTitleDescStyle}
brickRoadIndicator={errorText ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
description={translate('common.country')}
errorText={errorText}
onPress={() => {
const activeRoute = Navigation.getActiveRouteWithoutParams();
didOpenContrySelector.current = true;
Navigation.navigate(ROUTES.SETTINGS_ADDRESS_COUNTRY.getRoute(countryCode ?? '', activeRoute));
}}
/>
);
}
CountrySelector.displayName = 'CountrySelector';
export default forwardRef(CountrySelector);