react-native-international-phone-number 0.11.4 → 0.11.6

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.
Files changed (2) hide show
  1. package/lib/index.js +146 -159
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -5,6 +5,7 @@ import React, {
5
5
  useState,
6
6
  useRef,
7
7
  forwardRef,
8
+ useImperativeHandle,
8
9
  } from 'react';
9
10
  import {
10
11
  View,
@@ -122,121 +123,108 @@ const PhoneInput = forwardRef(
122
123
 
123
124
  const textInputRef = useRef(null);
124
125
 
125
- const refBase = {
126
- ...textInputRef.current,
127
- onFocus: () => textInputRef.current?.focus(),
128
- focus: () => textInputRef.current?.focus(),
129
- blur: () => textInputRef.current?.blur(),
130
- clear: () => textInputRef.current?.clear(),
131
- isFocused: () => textInputRef.current?.isFocused(),
132
- setNativeProps: (nativeProps) =>
133
- textInputRef.current?.setNativeProps(nativeProps),
134
- measure: (callback) => textInputRef.current?.measure(callback),
135
- measureInWindow: (callback) =>
136
- textInputRef.current?.measureInWindow(callback),
137
- measureLayout: (relativeToNativeNode, onSuccess, onFail) =>
138
- textInputRef.current?.measureLayout(
139
- relativeToNativeNode,
140
- onSuccess,
141
- onFail
126
+ useImperativeHandle(
127
+ ref,
128
+ () => ({
129
+ focus: () => textInputRef.current?.focus(),
130
+ blur: () => textInputRef.current?.blur(),
131
+ clear: () => textInputRef.current?.clear(),
132
+ isFocused: () => textInputRef.current?.isFocused(),
133
+ setNativeProps: (nativeProps) =>
134
+ textInputRef.current?.setNativeProps(nativeProps),
135
+ measure: (callback) =>
136
+ textInputRef.current?.measure(callback),
137
+ measureInWindow: (callback) =>
138
+ textInputRef.current?.measureInWindow(callback),
139
+ measureLayout: (relativeToNativeNode, onSuccess, onFail) =>
140
+ textInputRef.current?.measureLayout(
141
+ relativeToNativeNode,
142
+ onSuccess,
143
+ onFail
144
+ ),
145
+
146
+ // Custom methods and properties
147
+ getValue: () => inputValue?.replace(/\D/g, ''),
148
+ value: inputValue?.replace(/\D/g, ''),
149
+ getValueFormatted: () => inputValue,
150
+ valueFormatted: inputValue,
151
+ getFullPhoneNumber: () =>
152
+ `${countryValue?.idd?.root || ''} ${inputValue || ''}`,
153
+ fullPhoneNumber: `${countryValue?.idd?.root || ''} ${
154
+ inputValue || ''
155
+ }`,
156
+ phoneNumberLength: getPhoneNumberLength(
157
+ countryValue,
158
+ inputValue
142
159
  ),
143
- getValue: () => inputValue,
144
- value: inputValue,
145
- getFullPhoneNumber: () =>
146
- `${countryValue?.idd?.root} ${inputValue}`,
147
- fullPhoneNumber: `${countryValue?.idd?.root} ${inputValue}`,
148
- phoneNumberLength: getPhoneNumberLength(
149
- countryValue,
150
- inputValue
151
- ),
152
- getSelectedCountry: () => countryValue,
153
- selectedCountry: countryValue,
154
- isValid: isValidPhoneNumber(inputValue, selectedCountry),
155
- props: {
156
- theme,
157
- language,
158
- placeholder,
159
- phoneInputPlaceholderTextColor,
160
- phoneInputSelectionColor,
161
- phoneInputStyles,
162
- modalStyles,
163
- disabled,
164
- modalDisabled,
165
- defaultCountry,
166
- defaultValue,
167
- onChangePhoneNumber,
168
- selectedCountry,
169
- onChangeSelectedCountry,
170
- customMask,
171
- visibleCountries,
172
- hiddenCountries,
173
- popularCountries,
174
- customCaret,
175
- rtl,
176
- isFullScreen,
177
- modalType,
178
- minBottomsheetHeight,
179
- maxBottomsheetHeight,
180
- initialBottomsheetHeight,
181
- modalSearchInputPlaceholderTextColor,
182
- modalSearchInputPlaceholder,
183
- modalSearchInputSelectionColor,
184
- modalPopularCountriesTitle,
185
- modalAllCountriesTitle,
186
- modalSectionTitleComponent,
187
- modalCountryItemComponent,
188
- modalCloseButtonComponent,
189
- modalSectionTitleDisabled,
190
- modalNotFoundCountryMessage,
191
- disabledModalBackdropPress,
192
- removedModalBackdrop,
193
- onModalBackdropPress,
194
- onModalRequestClose,
195
- showModalSearchInput,
196
- showModalCloseButton,
197
- showModalScrollIndicator,
198
- customFlag,
199
- accessibilityLabelPhoneInput,
200
- accessibilityHintPhoneInput,
201
- accessibilityLabelCountriesButton,
202
- accessibilityHintCountriesButton,
203
- accessibilityLabelBackdrop,
204
- accessibilityHintBackdrop,
205
- accessibilityLabelCloseButton,
206
- accessibilityHintCloseButton,
207
- accessibilityLabelSearchInput,
208
- accessibilityHintSearchInput,
209
- accessibilityLabelCountriesList,
210
- accessibilityHintCountriesList,
211
- accessibilityLabelCountryItem,
212
- accessibilityHintCountryItem,
213
- allowFontScaling,
214
- ...rest,
215
- },
216
- };
217
-
218
- function updateRef(phoneNumber, country) {
219
- if (ref) {
220
- ref.current = {
221
- ...refBase,
222
- getValue: () => phoneNumber?.replace(/\D/g, ''),
223
- value: phoneNumber?.replace(/\D/g, ''),
224
- getValueFormatted: () => phoneNumber,
225
- valueFormatted: phoneNumber,
226
- getFullPhoneNumber: () =>
227
- `${country?.idd?.root} ${phoneNumber}`,
228
- fullPhoneNumber: `${country?.idd?.root} ${phoneNumber}`,
229
- getSelectedCountry: () => country,
230
- selectedCountry: country,
231
- isValid: isValidPhoneNumber(phoneNumber, country),
232
- props: {
233
- ...refBase.props,
234
- value: phoneNumber,
235
- selectedCountry: country,
236
- },
237
- };
238
- }
239
- }
160
+ getSelectedCountry: () => countryValue,
161
+ selectedCountry: countryValue,
162
+ isValid: isValidPhoneNumber(inputValue, countryValue),
163
+ props: {
164
+ theme,
165
+ language,
166
+ placeholder,
167
+ phoneInputPlaceholderTextColor,
168
+ phoneInputSelectionColor,
169
+ phoneInputStyles,
170
+ modalStyles,
171
+ disabled,
172
+ modalDisabled,
173
+ defaultCountry,
174
+ defaultValue,
175
+ onChangePhoneNumber,
176
+ selectedCountry,
177
+ onChangeSelectedCountry,
178
+ customMask,
179
+ visibleCountries,
180
+ hiddenCountries,
181
+ popularCountries,
182
+ customCaret,
183
+ rtl,
184
+ isFullScreen,
185
+ modalType,
186
+ minBottomsheetHeight,
187
+ maxBottomsheetHeight,
188
+ initialBottomsheetHeight,
189
+ modalSearchInputPlaceholderTextColor,
190
+ modalSearchInputPlaceholder,
191
+ modalSearchInputSelectionColor,
192
+ modalPopularCountriesTitle,
193
+ modalAllCountriesTitle,
194
+ modalSectionTitleComponent,
195
+ modalCountryItemComponent,
196
+ modalCloseButtonComponent,
197
+ modalSectionTitleDisabled,
198
+ modalNotFoundCountryMessage,
199
+ disabledModalBackdropPress,
200
+ removedModalBackdrop,
201
+ onModalBackdropPress,
202
+ onModalRequestClose,
203
+ showModalSearchInput,
204
+ showModalCloseButton,
205
+ showModalScrollIndicator,
206
+ customFlag,
207
+ accessibilityLabelPhoneInput,
208
+ accessibilityHintPhoneInput,
209
+ accessibilityLabelCountriesButton,
210
+ accessibilityHintCountriesButton,
211
+ accessibilityLabelBackdrop,
212
+ accessibilityHintBackdrop,
213
+ accessibilityLabelCloseButton,
214
+ accessibilityHintCloseButton,
215
+ accessibilityLabelSearchInput,
216
+ accessibilityHintSearchInput,
217
+ accessibilityLabelCountriesList,
218
+ accessibilityHintCountriesList,
219
+ accessibilityLabelCountryItem,
220
+ accessibilityHintCountryItem,
221
+ allowFontScaling,
222
+ value: inputValue,
223
+ ...rest,
224
+ },
225
+ }),
226
+ [inputValue, countryValue]
227
+ );
240
228
 
241
229
  function onSelect(country) {
242
230
  setShow(false);
@@ -248,7 +236,6 @@ const PhoneInput = forwardRef(
248
236
  if (onChangeSelectedCountry) {
249
237
  onChangeSelectedCountry(country);
250
238
  }
251
- updateRef('', country);
252
239
  }
253
240
 
254
241
  function formatPhoneNumberWithCustomMask(phoneNumber) {
@@ -278,58 +265,64 @@ const PhoneInput = forwardRef(
278
265
  if (onChangePhoneNumber) {
279
266
  onChangePhoneNumber(result);
280
267
  }
281
- updateRef(result, countryValue);
282
268
  }
283
269
 
284
270
  function formatPhoneNumber(phoneNumber, callingCode) {
285
- let formattedNumber = '';
271
+ try {
272
+ let formattedNumber = '';
286
273
 
287
- const metadata = new Metadata();
288
- metadata.selectNumberingPlan(countryValue?.cca2);
274
+ const metadata = new Metadata();
275
+ metadata.selectNumberingPlan(countryValue?.cca2);
289
276
 
290
- const possibleLengths = countryValue
291
- ? metadata.possibleLengths()
292
- : [];
277
+ const possibleLengths = countryValue
278
+ ? metadata.possibleLengths()
279
+ : [];
293
280
 
294
- let validCallingCode = callingCode
295
- ? callingCode
296
- : countryValue?.idd?.root;
281
+ let validCallingCode = callingCode
282
+ ? callingCode
283
+ : countryValue?.idd?.root;
297
284
 
298
- const res = formatIncompletePhoneNumber(
299
- `${validCallingCode}${phoneNumber}`
300
- );
285
+ const res = formatIncompletePhoneNumber(
286
+ `${validCallingCode}${phoneNumber}`
287
+ );
301
288
 
302
- formattedNumber = res;
289
+ formattedNumber = res;
303
290
 
304
- if (res.startsWith(0)) {
305
- formattedNumber = parsePhoneNumber(res)?.formatNational();
306
- } else {
307
- if (
308
- validCallingCode &&
309
- res &&
310
- res.startsWith(validCallingCode)
311
- ) {
312
- formattedNumber = res
313
- .substring(validCallingCode.length)
314
- .trim();
291
+ if (res.startsWith(0)) {
292
+ formattedNumber = parsePhoneNumber(res)?.formatNational();
293
+ } else {
294
+ if (
295
+ validCallingCode &&
296
+ res &&
297
+ res.startsWith(validCallingCode)
298
+ ) {
299
+ formattedNumber = res
300
+ .substring(validCallingCode.length)
301
+ .trim();
302
+ }
315
303
  }
316
- }
317
304
 
318
- const possibleLength = formattedNumber.startsWith(0)
319
- ? possibleLengths.slice(-1)[0] + 1
320
- : possibleLengths.slice(-1)[0];
305
+ const possibleLength = formattedNumber.startsWith(0)
306
+ ? possibleLengths.slice(-1)[0] + 1
307
+ : possibleLengths.slice(-1)[0];
321
308
 
322
- if (
323
- formattedNumber?.replace(/\D/g, '')?.length > possibleLength
324
- ) {
325
- return;
326
- }
309
+ if (
310
+ formattedNumber?.replace(/\D/g, '')?.length > possibleLength
311
+ ) {
312
+ return;
313
+ }
327
314
 
328
- setInputValue(formattedNumber);
329
- if (onChangePhoneNumber) {
330
- onChangePhoneNumber(formattedNumber);
315
+ setInputValue(formattedNumber);
316
+ if (onChangePhoneNumber) {
317
+ onChangePhoneNumber(formattedNumber);
318
+ }
319
+
320
+ } catch {
321
+ setInputValue(phoneNumber);
322
+ if (onChangePhoneNumber) {
323
+ onChangePhoneNumber(phoneNumber);
324
+ }
331
325
  }
332
- updateRef(formattedNumber, countryValue);
333
326
  }
334
327
 
335
328
  function onChangeText(phoneNumber, callingCode) {
@@ -343,7 +336,6 @@ const PhoneInput = forwardRef(
343
336
  if (onChangeSelectedCountry) {
344
337
  onChangeSelectedCountry(matchingCountry);
345
338
  }
346
- updateRef('', matchingCountry);
347
339
 
348
340
  onChangeText(
349
341
  phoneNumber.replace(matchingCountry?.idd?.root, ''),
@@ -367,9 +359,6 @@ const PhoneInput = forwardRef(
367
359
  if (onChangeSelectedCountry) {
368
360
  onChangeSelectedCountry(country);
369
361
  }
370
- updateRef('', country);
371
- } else if (countryValue) {
372
- updateRef(inputValue, countryValue);
373
362
  }
374
363
  }, []);
375
364
 
@@ -380,7 +369,6 @@ const PhoneInput = forwardRef(
380
369
  if (onChangeSelectedCountry) {
381
370
  onChangeSelectedCountry(c);
382
371
  }
383
- updateRef('', c);
384
372
  }
385
373
  }, [defaultCountry]);
386
374
 
@@ -395,7 +383,6 @@ const PhoneInput = forwardRef(
395
383
  if (onChangeSelectedCountry) {
396
384
  onChangeSelectedCountry(matchingCountry);
397
385
  }
398
- updateRef('', matchingCountry);
399
386
  } else {
400
387
  // console.warn(
401
388
  // "The default number provided (defaultValue) don't match with anyone country. Please, correct it to be shown in the input. For more information: https://github.com/AstrOOnauta/react-native-international-phone-number#intermediate-usage---typescript--default-phone-number-value",
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.11.4",
4
+ "version": "0.11.6",
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",
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "react-native-country-select": "0.3.8",
48
- "libphonenumber-js": "1.12.29"
48
+ "libphonenumber-js": "1.12.34"
49
49
  },
50
50
  "devDependencies": {
51
51
  "metro-react-native-babel-preset": "^0.61.0"