react-native-international-phone-number 0.4.11 → 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 +87 -41
- 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
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
<img src="https://img.shields.io/npm/v/react-native-international-phone-number.svg?style=flat-square">
|
|
12
12
|
</a>
|
|
13
13
|
<a href="https://www.npmjs.com/package/react-native-international-phone-number">
|
|
14
|
-
<img src="https://img.shields.io/npm/
|
|
14
|
+
<img src="https://img.shields.io/npm/dt/react-native-international-phone-number.svg?style=flat-square&color=success">
|
|
15
15
|
</a>
|
|
16
16
|
<a href="https://github.com/AstrOOnauta/react-native-international-phone-number">
|
|
17
17
|
<img src="https://img.shields.io/github/stars/AstrOOnauta/react-native-international-phone-number?style=flat-square&color=success"/>
|
|
@@ -29,6 +29,12 @@
|
|
|
29
29
|
|
|
30
30
|
<br>
|
|
31
31
|
|
|
32
|
+
<div align = "center">
|
|
33
|
+
<a href="https://www.buymeacoffee.com/astroonauta" target="_blank">
|
|
34
|
+
<img src="https://survivingmexico.files.wordpress.com/2018/07/button-gif.gif" alt="Buy Me A Coffee" style="height: auto !important;width: 60% !important;">
|
|
35
|
+
</a>
|
|
36
|
+
</div>
|
|
37
|
+
|
|
32
38
|
## Try it out
|
|
33
39
|
|
|
34
40
|
- [Demo](https://snack.expo.dev/@astroonauta/react-native-international-phone-number)
|
|
@@ -41,16 +47,19 @@
|
|
|
41
47
|
- [With Class Component](#class-component)
|
|
42
48
|
- [With Function Component](#function-component)
|
|
43
49
|
- [Custom Default Flag](#custom-default-flag)
|
|
44
|
-
- [
|
|
45
|
-
- [
|
|
46
|
-
- [
|
|
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)
|
|
47
56
|
- [Customizing Lib](#customizing-lib)
|
|
48
57
|
- [Dark Mode](#dark-mode)
|
|
49
58
|
- [Custom Lib Styles](#custom-lib-styles)
|
|
50
59
|
- [Phone Input Disabled Mode](#phone-input-disabled-mode)
|
|
51
60
|
- [Country Modal Disabled Mode](#country-modal-disabled-mode)
|
|
52
61
|
- [Custom Disabled Mode Style](#custom-disabled-mode-style)
|
|
53
|
-
- [Lib Props](#props)
|
|
62
|
+
- [Lib Props](#component-props-phoneinputprops)
|
|
54
63
|
- [Lib Functions](#functions)
|
|
55
64
|
- [Contributing](#contributing)
|
|
56
65
|
- [License](#license)
|
|
@@ -236,35 +245,29 @@ export default function App() {
|
|
|
236
245
|
}
|
|
237
246
|
```
|
|
238
247
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
## Basic Usage - Typescript
|
|
248
|
+
- ### Default Phone Number Value
|
|
242
249
|
|
|
243
250
|
```tsx
|
|
244
251
|
import React, { useState } from 'react';
|
|
245
252
|
import { View, Text } from 'react-native';
|
|
246
|
-
import {
|
|
247
|
-
PhoneInput,
|
|
248
|
-
ICountry,
|
|
249
|
-
} from 'react-native-international-phone-number';
|
|
253
|
+
import { PhoneInput } from 'react-native-international-phone-number';
|
|
250
254
|
|
|
251
255
|
export default function App() {
|
|
252
|
-
const [selectedCountry, setSelectedCountry] = useState
|
|
253
|
-
|
|
254
|
-
>(undefined);
|
|
255
|
-
const [inputValue, setInputValue] = useState<string>('');
|
|
256
|
+
const [selectedCountry, setSelectedCountry] = useState(undefined);
|
|
257
|
+
const [inputValue, setInputValue] = useState('');
|
|
256
258
|
|
|
257
|
-
function handleInputValue(phoneNumber
|
|
259
|
+
function handleInputValue(phoneNumber) {
|
|
258
260
|
setInputValue(phoneNumber);
|
|
259
261
|
}
|
|
260
262
|
|
|
261
|
-
function handleSelectedCountry(country
|
|
263
|
+
function handleSelectedCountry(country) {
|
|
262
264
|
setSelectedCountry(country);
|
|
263
265
|
}
|
|
264
266
|
|
|
265
267
|
return (
|
|
266
268
|
<View style={{ width: '100%', flex: 1, padding: 24 }}>
|
|
267
269
|
<PhoneInput
|
|
270
|
+
defaultValue="+12505550199"
|
|
268
271
|
value={inputValue}
|
|
269
272
|
onChangePhoneNumber={handleInputValue}
|
|
270
273
|
selectedCountry={selectedCountry}
|
|
@@ -285,9 +288,12 @@ export default function App() {
|
|
|
285
288
|
}
|
|
286
289
|
```
|
|
287
290
|
|
|
288
|
-
|
|
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).
|
|
289
295
|
|
|
290
|
-
|
|
296
|
+
- ### Typescript
|
|
291
297
|
|
|
292
298
|
```tsx
|
|
293
299
|
import React, { useState } from 'react';
|
|
@@ -314,7 +320,6 @@ export default function App() {
|
|
|
314
320
|
return (
|
|
315
321
|
<View style={{ width: '100%', flex: 1, padding: 24 }}>
|
|
316
322
|
<PhoneInput
|
|
317
|
-
defaultValue="+12505550199"
|
|
318
323
|
value={inputValue}
|
|
319
324
|
onChangePhoneNumber={handleInputValue}
|
|
320
325
|
selectedCountry={selectedCountry}
|
|
@@ -335,14 +340,63 @@ export default function App() {
|
|
|
335
340
|
}
|
|
336
341
|
```
|
|
337
342
|
|
|
338
|
-
> Observations:
|
|
339
|
-
>
|
|
340
|
-
> 1. You need to use a default value with the following format: `+(country callling code)(area code)(number phone)`
|
|
341
|
-
> 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).
|
|
342
|
-
|
|
343
343
|
<br>
|
|
344
344
|
|
|
345
|
-
##
|
|
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
|
|
346
400
|
|
|
347
401
|
```tsx
|
|
348
402
|
import React, { useState, useEffect } from 'react';
|
|
@@ -351,7 +405,7 @@ import {
|
|
|
351
405
|
PhoneInput,
|
|
352
406
|
ICountry,
|
|
353
407
|
} from 'react-native-international-phone-number';
|
|
354
|
-
import { Controller, FieldValues
|
|
408
|
+
import { Controller, FieldValues } from 'react-hook-form';
|
|
355
409
|
|
|
356
410
|
interface FormProps extends FieldValues {
|
|
357
411
|
phoneNumber: string;
|
|
@@ -362,9 +416,6 @@ export default function App() {
|
|
|
362
416
|
undefined | ICountry
|
|
363
417
|
>(undefined);
|
|
364
418
|
|
|
365
|
-
const { control, handleSubmit, setValue, watch } =
|
|
366
|
-
useForm<FormProps>();
|
|
367
|
-
|
|
368
419
|
function handleSelectedCountry(country: ICountry) {
|
|
369
420
|
setSelectedCountry(country);
|
|
370
421
|
}
|
|
@@ -376,12 +427,6 @@ export default function App() {
|
|
|
376
427
|
);
|
|
377
428
|
}
|
|
378
429
|
|
|
379
|
-
useEffect(() => {
|
|
380
|
-
const watchPhoneNumber = watch('phoneNumber');
|
|
381
|
-
|
|
382
|
-
setValue('phoneNumber', watchPhoneNumber);
|
|
383
|
-
}, []);
|
|
384
|
-
|
|
385
430
|
return (
|
|
386
431
|
<View style={{ width: '100%', flex: 1, padding: 24 }}>
|
|
387
432
|
<Controller
|
|
@@ -505,16 +550,17 @@ export default function App() {
|
|
|
505
550
|
## Component Props ([PhoneInputProps](https://github.com/AstrOOnauta/react-native-international-phone-number/blob/master/lib/interfaces/phoneInputProps.ts))
|
|
506
551
|
|
|
507
552
|
- `defaultValue?:` string;
|
|
508
|
-
- `value
|
|
509
|
-
- `onChangePhoneNumber
|
|
510
|
-
- `selectedCountry
|
|
511
|
-
- `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;
|
|
512
557
|
- `disabled?:` boolean;
|
|
513
558
|
- `modalDisabled?:` boolean;
|
|
514
559
|
- `withDarkTheme?:` boolean;
|
|
515
560
|
- `containerStyle?:` StyleProp<[ViewStyle](https://reactnative.dev/docs/view-style-props)>;
|
|
516
561
|
- `flagContainerStyle?:` StyleProp<[ViewStyle](https://reactnative.dev/docs/view-style-props)>;
|
|
517
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)>
|
|
518
564
|
|
|
519
565
|
<br>
|
|
520
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",
|