react-native-international-phone-number 0.4.14 → 0.4.15

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
@@ -48,6 +48,7 @@
48
48
  - [With Function Component](#function-component)
49
49
  - [Custom Default Flag](#custom-default-flag)
50
50
  - [Default Phone Number Value](#default-phone-number-value)
51
+ - [Custom Phone Mask](#custom-phone-mask)
51
52
  - [Typescript](#typescript)
52
53
  - [Intermediate Usage](#intermediate-usage)
53
54
  - [Typescript + useRef](#typescript--useref)
@@ -293,6 +294,49 @@ export default function App() {
293
294
  > 1. You need to use a default value with the following format: `+(country callling code)(area code)(number phone)`
294
295
  > 2. The lib has the mechanism to set the flag and mask of the supplied `defaultValue`. However, if the supplied `defaultValue` does not match any international standard, the `input mask of the defaultValue` will be set to "BR" (please make sure that the default value is in the format mentioned above).
295
296
 
297
+ - ### Custom Phone Mask
298
+
299
+ ```tsx
300
+ import React, { useState } from 'react';
301
+ import { View, Text } from 'react-native';
302
+ import { PhoneInput } from 'react-native-international-phone-number';
303
+
304
+ export default function App() {
305
+ const [selectedCountry, setSelectedCountry] = useState(undefined);
306
+ const [inputValue, setInputValue] = useState('');
307
+
308
+ function handleInputValue(phoneNumber) {
309
+ setInputValue(phoneNumber);
310
+ }
311
+
312
+ function handleSelectedCountry(country) {
313
+ setSelectedCountry(country);
314
+ }
315
+
316
+ return (
317
+ <View style={{ width: '100%', flex: 1, padding: 24 }}>
318
+ <PhoneInput
319
+ customMask={['#### ####', '##### ####']}
320
+ value={inputValue}
321
+ onChangePhoneNumber={handleInputValue}
322
+ selectedCountry={selectedCountry}
323
+ onChangeSelectedCountry={handleSelectedCountry}
324
+ />
325
+ <View style={{ marginTop: 10 }}>
326
+ <Text>
327
+ Country:{' '}
328
+ {`${selectedCountry?.name} (${selectedCountry?.cca2})`}
329
+ </Text>
330
+ <Text>
331
+ Phone Number:{' '}
332
+ {`${selectedCountry?.callingCode} ${inputValue}`}
333
+ </Text>
334
+ </View>
335
+ </View>
336
+ );
337
+ }
338
+ ```
339
+
296
340
  - ### Typescript
297
341
 
298
342
  ```tsx
@@ -551,6 +595,7 @@ export default function App() {
551
595
 
552
596
  ## Component Props ([PhoneInputProps](https://github.com/AstrOOnauta/react-native-international-phone-number/blob/master/lib/interfaces/phoneInputProps.ts))
553
597
 
598
+ - `customMask?:` string[];
554
599
  - `defaultValue?:` string;
555
600
  - `value?:` string;
556
601
  - `onChangePhoneNumber?:` (phoneNumber: string) => void;
package/lib/index.js CHANGED
@@ -38,6 +38,7 @@ const PhoneInput = forwardRef(
38
38
  onChangePhoneNumber,
39
39
  selectedCountry,
40
40
  onChangeSelectedCountry,
41
+ customMask,
41
42
  ...rest
42
43
  },
43
44
  ref
@@ -122,7 +123,8 @@ const PhoneInput = forwardRef(
122
123
  const res = phoneMask(
123
124
  phoneNumber,
124
125
  callingCode ? callingCode : countryValue?.callingCode,
125
- countryValue?.cca2
126
+ countryValue?.cca2,
127
+ customMask ? customMask : null,
126
128
  );
127
129
 
128
130
  if (ref) {
@@ -24,6 +24,7 @@ interface IPhoneInputPropsWithoutRef extends TextInputProps {
24
24
  selectedCountry: undefined | ICountry;
25
25
  onChangeSelectedCountry: (country: ICountry) => void;
26
26
  ref?: never;
27
+ customMask?:Array<string>;
27
28
  }
28
29
 
29
30
  interface IPhoneInputPropsWithRef extends TextInputProps {
@@ -41,6 +42,7 @@ interface IPhoneInputPropsWithRef extends TextInputProps {
41
42
  selectedCountry?: never;
42
43
  onChangeSelectedCountry?: never;
43
44
  ref: Ref<IPhoneInputRef>;
45
+ customMask?:Array<string>;
44
46
  }
45
47
 
46
48
  export type PhoneInputProps =
@@ -1,22 +1,23 @@
1
1
  import { countries } from './countries';
2
2
 
3
- export default function phoneMask(phoneNumber, callingCode, cca2) {
3
+ export default function phoneMask(phoneNumber, callingCode, cca2, customMask) {
4
4
  let matrix = '';
5
5
 
6
6
  countries.forEach((item) => {
7
7
  const newCode = item.callingCode.replace(/[\s#]/g, '');
8
+ const phoneMask = Array.isArray(customMask) && customMask || item.phoneMasks;
8
9
 
9
10
  if (callingCode && callingCode.includes(newCode)) {
10
- if (item.phoneMasks.length === 1) {
11
+ if (phoneMask.length === 1) {
11
12
  if (cca2 !== 'CA' && cca2 !== 'US' && cca2 !== 'IT') {
12
- matrix = item.phoneMasks[0].replace(/[0-9]/g, '').trim();
13
+ matrix = phoneMask[0].replace(/[0-9]/g, '').trim();
13
14
  }
14
- } else if (item.phoneMasks.length > 1) {
15
+ } else if (phoneMask.length > 1) {
15
16
  let hasDifferentLengthsOfPhoneNumbers = false;
16
17
 
17
- for (let i = 0; i < item.phoneMasks.length; i++) {
18
+ for (let i = 0; i < phoneMask.length; i++) {
18
19
  if (
19
- phoneNumber.length > item.phoneMasks[0].length &&
20
+ phoneNumber.length > phoneMask[0].length &&
20
21
  newCode !== '+1'
21
22
  ) {
22
23
  hasDifferentLengthsOfPhoneNumbers = true;
@@ -25,17 +26,17 @@ export default function phoneMask(phoneNumber, callingCode, cca2) {
25
26
 
26
27
  if (!hasDifferentLengthsOfPhoneNumbers) {
27
28
  if (cca2 === 'CA' || cca2 === 'US') {
28
- matrix = item.phoneMasks[0].replace(/\d/g, '#').trim();
29
+ matrix = phoneMask[0].replace(/\d/g, '#').trim();
29
30
  } else {
30
- matrix = item.phoneMasks[0].replace(/[0-9]/g, '').trim();
31
+ matrix = phoneMask[0].replace(/[0-9]/g, '').trim();
31
32
  }
32
33
  } else {
33
- for (let i = 0; i < item.phoneMasks.length; i++) {
34
+ for (let i = 0; i < phoneMask.length; i++) {
34
35
  if (
35
- phoneNumber.length > item.phoneMasks[i].length &&
36
- item.phoneMasks[i + 1]
36
+ phoneNumber.length > phoneMask[i].length &&
37
+ phoneMask[i + 1]
37
38
  ) {
38
- matrix = item.phoneMasks[i + 1]
39
+ matrix = phoneMask[i + 1]
39
40
  .replace(/[0-9]/g, '')
40
41
  .trim();
41
42
  }
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.14",
4
+ "version": "0.4.15",
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",