react-native-international-phone-number 0.4.12 → 0.4.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -39
- package/lib/index.d.ts +8 -1
- package/lib/index.js +283 -170
- package/lib/interfaces/phoneInputRef.ts +14 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -47,9 +47,12 @@
|
|
|
47
47
|
- [With Class Component](#class-component)
|
|
48
48
|
- [With Function Component](#function-component)
|
|
49
49
|
- [Custom Default Flag](#custom-default-flag)
|
|
50
|
-
- [
|
|
51
|
-
- [
|
|
52
|
-
- [
|
|
50
|
+
- [Default Phone Number Value](#default-phone-number-value)
|
|
51
|
+
- [Typescript](#typescript)
|
|
52
|
+
- [Intermediate Usage](#intermediate-usage)
|
|
53
|
+
- [Typescript + useRef](#typescript--useref)
|
|
54
|
+
- [Advanced Usage](#advanced-usage)
|
|
55
|
+
- [React-Hook-Form + Typescript + Default Phone Number Value](#react-hook-form--typescript--default-phone-number-value)
|
|
53
56
|
- [Customizing Lib](#customizing-lib)
|
|
54
57
|
- [Dark Mode](#dark-mode)
|
|
55
58
|
- [Custom Lib Styles](#custom-lib-styles)
|
|
@@ -242,35 +245,29 @@ export default function App() {
|
|
|
242
245
|
}
|
|
243
246
|
```
|
|
244
247
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
## Basic Usage - Typescript
|
|
248
|
+
- ### Default Phone Number Value
|
|
248
249
|
|
|
249
250
|
```tsx
|
|
250
251
|
import React, { useState } from 'react';
|
|
251
252
|
import { View, Text } from 'react-native';
|
|
252
|
-
import {
|
|
253
|
-
PhoneInput,
|
|
254
|
-
ICountry,
|
|
255
|
-
} from 'react-native-international-phone-number';
|
|
253
|
+
import { PhoneInput } from 'react-native-international-phone-number';
|
|
256
254
|
|
|
257
255
|
export default function App() {
|
|
258
|
-
const [selectedCountry, setSelectedCountry] = useState
|
|
259
|
-
|
|
260
|
-
>(undefined);
|
|
261
|
-
const [inputValue, setInputValue] = useState<string>('');
|
|
256
|
+
const [selectedCountry, setSelectedCountry] = useState(undefined);
|
|
257
|
+
const [inputValue, setInputValue] = useState('');
|
|
262
258
|
|
|
263
|
-
function handleInputValue(phoneNumber
|
|
259
|
+
function handleInputValue(phoneNumber) {
|
|
264
260
|
setInputValue(phoneNumber);
|
|
265
261
|
}
|
|
266
262
|
|
|
267
|
-
function handleSelectedCountry(country
|
|
263
|
+
function handleSelectedCountry(country) {
|
|
268
264
|
setSelectedCountry(country);
|
|
269
265
|
}
|
|
270
266
|
|
|
271
267
|
return (
|
|
272
268
|
<View style={{ width: '100%', flex: 1, padding: 24 }}>
|
|
273
269
|
<PhoneInput
|
|
270
|
+
defaultValue="+12505550199"
|
|
274
271
|
value={inputValue}
|
|
275
272
|
onChangePhoneNumber={handleInputValue}
|
|
276
273
|
selectedCountry={selectedCountry}
|
|
@@ -291,9 +288,12 @@ export default function App() {
|
|
|
291
288
|
}
|
|
292
289
|
```
|
|
293
290
|
|
|
294
|
-
|
|
291
|
+
> Observations:
|
|
292
|
+
>
|
|
293
|
+
> 1. You need to use a default value with the following format: `+(country callling code)(area code)(number phone)`
|
|
294
|
+
> 2. The lib has the mechanism to set the flag and mask of the supplied `defaultValue`. However, if the supplied `defaultValue` does not match any international standard, the `input mask of the defaultValue` will be set to "BR" (please make sure that the default value is in the format mentioned above).
|
|
295
295
|
|
|
296
|
-
|
|
296
|
+
- ### Typescript
|
|
297
297
|
|
|
298
298
|
```tsx
|
|
299
299
|
import React, { useState } from 'react';
|
|
@@ -320,7 +320,6 @@ export default function App() {
|
|
|
320
320
|
return (
|
|
321
321
|
<View style={{ width: '100%', flex: 1, padding: 24 }}>
|
|
322
322
|
<PhoneInput
|
|
323
|
-
defaultValue="+12505550199"
|
|
324
323
|
value={inputValue}
|
|
325
324
|
onChangePhoneNumber={handleInputValue}
|
|
326
325
|
selectedCountry={selectedCountry}
|
|
@@ -341,14 +340,63 @@ export default function App() {
|
|
|
341
340
|
}
|
|
342
341
|
```
|
|
343
342
|
|
|
344
|
-
> Observations:
|
|
345
|
-
>
|
|
346
|
-
> 1. You need to use a default value with the following format: `+(country callling code)(area code)(number phone)`
|
|
347
|
-
> 2. The lib has the mechanism to set the flag and mask of the supplied `defaultValue`. However, if the supplied `defaultValue` does not match any international standard, the `input mask of the defaultValue` will be set to "BR" (please make sure that the default value is in the format mentioned above).
|
|
348
|
-
|
|
349
343
|
<br>
|
|
350
344
|
|
|
351
|
-
##
|
|
345
|
+
## Intermediate Usage
|
|
346
|
+
|
|
347
|
+
- ### Typescript + useRef
|
|
348
|
+
|
|
349
|
+
```tsx
|
|
350
|
+
import React, { useState, useRef } from 'react';
|
|
351
|
+
import { View, Text } from 'react-native';
|
|
352
|
+
import {
|
|
353
|
+
PhoneInput,
|
|
354
|
+
ICountry,
|
|
355
|
+
IPhoneInputRef,
|
|
356
|
+
} from 'react-native-international-phone-number';
|
|
357
|
+
|
|
358
|
+
export default function App() {
|
|
359
|
+
const phoneInputRef = useRef<IPhoneInputRef>(null);
|
|
360
|
+
|
|
361
|
+
function onSubmitRef() {
|
|
362
|
+
Alert.alert(
|
|
363
|
+
'Intermediate Result',
|
|
364
|
+
`${phoneInputRef.current?.selectedCountry?.callingCode} ${phoneInputRef.current?.value}`
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return (
|
|
369
|
+
<View style={{ width: '100%', flex: 1, padding: 24 }}>
|
|
370
|
+
<PhoneInput ref={phoneInputRef} />
|
|
371
|
+
<TouchableOpacity
|
|
372
|
+
style={{
|
|
373
|
+
width: '100%',
|
|
374
|
+
paddingVertical: 12,
|
|
375
|
+
backgroundColor: '#2196F3',
|
|
376
|
+
borderRadius: 4,
|
|
377
|
+
marginTop: 10,
|
|
378
|
+
}}
|
|
379
|
+
onPress={onSubmit}
|
|
380
|
+
>
|
|
381
|
+
<Text
|
|
382
|
+
style={{
|
|
383
|
+
color: '#F3F3F3',
|
|
384
|
+
textAlign: 'center',
|
|
385
|
+
fontSize: 16,
|
|
386
|
+
fontWeight: 'bold',
|
|
387
|
+
}}
|
|
388
|
+
>
|
|
389
|
+
Submit
|
|
390
|
+
</Text>
|
|
391
|
+
</TouchableOpacity>
|
|
392
|
+
</View>
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
## Advanced Usage
|
|
398
|
+
|
|
399
|
+
- ### React-Hook-Form + Typescript + Default Phone Number Value
|
|
352
400
|
|
|
353
401
|
```tsx
|
|
354
402
|
import React, { useState, useEffect } from 'react';
|
|
@@ -357,7 +405,7 @@ import {
|
|
|
357
405
|
PhoneInput,
|
|
358
406
|
ICountry,
|
|
359
407
|
} from 'react-native-international-phone-number';
|
|
360
|
-
import { Controller, FieldValues
|
|
408
|
+
import { Controller, FieldValues } from 'react-hook-form';
|
|
361
409
|
|
|
362
410
|
interface FormProps extends FieldValues {
|
|
363
411
|
phoneNumber: string;
|
|
@@ -368,9 +416,6 @@ export default function App() {
|
|
|
368
416
|
undefined | ICountry
|
|
369
417
|
>(undefined);
|
|
370
418
|
|
|
371
|
-
const { control, handleSubmit, setValue, watch } =
|
|
372
|
-
useForm<FormProps>();
|
|
373
|
-
|
|
374
419
|
function handleSelectedCountry(country: ICountry) {
|
|
375
420
|
setSelectedCountry(country);
|
|
376
421
|
}
|
|
@@ -382,12 +427,6 @@ export default function App() {
|
|
|
382
427
|
);
|
|
383
428
|
}
|
|
384
429
|
|
|
385
|
-
useEffect(() => {
|
|
386
|
-
const watchPhoneNumber = watch('phoneNumber');
|
|
387
|
-
|
|
388
|
-
setValue('phoneNumber', watchPhoneNumber);
|
|
389
|
-
}, []);
|
|
390
|
-
|
|
391
430
|
return (
|
|
392
431
|
<View style={{ width: '100%', flex: 1, padding: 24 }}>
|
|
393
432
|
<Controller
|
|
@@ -511,16 +550,17 @@ export default function App() {
|
|
|
511
550
|
## Component Props ([PhoneInputProps](https://github.com/AstrOOnauta/react-native-international-phone-number/blob/master/lib/interfaces/phoneInputProps.ts))
|
|
512
551
|
|
|
513
552
|
- `defaultValue?:` string;
|
|
514
|
-
- `value
|
|
515
|
-
- `onChangePhoneNumber
|
|
516
|
-
- `selectedCountry
|
|
517
|
-
- `onChangeSelectedCountry
|
|
553
|
+
- `value?:` string;
|
|
554
|
+
- `onChangePhoneNumber?:` (phoneNumber: string) => void;
|
|
555
|
+
- `selectedCountry?:` undefined | [ICountry](https://github.com/AstrOOnauta/react-native-international-phone-number/blob/master/lib/interfaces/country.ts);
|
|
556
|
+
- `onChangeSelectedCountry?:` (country: [ICountry](https://github.com/AstrOOnauta/react-native-international-phone-number/blob/master/lib/interfaces/country.ts)) => void;
|
|
518
557
|
- `disabled?:` boolean;
|
|
519
558
|
- `modalDisabled?:` boolean;
|
|
520
559
|
- `withDarkTheme?:` boolean;
|
|
521
560
|
- `containerStyle?:` StyleProp<[ViewStyle](https://reactnative.dev/docs/view-style-props)>;
|
|
522
561
|
- `flagContainerStyle?:` StyleProp<[ViewStyle](https://reactnative.dev/docs/view-style-props)>;
|
|
523
562
|
- `inputStyle?:` StyleProp<[TextStyle](https://reactnative.dev/docs/text-style-props)>;
|
|
563
|
+
- `ref?:` [Ref](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/663f439d11d78b65f1dfd38d120f3728ea2cc207/types/react/index.d.ts#L100)<[IPhoneInputRef](https://github.com/AstrOOnauta/react-native-international-phone-number/blob/master/lib/interfaces/phoneInputeRef.ts)>
|
|
524
564
|
|
|
525
565
|
<br>
|
|
526
566
|
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import { Ref } from 'react';
|
|
2
|
+
|
|
1
3
|
import { ICountry } from './interfaces/country';
|
|
4
|
+
import { IPhoneInputRef } from './interfaces/phoneInputRef';
|
|
2
5
|
import { PhoneInputProps } from './interfaces/phoneInputProps';
|
|
3
6
|
|
|
4
|
-
declare function PhoneInput
|
|
7
|
+
declare function PhoneInput<IPhoneInputRef, PhoneInputProps>(
|
|
8
|
+
props: PhoneInputProps,
|
|
9
|
+
ref?: Ref<IPhoneInputRef>
|
|
10
|
+
): JSX.Element;
|
|
5
11
|
|
|
6
12
|
declare function getAllCountries(): ICountry[];
|
|
7
13
|
|
|
@@ -29,5 +35,6 @@ export {
|
|
|
29
35
|
getCountriesByCallingCode,
|
|
30
36
|
getCountriesByName,
|
|
31
37
|
ICountry,
|
|
38
|
+
IPhoneInputRef,
|
|
32
39
|
PhoneInputProps,
|
|
33
40
|
};
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, {
|
|
2
|
+
useEffect,
|
|
3
|
+
useState,
|
|
4
|
+
useRef,
|
|
5
|
+
forwardRef,
|
|
6
|
+
} from 'react';
|
|
2
7
|
import {
|
|
3
8
|
StyleSheet,
|
|
4
9
|
Dimensions,
|
|
@@ -17,196 +22,304 @@ import getCountryByCca2 from './utils/getCountryByCca2';
|
|
|
17
22
|
import getCountryByPhoneNumber from './utils/getCountryByPhoneNumber';
|
|
18
23
|
import phoneMask from './utils/inputMask';
|
|
19
24
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
onChangePhoneNumber('');
|
|
43
|
-
if (onChangeSelectedCountry) {
|
|
44
|
-
onChangeSelectedCountry({
|
|
45
|
-
name: country.name,
|
|
46
|
-
cca2: country.cca2,
|
|
47
|
-
flag: country.flag,
|
|
48
|
-
callingCode: `+${country.callingCode[0]}`,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function onChangeText(phoneNumber, callingCode) {
|
|
54
|
-
const res = phoneMask(
|
|
55
|
-
phoneNumber,
|
|
56
|
-
callingCode ? callingCode : selectedCountry?.callingCode,
|
|
57
|
-
selectedCountry?.cca2
|
|
25
|
+
const PhoneInput = forwardRef(
|
|
26
|
+
(
|
|
27
|
+
{
|
|
28
|
+
placeholder,
|
|
29
|
+
placeholderTextColor,
|
|
30
|
+
selectionColor,
|
|
31
|
+
containerStyle,
|
|
32
|
+
flagContainerStyle,
|
|
33
|
+
inputStyle,
|
|
34
|
+
withDarkTheme,
|
|
35
|
+
disabled,
|
|
36
|
+
modalDisabled,
|
|
37
|
+
defaultValue,
|
|
38
|
+
onChangePhoneNumber,
|
|
39
|
+
selectedCountry,
|
|
40
|
+
onChangeSelectedCountry,
|
|
41
|
+
...rest
|
|
42
|
+
},
|
|
43
|
+
ref
|
|
44
|
+
) => {
|
|
45
|
+
const [containerWidth, setContainerWidth] = useState(
|
|
46
|
+
Dimensions.get('window').width
|
|
58
47
|
);
|
|
48
|
+
const [defaultCca2, setDefaultCca2] = useState('');
|
|
49
|
+
const [inputValue, setInputValue] = useState(null);
|
|
50
|
+
const [countryValue, setCountryValue] = useState(null);
|
|
59
51
|
|
|
60
|
-
|
|
61
|
-
}
|
|
52
|
+
const textInputRef = useRef(null);
|
|
62
53
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
54
|
+
const refBase = {
|
|
55
|
+
...textInputRef.current,
|
|
56
|
+
onFocus: textInputRef.current?.focus,
|
|
57
|
+
focus: textInputRef.current?.focus,
|
|
58
|
+
getValue: () => inputValue,
|
|
59
|
+
value: inputValue,
|
|
60
|
+
getSelectedCountry: () => countryValue,
|
|
61
|
+
selectedCountry: countryValue,
|
|
62
|
+
props: {
|
|
63
|
+
placeholder,
|
|
64
|
+
placeholderTextColor,
|
|
65
|
+
selectionColor,
|
|
66
|
+
containerStyle,
|
|
67
|
+
flagContainerStyle,
|
|
68
|
+
inputStyle,
|
|
69
|
+
withDarkTheme,
|
|
70
|
+
disabled,
|
|
71
|
+
modalDisabled,
|
|
72
|
+
defaultValue,
|
|
73
|
+
onChangePhoneNumber,
|
|
74
|
+
selectedCountry,
|
|
75
|
+
onChangeSelectedCountry,
|
|
76
|
+
...rest,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
67
79
|
|
|
68
|
-
|
|
69
|
-
|
|
80
|
+
function updateRef(phoneNumber, country) {
|
|
81
|
+
if (ref) {
|
|
82
|
+
ref.current = {
|
|
83
|
+
...refBase,
|
|
84
|
+
getValue: () => phoneNumber,
|
|
85
|
+
value: phoneNumber,
|
|
86
|
+
getSelectedCountry: () => country,
|
|
87
|
+
selectedCountry: country,
|
|
88
|
+
props: {
|
|
89
|
+
...refBase.props,
|
|
90
|
+
value: phoneNumber,
|
|
91
|
+
selectedCountry: country,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
70
96
|
|
|
71
|
-
|
|
97
|
+
function onSelect(country) {
|
|
98
|
+
if (ref) {
|
|
99
|
+
setInputValue('');
|
|
72
100
|
} else {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
101
|
+
onChangePhoneNumber('');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (onChangeSelectedCountry || ref) {
|
|
105
|
+
const newValue = {
|
|
106
|
+
name: country.name,
|
|
107
|
+
cca2: country.cca2,
|
|
108
|
+
flag: country.flag,
|
|
109
|
+
callingCode: `+${country.callingCode[0]}`,
|
|
110
|
+
};
|
|
76
111
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
112
|
+
if (ref) {
|
|
113
|
+
setCountryValue(newValue);
|
|
114
|
+
updateRef('', newValue);
|
|
115
|
+
} else {
|
|
116
|
+
onChangeSelectedCountry(newValue);
|
|
117
|
+
}
|
|
80
118
|
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function onChangeText(phoneNumber, callingCode) {
|
|
122
|
+
const res = phoneMask(
|
|
123
|
+
phoneNumber,
|
|
124
|
+
callingCode ? callingCode : countryValue?.callingCode,
|
|
125
|
+
countryValue?.cca2
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
if (ref) {
|
|
129
|
+
setInputValue(res);
|
|
130
|
+
updateRef(res, countryValue);
|
|
131
|
+
} else {
|
|
132
|
+
onChangePhoneNumber(res);
|
|
89
133
|
}
|
|
90
134
|
}
|
|
91
|
-
}, [defaultValue]);
|
|
92
135
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
) {
|
|
100
|
-
const callingCode = selectedCountry.callingCode;
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
if (ref) {
|
|
138
|
+
setInputValue('');
|
|
139
|
+
} else {
|
|
140
|
+
onChangePhoneNumber('');
|
|
141
|
+
}
|
|
101
142
|
|
|
102
|
-
|
|
143
|
+
if (defaultValue) {
|
|
144
|
+
const matchingCountry = getCountryByPhoneNumber(defaultValue);
|
|
103
145
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
146
|
+
if (matchingCountry) {
|
|
147
|
+
setDefaultCca2(matchingCountry.cca2);
|
|
148
|
+
|
|
149
|
+
if (ref) {
|
|
150
|
+
setCountryValue(matchingCountry);
|
|
151
|
+
updateRef('', matchingCountry);
|
|
152
|
+
} else {
|
|
153
|
+
onChangeSelectedCountry(matchingCountry);
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
setDefaultCca2(null);
|
|
157
|
+
|
|
158
|
+
if (ref) {
|
|
159
|
+
setCountryValue(null);
|
|
160
|
+
updateRef('', null);
|
|
161
|
+
} else {
|
|
162
|
+
onChangeSelectedCountry(null);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
onChangeText('', null);
|
|
166
|
+
|
|
167
|
+
console.warn(
|
|
168
|
+
"The default number provided (defaultValue) don't match with anyone country. Please, correct it to be shown in the input. For more information: https://github.com/AstrOOnauta/react-native-international-phone-number#intermediate-usage---typescript--default-phone-number-value"
|
|
126
169
|
);
|
|
170
|
+
}
|
|
127
171
|
} else {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
onChangeText(phoneNumber, callingCode);
|
|
137
|
-
}
|
|
138
|
-
}, [selectedCountry]);
|
|
172
|
+
if (!countryValue) {
|
|
173
|
+
const defaultCountry = {
|
|
174
|
+
callingCode: '+55',
|
|
175
|
+
cca2: 'BR',
|
|
176
|
+
flag: 'flag-br',
|
|
177
|
+
name: 'Brazil',
|
|
178
|
+
};
|
|
139
179
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
: {
|
|
151
|
-
...styles.lightContainer,
|
|
152
|
-
backgroundColor: disabled
|
|
153
|
-
? '#E3E3E3'
|
|
154
|
-
: styles.lightContainer.backgroundColor,
|
|
155
|
-
},
|
|
156
|
-
containerStyle ? containerStyle : {},
|
|
157
|
-
]}
|
|
158
|
-
onLayout={(e) => setContainerWidth(e.nativeEvent.layout.width)}
|
|
159
|
-
>
|
|
160
|
-
<CountryPicker
|
|
161
|
-
containerButtonStyle={[
|
|
162
|
-
flagContainerBase,
|
|
163
|
-
selectedCountry?.cca2 === 'VA' ? { width: 140 } : {},
|
|
164
|
-
flagContainerStyle ? flagContainerStyle : {},
|
|
165
|
-
]}
|
|
166
|
-
onSelect={onSelect}
|
|
167
|
-
withFilter
|
|
168
|
-
withAlphaFilter
|
|
169
|
-
withCallingCode
|
|
170
|
-
withCallingCodeButton={selectedCountry || defaultCca2}
|
|
171
|
-
theme={withDarkTheme ? DARK_THEME : DEFAULT_THEME}
|
|
172
|
-
countryCode={
|
|
173
|
-
selectedCountry ? selectedCountry.cca2 : defaultCca2
|
|
180
|
+
if (ref) {
|
|
181
|
+
setCountryValue(defaultCountry);
|
|
182
|
+
updateRef('', defaultCountry);
|
|
183
|
+
} else {
|
|
184
|
+
onChangeSelectedCountry(defaultCountry);
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
if (ref) {
|
|
188
|
+
updateRef('', countryValue);
|
|
189
|
+
}
|
|
174
190
|
}
|
|
175
|
-
|
|
176
|
-
|
|
191
|
+
}
|
|
192
|
+
}, [defaultValue]);
|
|
193
|
+
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
if (
|
|
196
|
+
defaultValue &&
|
|
197
|
+
countryValue &&
|
|
198
|
+
countryValue.cca2 === defaultCca2 &&
|
|
199
|
+
!inputValue
|
|
200
|
+
) {
|
|
201
|
+
const callingCode = countryValue.callingCode;
|
|
202
|
+
|
|
203
|
+
let phoneNumber = defaultValue;
|
|
204
|
+
|
|
205
|
+
if (
|
|
206
|
+
callingCode === '+1' &&
|
|
207
|
+
countryValue.cca2 !== 'CA' &&
|
|
208
|
+
countryValue.cca2 !== 'US'
|
|
209
|
+
) {
|
|
210
|
+
phoneNumber = defaultValue
|
|
211
|
+
.replace(/\s/g, '')
|
|
212
|
+
.substring(
|
|
213
|
+
callingCode.length + 3,
|
|
214
|
+
defaultValue.replace(/\D/g, '').length +
|
|
215
|
+
callingCode.length
|
|
216
|
+
);
|
|
217
|
+
} else if (
|
|
218
|
+
callingCode === '+39' &&
|
|
219
|
+
countryValue.cca2 === 'VA'
|
|
220
|
+
) {
|
|
221
|
+
phoneNumber = defaultValue
|
|
222
|
+
.replace(/\s/g, '')
|
|
223
|
+
.substring(
|
|
224
|
+
callingCode.length + 5,
|
|
225
|
+
defaultValue.replace(/\D/g, '').length +
|
|
226
|
+
callingCode.length
|
|
227
|
+
);
|
|
228
|
+
} else {
|
|
229
|
+
phoneNumber = defaultValue
|
|
230
|
+
.replace(/\s/g, '')
|
|
231
|
+
.substring(
|
|
232
|
+
callingCode.length,
|
|
233
|
+
defaultValue.replace(/\D/g, '').length +
|
|
234
|
+
callingCode.length
|
|
235
|
+
);
|
|
177
236
|
}
|
|
178
|
-
|
|
179
|
-
|
|
237
|
+
onChangeText(phoneNumber, callingCode);
|
|
238
|
+
}
|
|
239
|
+
}, [countryValue]);
|
|
240
|
+
|
|
241
|
+
useEffect(() => {
|
|
242
|
+
if (!ref) {
|
|
243
|
+
setInputValue(rest.value);
|
|
244
|
+
setCountryValue(selectedCountry);
|
|
245
|
+
}
|
|
246
|
+
}, [selectedCountry]);
|
|
247
|
+
|
|
248
|
+
return (
|
|
249
|
+
<View
|
|
180
250
|
style={[
|
|
181
|
-
withDarkTheme
|
|
182
|
-
|
|
183
|
-
|
|
251
|
+
withDarkTheme
|
|
252
|
+
? {
|
|
253
|
+
...styles.darkContainer,
|
|
254
|
+
backgroundColor: disabled
|
|
255
|
+
? '#858585'
|
|
256
|
+
: styles.darkContainer.backgroundColor,
|
|
257
|
+
}
|
|
258
|
+
: {
|
|
259
|
+
...styles.lightContainer,
|
|
260
|
+
backgroundColor: disabled
|
|
261
|
+
? '#E3E3E3'
|
|
262
|
+
: styles.lightContainer.backgroundColor,
|
|
263
|
+
},
|
|
264
|
+
containerStyle ? containerStyle : {},
|
|
184
265
|
]}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
placeholderTextColor={
|
|
189
|
-
placeholderTextColor
|
|
190
|
-
? placeholderTextColor
|
|
191
|
-
: withDarkTheme
|
|
192
|
-
? '#CCCCCC'
|
|
193
|
-
: '#DDDDDD'
|
|
194
|
-
}
|
|
195
|
-
selectionColor={
|
|
196
|
-
selectionColor
|
|
197
|
-
? selectionColor
|
|
198
|
-
: withDarkTheme
|
|
199
|
-
? 'rgba(255,255,255, .4)'
|
|
200
|
-
: 'rgba(0 ,0 ,0 , .4)'
|
|
266
|
+
onLayout={(e) =>
|
|
267
|
+
setContainerWidth(e.nativeEvent.layout.width)
|
|
201
268
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
269
|
+
>
|
|
270
|
+
<CountryPicker
|
|
271
|
+
containerButtonStyle={[
|
|
272
|
+
flagContainerBase,
|
|
273
|
+
countryValue?.cca2 === 'VA' ? { width: 140 } : {},
|
|
274
|
+
flagContainerStyle ? flagContainerStyle : {},
|
|
275
|
+
]}
|
|
276
|
+
onSelect={onSelect}
|
|
277
|
+
withFilter
|
|
278
|
+
withAlphaFilter
|
|
279
|
+
withCallingCode
|
|
280
|
+
withCallingCodeButton={countryValue || defaultCca2}
|
|
281
|
+
theme={withDarkTheme ? DARK_THEME : DEFAULT_THEME}
|
|
282
|
+
countryCode={
|
|
283
|
+
countryValue ? countryValue?.cca2 : defaultCca2
|
|
284
|
+
}
|
|
285
|
+
modalProps={
|
|
286
|
+
disabled || modalDisabled ? { visible: false } : {}
|
|
287
|
+
}
|
|
288
|
+
/>
|
|
289
|
+
<TextInput
|
|
290
|
+
style={[
|
|
291
|
+
withDarkTheme ? styles.darkInput : styles.lightInput,
|
|
292
|
+
{ width: containerWidth - 100 },
|
|
293
|
+
inputStyle ? inputStyle : {},
|
|
294
|
+
]}
|
|
295
|
+
placeholder={
|
|
296
|
+
placeholder ? placeholder : 'Insert your phone number'
|
|
297
|
+
}
|
|
298
|
+
placeholderTextColor={
|
|
299
|
+
placeholderTextColor
|
|
300
|
+
? placeholderTextColor
|
|
301
|
+
: withDarkTheme
|
|
302
|
+
? '#CCCCCC'
|
|
303
|
+
: '#DDDDDD'
|
|
304
|
+
}
|
|
305
|
+
selectionColor={
|
|
306
|
+
selectionColor
|
|
307
|
+
? selectionColor
|
|
308
|
+
: withDarkTheme
|
|
309
|
+
? 'rgba(255,255,255, .4)'
|
|
310
|
+
: 'rgba(0 ,0 ,0 , .4)'
|
|
311
|
+
}
|
|
312
|
+
editable={!disabled}
|
|
313
|
+
value={inputValue}
|
|
314
|
+
onChangeText={onChangeText}
|
|
315
|
+
keyboardType="numeric"
|
|
316
|
+
ref={textInputRef}
|
|
317
|
+
{...rest}
|
|
318
|
+
/>
|
|
319
|
+
</View>
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
);
|
|
210
323
|
|
|
211
324
|
const containerBase = {
|
|
212
325
|
flexDirection: 'row',
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { TextInput } from 'react-native';
|
|
2
|
+
|
|
3
|
+
import { ICountry } from './country';
|
|
4
|
+
import { PhoneInputProps } from './phoneInputProps';
|
|
5
|
+
|
|
6
|
+
export interface IPhoneInputRef extends TextInput {
|
|
7
|
+
props: PhoneInputProps;
|
|
8
|
+
onFocus: () => void;
|
|
9
|
+
focus: () => void;
|
|
10
|
+
getValue: () => string;
|
|
11
|
+
value: string;
|
|
12
|
+
getSelectedCountry: () => ICountry;
|
|
13
|
+
selectedCountry: ICountry;
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-international-phone-number",
|
|
3
3
|
"author": "AstrOOnauta (https://github.com/AstrOOnauta)",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.13",
|
|
5
5
|
"description": "International mobile phone input component with mask for React Native",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/index.d.ts",
|