react-native-international-phone-number 0.2.6 → 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 +58 -7
- 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
|
@@ -49,9 +49,15 @@ AND
|
|
|
49
49
|
> };
|
|
50
50
|
> ```
|
|
51
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
|
+
|
|
52
58
|
## Basic Usage
|
|
53
59
|
|
|
54
|
-
```
|
|
60
|
+
```jsx
|
|
55
61
|
import React, { useState } from 'react';
|
|
56
62
|
import { View, Text } from 'react-native';
|
|
57
63
|
import {
|
|
@@ -89,10 +95,53 @@ export default function App() {
|
|
|
89
95
|
}
|
|
90
96
|
```
|
|
91
97
|
|
|
92
|
-
##
|
|
98
|
+
## Basic Usage - Typescript
|
|
93
99
|
|
|
94
100
|
```tsx
|
|
95
101
|
import React, { useState } from 'react';
|
|
102
|
+
import { View, Text } from 'react-native';
|
|
103
|
+
import {
|
|
104
|
+
phoneMask,
|
|
105
|
+
PhoneInput,
|
|
106
|
+
} from 'react-native-international-phone-number';
|
|
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';
|
|
96
145
|
import { View, Text, Alert } from 'react-native';
|
|
97
146
|
import {
|
|
98
147
|
phoneMask,
|
|
@@ -106,9 +155,7 @@ interface FormProps extends FieldValues {
|
|
|
106
155
|
}
|
|
107
156
|
|
|
108
157
|
export default function App() {
|
|
109
|
-
const [selectedCountry, setSelectedCountry] = useState<
|
|
110
|
-
undefined | Country
|
|
111
|
-
>(undefined);
|
|
158
|
+
const [selectedCountry, setSelectedCountry] = useState<undefined | Country>(undefined);
|
|
112
159
|
|
|
113
160
|
const { control, handleSubmit, resetField } = useForm();
|
|
114
161
|
|
|
@@ -120,6 +167,10 @@ export default function App() {
|
|
|
120
167
|
resetField('phoneNumber');
|
|
121
168
|
}
|
|
122
169
|
|
|
170
|
+
useEffect(()=>{
|
|
171
|
+
resetField('phoneNumber')
|
|
172
|
+
},[selectedCountry])
|
|
173
|
+
|
|
123
174
|
return (
|
|
124
175
|
<View style={{ width: '100%', flex: 1, padding: 24 }}>
|
|
125
176
|
<Controller
|
|
@@ -132,7 +183,7 @@ export default function App() {
|
|
|
132
183
|
value={value}
|
|
133
184
|
onChangeText={(newValue) =>
|
|
134
185
|
onChange(
|
|
135
|
-
phoneMask(newValue, selectedCountry
|
|
186
|
+
phoneMask(newValue, selectedCountry?.callingCode[0])
|
|
136
187
|
)
|
|
137
188
|
}
|
|
138
189
|
/>
|
|
@@ -166,7 +217,7 @@ export default function App() {
|
|
|
166
217
|
|
|
167
218
|
## Methods
|
|
168
219
|
|
|
169
|
-
- `phoneMask`: (
|
|
220
|
+
- `phoneMask`: (phoneNumber: string, callingCode: string) => string
|
|
170
221
|
|
|
171
222
|
## Contributing
|
|
172
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",
|