react-native-international-phone-number 0.4.3 → 0.4.5

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 CHANGED
@@ -29,23 +29,24 @@
29
29
 
30
30
  <br>
31
31
 
32
- ## Table of Contents
32
+ ## List of Contents
33
33
 
34
34
  - [Installation](#installation)
35
35
  - [Features](#features)
36
36
  - [Basic Usage](#basic-usage)
37
- - With Function Component
38
- - With Class Component
37
+ - [With Class Component](#class-component)
38
+ - [With Function Component](#function-component)
39
+ - [Custom Default Flag](#custom-default-flag)
39
40
  - [Basic Usage - Typescript](#basic-usage---typescript)
40
41
  - [Intermediate Usage - Typescrypt + Default Phone Number Value](#intermediate-usage---typescript--default-phone-number-value)
41
42
  - [Advanced Usage - React Hook Form + Typescript + Default Phone Number Value](#advanced-usage---react-hook-form--typescript--default-phone-number-value)
42
43
  - [Customizing Lib](#customizing-lib)
43
- - Theme Mode: Light (default) and Dark
44
- - Custom Lib Styles
45
- - Disabled Mode
46
- - Custom Disabled Mode Style
44
+ - [Dark Mode](#dark-mode)
45
+ - [Custom Lib Styles](#custom-lib-styles)
46
+ - [Disabled Mode](#disabled-mode)
47
+ - [Custom Disabled Mode Style](#custom-disabled-mode-style)
47
48
  - [Lib Props](#props)
48
- - [Functions](#functions)
49
+ - [Lib Functions](#functions)
49
50
  - [Contributing](#contributing)
50
51
  - [License](#license)
51
52
 
@@ -89,7 +90,59 @@ AND
89
90
 
90
91
  ## Basic Usage
91
92
 
92
- - Function Component:
93
+ - ### Class Component:
94
+
95
+ ```jsx
96
+ import React from 'react';
97
+ import { View, Text } from 'react-native';
98
+ import { PhoneInput } from 'react-native-international-phone-number';
99
+
100
+ export class App extends React.Component {
101
+ constructor(props) {
102
+ super(props)
103
+ this.state = {
104
+ selectedCountry: undefined,
105
+ inputValue: ''
106
+ }
107
+ }
108
+
109
+ function handleSelectedCountry(country) {
110
+ this.setState({
111
+ selectedCountry: country
112
+ })
113
+ }
114
+
115
+ function handleInputValue(phoneNumber) {
116
+ this.setState({
117
+ inputValue: phoneNumber
118
+ })
119
+ }
120
+
121
+ render(){
122
+ return (
123
+ <View style={{ width: '100%', flex: 1, padding: 24 }}>
124
+ <PhoneInput
125
+ value={this.state.inputValue}
126
+ onChangePhoneNumber={this.handleInputValue}
127
+ selectedCountry={this.state.selectedCountry}
128
+ onChangeSelectedCountry={this.handleSelectedCountry}
129
+ />
130
+ <View style={{ marginTop: 10 }}>
131
+ <Text>
132
+ Country:{' '}
133
+ {`${this.state.selectedCountry?.name} (${this.state.selectedCountry?.cca2})`}
134
+ </Text>
135
+ <Text>
136
+ Phone Number: {`${this.state.selectedCountry?.callingCode} ${this.state.inputValue}`}
137
+ </Text>
138
+ </View>
139
+ </View>
140
+ );
141
+ }
142
+ }
143
+ ```
144
+
145
+ - ### Function Component:
93
146
 
94
147
  ```jsx
95
148
  import React, { useState } from 'react';
@@ -131,55 +184,50 @@ export default function App() {
131
184
  }
132
185
  ```
133
186
 
134
- - Class Component:
187
+ - ### Custom Default Flag:
135
188
 
136
189
  ```jsx
137
- import React from 'react';
190
+ import React, { useState } from 'react';
138
191
  import { View, Text } from 'react-native';
139
- import { PhoneInput } from 'react-native-international-phone-number';
192
+ import {
193
+ PhoneInput,
194
+ getCountryByCca2,
195
+ } from 'react-native-international-phone-number';
140
196
 
141
- export class App extends React.Component {
142
- constructor(props) {
143
- super(props)
144
- this.state = {
145
- selectedCountry: undefined,
146
- inputValue: ''
147
- }
148
- }
197
+ export default function App() {
198
+ const [selectedCountry, setSelectedCountry] = useState(
199
+ getCountryByCca2('CA') // <--- In this exemple, returns the CANADA Country and PhoneInput CANADA Flag
200
+ );
201
+ const [inputValue, setInputValue] = useState('');
149
202
 
150
- function handleSelectedCountry(country) {
151
- this.setState({
152
- selectedCountry: country
153
- })
203
+ function handleInputValue(phoneNumber) {
204
+ setInputValue(phoneNumber);
154
205
  }
155
206
 
156
- function handleInputValue(phoneNumber) {
157
- this.setState({
158
- inputValue: phoneNumber
159
- })
207
+ function handleSelectedCountry(country) {
208
+ setSelectedCountry(country);
160
209
  }
161
210
 
162
- render(){
163
- return (
164
- <View style={{ width: '100%', flex: 1, padding: 24 }}>
165
- <PhoneInput
166
- value={this.inputValue}
167
- onChangePhoneNumber={this.handleInputValue}
168
- selectedCountry={this.selectedCountry}
169
- onChangeSelectedCountry={this.handleSelectedCountry}
170
- />
171
- <View style={{ marginTop: 10 }}>
172
- <Text>
173
- Country:{' '}
174
- {`${this.selectedCountry?.name} (${this.selectedCountry?.cca2})`}
175
- </Text>
176
- <Text>
177
- Phone Number: {`${this.selectedCountry?.callingCode} ${this.inputValue}`}
178
- </Text>
179
- </View>
211
+ return (
212
+ <View style={{ width: '100%', flex: 1, padding: 24 }}>
213
+ <PhoneInput
214
+ value={inputValue}
215
+ onChangePhoneNumber={handleInputValue}
216
+ selectedCountry={selectedCountry}
217
+ onChangeSelectedCountry={handleSelectedCountry}
218
+ />
219
+ <View style={{ marginTop: 10 }}>
220
+ <Text>
221
+ Country:{' '}
222
+ {`${selectedCountry?.name} (${selectedCountry?.cca2})`}
223
+ </Text>
224
+ <Text>
225
+ Phone Number:{' '}
226
+ {`${selectedCountry?.callingCode} ${inputValue}`}
227
+ </Text>
180
228
  </View>
181
- );
182
- }
229
+ </View>
230
+ );
183
231
  }
184
232
  ```
185
233
 
@@ -378,7 +426,7 @@ export default function App() {
378
426
 
379
427
  ## Customizing lib
380
428
 
381
- - Dark Mode:
429
+ - ### Dark Mode:
382
430
 
383
431
  ```jsx
384
432
  <PhoneInput
@@ -387,7 +435,7 @@ export default function App() {
387
435
  />
388
436
  ```
389
437
 
390
- - Custom Lib Styles:
438
+ - ### Custom Lib Styles:
391
439
 
392
440
  <div>
393
441
  <img src="https://github.com/AstrOOnauta/react-native-international-phone-number/blob/master/images/custom-styles.png">
@@ -415,7 +463,7 @@ export default function App() {
415
463
  />
416
464
  ```
417
465
 
418
- - Disabled Mode:
466
+ - ### Disabled Mode:
419
467
 
420
468
  ```jsx
421
469
  <PhoneInput
@@ -424,7 +472,7 @@ export default function App() {
424
472
  />
425
473
  ```
426
474
 
427
- - Custom Disabled Mode Style:
475
+ - ### Custom Disabled Mode Style:
428
476
 
429
477
  ```jsx
430
478
  ...
@@ -458,6 +506,9 @@ export default function App() {
458
506
  ## Functions
459
507
 
460
508
  - `getCountryByPhoneNumber:` (phoneNumber: string) => ICountry | undefined;
509
+ - `getCountryByCca2:` (phoneNumber: string) => ICountry | undefined;
510
+ - `getCountriesByCallingCode:` (phoneNumber: string) => ICountry[] | undefined;
511
+ - `getCountriesByName:` (phoneNumber: string) => ICountry[] | undefined;
461
512
 
462
513
  </br>
463
514
 
package/lib/index.d.ts CHANGED
@@ -33,4 +33,23 @@ declare function getCountryByPhoneNumber(
33
33
  phoneNumber: string
34
34
  ): ICountry | undefined;
35
35
 
36
- export { PhoneInput, ICountry, getCountryByPhoneNumber };
36
+ declare function getCountryByCca2(
37
+ phoneNumber: string
38
+ ): ICountry | undefined;
39
+
40
+ declare function getCountriesByCallingCode(
41
+ phoneNumber: string
42
+ ): ICountry[] | undefined;
43
+
44
+ declare function getCountriesByName(
45
+ phoneNumber: string
46
+ ): ICountry[] | undefined;
47
+
48
+ export {
49
+ PhoneInput,
50
+ ICountry,
51
+ getCountryByPhoneNumber,
52
+ getCountryByCca2,
53
+ getCountriesByCallingCode,
54
+ getCountriesByName,
55
+ };
package/lib/index.js CHANGED
@@ -10,6 +10,9 @@ import CountryPicker, {
10
10
  DEFAULT_THEME,
11
11
  } from 'react-native-country-picker-modal';
12
12
 
13
+ import getCountriesByName from './utils/getCountriesByName';
14
+ import getCountriesByCallingCode from './utils/getCountriesByCallingCode';
15
+ import getCountryByCca2 from './utils/getCountryByCca2';
13
16
  import getCountryByPhoneNumber from './utils/getCountryByPhoneNumber';
14
17
  import phoneMask from './utils/inputMask';
15
18
 
@@ -248,4 +251,10 @@ const styles = StyleSheet.create({
248
251
  },
249
252
  });
250
253
 
251
- export { PhoneInput, getCountryByPhoneNumber };
254
+ export {
255
+ PhoneInput,
256
+ getCountryByPhoneNumber,
257
+ getCountryByCca2,
258
+ getCountriesByCallingCode,
259
+ getCountriesByName,
260
+ };
@@ -0,0 +1,24 @@
1
+ import {countries} from './countries';
2
+
3
+ export default function getCountriesByCallingCode(callingCode) {
4
+ const formatedCallingCode = String(callingCode).includes('+')
5
+ ? callingCode
6
+ : `+${callingCode}`;
7
+
8
+ const matchingCountries = countries.filter(country => {
9
+ return country.callingCode === formatedCallingCode;
10
+ });
11
+
12
+ const formatedCountries = [];
13
+
14
+ matchingCountries.forEach(country => {
15
+ formatedCountries.push({
16
+ name: country.name,
17
+ cca2: country.cca2,
18
+ flag: country.flag,
19
+ callingCode: country.callingCode,
20
+ });
21
+ });
22
+
23
+ return formatedCountries;
24
+ }
@@ -0,0 +1,20 @@
1
+ import {countries} from './countries';
2
+
3
+ export default function getCountriesByName(name) {
4
+ const matchingCountries = countries.filter(country => {
5
+ return country.name.includes(name);
6
+ });
7
+
8
+ const formatedCountries = [];
9
+
10
+ matchingCountries.forEach(country => {
11
+ formatedCountries.push({
12
+ name: country.name,
13
+ cca2: country.cca2,
14
+ flag: country.flag,
15
+ callingCode: country.callingCode,
16
+ });
17
+ });
18
+
19
+ return formatedCountries;
20
+ }
@@ -0,0 +1,20 @@
1
+ import {countries} from './countries';
2
+
3
+ export default function getCountryByCca2(cca2) {
4
+ let matchingCountry = countries.filter(country => {
5
+ return country.cca2.toLowerCase() === cca2.toLowerCase();
6
+ })[0];
7
+
8
+ if (matchingCountry) {
9
+ matchingCountry = {
10
+ name: matchingCountry.name,
11
+ cca2: matchingCountry.cca2,
12
+ flag: matchingCountry.flag,
13
+ callingCode: matchingCountry.callingCode,
14
+ };
15
+
16
+ return matchingCountry;
17
+ } else {
18
+ return {};
19
+ }
20
+ }
@@ -49,12 +49,14 @@ export default function getCountryByPhoneNumber(phoneNumber) {
49
49
  }
50
50
  }
51
51
 
52
- matchingCountry = {
53
- name: matchingCountry.name,
54
- cca2: matchingCountry.cca2,
55
- flag: matchingCountry.flag,
56
- callingCode: matchingCountry.callingCode,
57
- };
52
+ if (matchingCountry) {
53
+ matchingCountry = {
54
+ name: matchingCountry.name,
55
+ cca2: matchingCountry.cca2,
56
+ flag: matchingCountry.flag,
57
+ callingCode: matchingCountry.callingCode,
58
+ };
59
+ }
58
60
 
59
61
  return matchingCountry;
60
62
  }
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.3",
4
+ "version": "0.4.5",
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",