react-native-international-phone-number 0.10.3 → 0.10.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/lib/index.js CHANGED
@@ -201,15 +201,21 @@ const PhoneInput = forwardRef(
201
201
  }
202
202
  }
203
203
 
204
- return result;
204
+ if (ref) {
205
+ setInputValue(result);
206
+ updateRef(result, countryValue);
207
+ } else {
208
+ onChangePhoneNumber(result);
209
+ }
205
210
  }
206
211
 
207
212
  function formatPhoneNumber(phoneNumber, callingCode) {
208
213
  let formattedNumber = '';
209
214
 
210
215
  const metadata = new Metadata();
211
- metadata.selectNumberingPlan(selectedCountry.cca2);
212
- const possibleLengths = metadata.possibleLengths();
216
+ metadata.selectNumberingPlan(selectedCountry?.cca2);
217
+
218
+ const possibleLengths = selectedCountry ? metadata.possibleLengths() : [];
213
219
 
214
220
  let validCallingCode = callingCode
215
221
  ? callingCode
@@ -234,10 +240,15 @@ const PhoneInput = forwardRef(
234
240
  : possibleLengths.slice(-1)[0];
235
241
 
236
242
  if (formattedNumber?.replace(/\D/g, '')?.length > possibleLength) {
237
- return phoneNumber.slice(0, -1);
243
+ return;
238
244
  }
239
245
 
240
- return formattedNumber;
246
+ if (ref) {
247
+ setInputValue(formattedNumber);
248
+ updateRef(formattedNumber, countryValue);
249
+ } else {
250
+ onChangePhoneNumber(formattedNumber);
251
+ }
241
252
  }
242
253
 
243
254
  function onChangeText(phoneNumber, callingCode) {
@@ -263,20 +274,10 @@ const PhoneInput = forwardRef(
263
274
  return;
264
275
  }
265
276
 
266
- let formattedNumber = '';
267
-
268
277
  if (customMask) {
269
- formattedNumber = formatPhoneNumberWithCustomMask(phoneNumber);
270
- } else {
271
- formattedNumber = formatPhoneNumber(phoneNumber, callingCode);
272
- }
273
-
274
- if (ref) {
275
- setInputValue(formattedNumber);
276
- updateRef(formattedNumber, countryValue);
277
- } else {
278
- onChangePhoneNumber(formattedNumber);
278
+ return formatPhoneNumberWithCustomMask(phoneNumber);
279
279
  }
280
+ formatPhoneNumber(phoneNumber, callingCode);
280
281
  }
281
282
 
282
283
  useEffect(() => {
@@ -1,10 +1,22 @@
1
1
  import parsePhoneNumber from 'libphonenumber-js';
2
- import {getCountryByCca2} from 'react-native-country-select';
2
+ import {
3
+ getCountryByCca2,
4
+ getCountriesByCallingCode,
5
+ } from 'react-native-country-select';
3
6
 
4
7
  export default function getCountryByPhoneNumber(phoneNumber) {
5
- const matchingCountry = getCountryByCca2(
6
- parsePhoneNumber(phoneNumber)?.getPossibleCountries()[0],
7
- );
8
+ const country = parsePhoneNumber(phoneNumber)?.getPossibleCountries()[0];
9
+ let matchingCountry = getCountryByCca2(country);
10
+
11
+ if (!matchingCountry) {
12
+ const callingCode = parsePhoneNumber(
13
+ phoneNumber,
14
+ )?.countryCallingCode.includes('+')
15
+ ? parsePhoneNumber(phoneNumber)?.countryCallingCode
16
+ : `+${parsePhoneNumber(phoneNumber)?.countryCallingCode}`;
17
+
18
+ matchingCountry = getCountriesByCallingCode(callingCode)[0];
19
+ }
8
20
 
9
21
  return matchingCountry;
10
22
  }
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.10.3",
4
+ "version": "0.10.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",