react-native-international-phone-number 0.2.6 → 0.2.8
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 -7
- package/lib/index.d.ts +14 -4
- package/lib/index.js +4 -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
|
/>
|
|
@@ -153,6 +204,35 @@ export default function App() {
|
|
|
153
204
|
}
|
|
154
205
|
```
|
|
155
206
|
|
|
207
|
+
## Customizing lib
|
|
208
|
+
|
|
209
|
+
### Dark mode:
|
|
210
|
+
|
|
211
|
+
```jsx
|
|
212
|
+
<PhoneInput
|
|
213
|
+
...
|
|
214
|
+
withDarkTheme
|
|
215
|
+
/>
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### containerStyle + inputStyle:
|
|
219
|
+
|
|
220
|
+
```jsx
|
|
221
|
+
<PhoneInput
|
|
222
|
+
...
|
|
223
|
+
inputStyle={{
|
|
224
|
+
color: '#F3F3F3',
|
|
225
|
+
}}
|
|
226
|
+
containerStyle={{
|
|
227
|
+
backgroundColor: '#575757',
|
|
228
|
+
borderWidth: 1,
|
|
229
|
+
borderStyle: 'solid',
|
|
230
|
+
borderColor: '#F3F3F3',
|
|
231
|
+
marginVertical: 16,
|
|
232
|
+
}}
|
|
233
|
+
/>
|
|
234
|
+
```
|
|
235
|
+
|
|
156
236
|
## Props
|
|
157
237
|
|
|
158
238
|
- `value?`: string
|
|
@@ -166,7 +246,7 @@ export default function App() {
|
|
|
166
246
|
|
|
167
247
|
## Methods
|
|
168
248
|
|
|
169
|
-
- `phoneMask`: (
|
|
249
|
+
- `phoneMask`: (phoneNumber: string, callingCode: string) => string
|
|
170
250
|
|
|
171
251
|
## Contributing
|
|
172
252
|
|
package/lib/index.d.ts
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
StyleProp,
|
|
4
|
+
TextInputProps,
|
|
5
|
+
TextStyle,
|
|
6
|
+
ViewStyle,
|
|
7
|
+
} from 'react-native';
|
|
3
8
|
import { Country } from 'react-native-country-picker-modal';
|
|
4
9
|
|
|
5
10
|
interface PhoneInputProps extends TextInputProps {
|
|
6
11
|
placeholder?: string;
|
|
7
12
|
placeholderTextColor?: string;
|
|
8
13
|
containerStyle?: StyleProp<ViewStyle>;
|
|
9
|
-
inputStyle?: StyleProp<
|
|
14
|
+
inputStyle?: StyleProp<TextStyle>;
|
|
10
15
|
withDarkTheme?: boolean;
|
|
11
16
|
disabled?: boolean;
|
|
12
|
-
selectedCountry: Country;
|
|
17
|
+
selectedCountry: undefined | Country;
|
|
13
18
|
setSelectedCountry: Dispatch<SetStateAction<undefined | Country>>;
|
|
14
19
|
}
|
|
15
20
|
|
|
16
21
|
declare function PhoneInput(props: PhoneInputProps): JSX.Element;
|
|
17
22
|
|
|
18
|
-
|
|
23
|
+
declare function phoneMask(
|
|
24
|
+
value: string,
|
|
25
|
+
countryCode?: string
|
|
26
|
+
): string;
|
|
27
|
+
|
|
28
|
+
export { PhoneInput, phoneMask };
|
package/lib/index.js
CHANGED
|
@@ -46,7 +46,9 @@ function PhoneInput({
|
|
|
46
46
|
}, []);
|
|
47
47
|
|
|
48
48
|
return (
|
|
49
|
-
<View
|
|
49
|
+
<View
|
|
50
|
+
style={[styles.container, containerStyle ? containerStyle : {}]}
|
|
51
|
+
>
|
|
50
52
|
<CountryPicker
|
|
51
53
|
containerButtonStyle={{
|
|
52
54
|
paddingLeft: 20,
|
|
@@ -58,7 +60,7 @@ function PhoneInput({
|
|
|
58
60
|
countryCode={selectedCountry ? selectedCountry.cca2 : 'BR'}
|
|
59
61
|
/>
|
|
60
62
|
<TextInput
|
|
61
|
-
style={inputStyle ? inputStyle :
|
|
63
|
+
style={[styles.input, inputStyle ? inputStyle : {}]}
|
|
62
64
|
placeholder={
|
|
63
65
|
placeholder ? placeholder : 'Insert your phone number'
|
|
64
66
|
}
|
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.8",
|
|
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",
|