react-native-international-phone-number 0.2.5 → 0.2.7
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 +63 -9
- package/lib/index.d.ts +7 -2
- package/lib/utils/inputMask.js +3 -3
- package/package.json +1 -1
- package/lib/utils/inputMask.d.ts +0 -6
package/README.md
CHANGED
|
@@ -13,10 +13,13 @@
|
|
|
13
13
|
<a href="https://www.npmjs.com/package/react-native-international-phone-number">
|
|
14
14
|
<img src="https://img.shields.io/npm/dm/react-native-international-phone-number.svg?style=flat-square">
|
|
15
15
|
</a>
|
|
16
|
-
<a href="https://
|
|
16
|
+
<a href="https://github.com/AstrOOnauta/react-native-international-phone-number">
|
|
17
|
+
<img src="https://img.shields.io/github/stars/AstrOOnauta/react-native-international-phone-number"/>
|
|
18
|
+
</a>
|
|
19
|
+
<a href="https://github.com/AstrOOnauta/react-native-international-phone-number/issues">
|
|
17
20
|
<img src="https://img.shields.io/github/issues/AstrOOnauta/react-native-international-phone-number"/>
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
22
|
+
<a href="https://github.com/AstrOOnauta/react-native-international-phone-number/pulls">
|
|
20
23
|
<img src="https://img.shields.io/github/issues-pr/AstrOOnauta/react-native-international-phone-number"/>
|
|
21
24
|
</a>
|
|
22
25
|
</p>
|
|
@@ -46,9 +49,15 @@ AND
|
|
|
46
49
|
> };
|
|
47
50
|
> ```
|
|
48
51
|
|
|
52
|
+
## Features
|
|
53
|
+
|
|
54
|
+
- 📱 Works with iOS, Android (Cross-platform) and Expo;
|
|
55
|
+
- 🎨 Lib with UI customizable
|
|
56
|
+
- 🌎 Phone Input Mask according to the selected country.
|
|
57
|
+
|
|
49
58
|
## Basic Usage
|
|
50
59
|
|
|
51
|
-
```
|
|
60
|
+
```jsx
|
|
52
61
|
import React, { useState } from 'react';
|
|
53
62
|
import { View, Text } from 'react-native';
|
|
54
63
|
import {
|
|
@@ -86,7 +95,7 @@ export default function App() {
|
|
|
86
95
|
}
|
|
87
96
|
```
|
|
88
97
|
|
|
89
|
-
##
|
|
98
|
+
## Basic Usage - Typescript
|
|
90
99
|
|
|
91
100
|
```tsx
|
|
92
101
|
import React, { useState } from 'react';
|
|
@@ -96,6 +105,49 @@ import {
|
|
|
96
105
|
PhoneInput,
|
|
97
106
|
} from 'react-native-international-phone-number';
|
|
98
107
|
import { Country } from 'react-native-country-picker-modal';
|
|
108
|
+
|
|
109
|
+
export default function App() {
|
|
110
|
+
const [selectedCountry, setSelectedCountry] = useState<
|
|
111
|
+
undefined | Country
|
|
112
|
+
>(undefined);
|
|
113
|
+
const [phoneInput, setPhoneInput] = useState<string>('');
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<View style={{ width: '100%', flex: 1, padding: 24 }}>
|
|
117
|
+
<PhoneInput
|
|
118
|
+
selectedCountry={selectedCountry}
|
|
119
|
+
setSelectedCountry={setSelectedCountry}
|
|
120
|
+
value={phoneInput}
|
|
121
|
+
onChangeText={(newValue) =>
|
|
122
|
+
setPhoneInput(
|
|
123
|
+
phoneMask(newValue, selectedCountry?.callingCode[0])
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
/>
|
|
127
|
+
<View style={{ marginTop: 10 }}>
|
|
128
|
+
<Text>
|
|
129
|
+
Country:{' '}
|
|
130
|
+
{`${selectedCountry?.name} (${selectedCountry?.cca2})`}
|
|
131
|
+
</Text>
|
|
132
|
+
<Text>
|
|
133
|
+
Phone: {`${selectedCountry?.callingCode[0]} ${phoneInput}`}
|
|
134
|
+
</Text>
|
|
135
|
+
</View>
|
|
136
|
+
</View>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Advanced Usage - React-Hook-Form and Typescript
|
|
142
|
+
|
|
143
|
+
```tsx
|
|
144
|
+
import React, { useState, useEffect } from 'react';
|
|
145
|
+
import { View, Text, Alert } from 'react-native';
|
|
146
|
+
import {
|
|
147
|
+
phoneMask,
|
|
148
|
+
PhoneInput,
|
|
149
|
+
} from 'react-native-international-phone-number';
|
|
150
|
+
import { Country } from 'react-native-country-picker-modal';
|
|
99
151
|
import { Controller, FieldValues, useForm } from 'react-hook-form';
|
|
100
152
|
|
|
101
153
|
interface FormProps extends FieldValues {
|
|
@@ -103,9 +155,7 @@ interface FormProps extends FieldValues {
|
|
|
103
155
|
}
|
|
104
156
|
|
|
105
157
|
export default function App() {
|
|
106
|
-
const [selectedCountry, setSelectedCountry] = useState<
|
|
107
|
-
undefined | Country
|
|
108
|
-
>(undefined);
|
|
158
|
+
const [selectedCountry, setSelectedCountry] = useState<undefined | Country>(undefined);
|
|
109
159
|
|
|
110
160
|
const { control, handleSubmit, resetField } = useForm();
|
|
111
161
|
|
|
@@ -117,6 +167,10 @@ export default function App() {
|
|
|
117
167
|
resetField('phoneNumber');
|
|
118
168
|
}
|
|
119
169
|
|
|
170
|
+
useEffect(()=>{
|
|
171
|
+
resetField('phoneNumber')
|
|
172
|
+
},[selectedCountry])
|
|
173
|
+
|
|
120
174
|
return (
|
|
121
175
|
<View style={{ width: '100%', flex: 1, padding: 24 }}>
|
|
122
176
|
<Controller
|
|
@@ -129,7 +183,7 @@ export default function App() {
|
|
|
129
183
|
value={value}
|
|
130
184
|
onChangeText={(newValue) =>
|
|
131
185
|
onChange(
|
|
132
|
-
phoneMask(newValue, selectedCountry
|
|
186
|
+
phoneMask(newValue, selectedCountry?.callingCode[0])
|
|
133
187
|
)
|
|
134
188
|
}
|
|
135
189
|
/>
|
|
@@ -163,7 +217,7 @@ export default function App() {
|
|
|
163
217
|
|
|
164
218
|
## Methods
|
|
165
219
|
|
|
166
|
-
- `phoneMask`: (
|
|
220
|
+
- `phoneMask`: (phoneNumber: string, callingCode: string) => string
|
|
167
221
|
|
|
168
222
|
## Contributing
|
|
169
223
|
|
package/lib/index.d.ts
CHANGED
|
@@ -9,10 +9,15 @@ interface PhoneInputProps extends TextInputProps {
|
|
|
9
9
|
inputStyle?: StyleProp<ViewStyle>;
|
|
10
10
|
withDarkTheme?: boolean;
|
|
11
11
|
disabled?: boolean;
|
|
12
|
-
selectedCountry: Country;
|
|
12
|
+
selectedCountry: undefined | Country;
|
|
13
13
|
setSelectedCountry: Dispatch<SetStateAction<undefined | Country>>;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
declare function PhoneInput(props: PhoneInputProps): JSX.Element;
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
declare function phoneMask(
|
|
19
|
+
value: string,
|
|
20
|
+
countryCode?: string
|
|
21
|
+
): string;
|
|
22
|
+
|
|
23
|
+
export { PhoneInput, phoneMask };
|
package/lib/utils/inputMask.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { countriesPhoneCodeList } from './countriesPhoneCode';
|
|
2
2
|
|
|
3
|
-
export const phoneMask = (
|
|
3
|
+
export const phoneMask = (phoneNumber, callingCode) => {
|
|
4
4
|
let matrix = '## ##### ####';
|
|
5
5
|
|
|
6
6
|
countriesPhoneCodeList.forEach((item) => {
|
|
7
7
|
const newCode = item.code.replace(/[\s#]/g, '');
|
|
8
8
|
|
|
9
|
-
if (
|
|
9
|
+
if (callingCode && callingCode.includes(newCode)) {
|
|
10
10
|
matrix = item.matrix;
|
|
11
11
|
}
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
let i = 0;
|
|
15
|
-
const newValue =
|
|
15
|
+
const newValue = phoneNumber.replace(/\D/g, '');
|
|
16
16
|
|
|
17
17
|
return matrix.replace(/(?!\+)./g, function (a) {
|
|
18
18
|
return /[#\d]/.test(a) && i < newValue.length
|
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.2.
|
|
4
|
+
"version": "0.2.7",
|
|
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",
|