-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathPhoneInput.tsx
More file actions
115 lines (110 loc) · 3.57 KB
/
Copy pathPhoneInput.tsx
File metadata and controls
115 lines (110 loc) · 3.57 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import React, { useCallback } from 'react';
import { Appearance, Image, TouchableOpacity, View } from 'react-native';
import CountryPicker, {
CountryModalProvider,
DARK_THEME,
DEFAULT_THEME,
FlagButton,
} from 'react-native-country-picker-modal';
import MaskInput from 'react-native-mask-input';
import { EXCLUDED_COUNTRIES } from './constants';
import type { PhoneInputProps } from './PhoneInput.types';
import styles from './styles';
import { usePhoneInput } from './usePhoneInput';
const isDarkTheme = Appearance.getColorScheme() === 'dark';
const dropDownImage =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAi0lEQVRYR+3WuQ6AIBRE0eHL1T83FBqU5S1szdiY2NyTKcCAzU/Y3AcBXIALcIF0gRPAsehgugDEXnYQrUC88RIgfpuJ+MRrgFmILN4CjEYU4xJgFKIa1wB6Ec24FuBFiHELwIpQxa0ALUId9wAkhCnuBdQQ5ngP4I9wxXsBDyJ9m+8y/g9wAS7ABW4giBshQZji3AAAAABJRU5ErkJggg==';
export const PhoneInput: React.FC<PhoneInputProps> = (props) => {
const {
models: { countryCode, phoneNumber, mask, inputRef },
actions: { handleChangeText, onSelect },
forms: { modalVisible, showModal, hideModal },
} = usePhoneInput(props);
const {
theme: {
enableDarkTheme = isDarkTheme,
textInputStyle,
flagButtonStyle,
containerStyle,
dropDownImageStyle,
} = {},
maskInputProps,
autoFocus,
hideDropdownIcon,
renderCustomDropdown,
countryPickerProps,
flagProps,
disabled,
dropDownImageProps,
} = props;
const renderFlagButton = useCallback(
(flagButtonProps?: { onOpen?: () => void }) => (
<FlagButton
countryCode={countryCode}
flagSize={flagProps?.flagSize || DEFAULT_THEME.flagSize}
placeholder={countryCode}
onOpen={flagButtonProps?.onOpen}
{...flagProps}
/>
),
[countryCode, flagProps]
);
return (
<CountryModalProvider>
<View
testID="phone-input-container"
style={[styles.container, containerStyle]}
>
<TouchableOpacity
testID="country-picker-button"
style={[styles.flagButtonView, flagButtonStyle]}
disabled={disabled}
onPress={showModal}
>
<CountryPicker
onSelect={onSelect}
withEmoji
withFilter
withFlag
withAlphaFilter
countryCode={countryCode}
withCallingCode
disableNativeModal={disabled}
visible={modalVisible}
theme={enableDarkTheme ? DARK_THEME : DEFAULT_THEME}
renderFlagButton={renderFlagButton}
excludeCountries={EXCLUDED_COUNTRIES}
onClose={hideModal}
{...countryPickerProps}
/>
{!hideDropdownIcon &&
(renderCustomDropdown || (
<Image
testID="dropdown-icon"
source={{ uri: dropDownImage }}
resizeMode="contain"
style={[styles.dropDownImage, dropDownImageStyle]}
{...dropDownImageProps}
/>
))}
</TouchableOpacity>
<MaskInput
testID="phone-input"
ref={(ref) => {
inputRef.current = ref;
}}
style={[styles.numberText, textInputStyle]}
mask={mask}
onChangeText={handleChangeText}
value={phoneNumber}
editable={!disabled}
selectionColor="black"
keyboardAppearance={enableDarkTheme ? 'dark' : 'default'}
keyboardType="number-pad"
autoFocus={autoFocus}
{...maskInputProps}
/>
</View>
</CountryModalProvider>
);
};