react-native-international-phone-number 0.11.4 → 0.11.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 +147 -159
- 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
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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,65 @@ 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
|
-
|
|
271
|
+
try {
|
|
272
|
+
let formattedNumber = '';
|
|
286
273
|
|
|
287
|
-
|
|
288
|
-
|
|
274
|
+
const metadata = new Metadata();
|
|
275
|
+
metadata.selectNumberingPlan(countryValue?.cca2);
|
|
289
276
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
277
|
+
const possibleLengths = countryValue
|
|
278
|
+
? metadata.possibleLengths()
|
|
279
|
+
: [];
|
|
293
280
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
281
|
+
let validCallingCode = callingCode
|
|
282
|
+
? callingCode
|
|
283
|
+
: countryValue?.idd?.root;
|
|
297
284
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
285
|
+
const res = formatIncompletePhoneNumber(
|
|
286
|
+
`${validCallingCode}${phoneNumber}`
|
|
287
|
+
);
|
|
301
288
|
|
|
302
|
-
|
|
289
|
+
formattedNumber = res;
|
|
303
290
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
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
|
-
|
|
319
|
-
|
|
320
|
-
|
|
305
|
+
const possibleLength = formattedNumber.startsWith(0)
|
|
306
|
+
? possibleLengths.slice(-1)[0] + 1
|
|
307
|
+
: possibleLengths.slice(-1)[0];
|
|
321
308
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
309
|
+
if (
|
|
310
|
+
formattedNumber?.replace(/\D/g, '')?.length > possibleLength
|
|
311
|
+
) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
327
314
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
315
|
+
setInputValue(formattedNumber);
|
|
316
|
+
if (onChangePhoneNumber) {
|
|
317
|
+
onChangePhoneNumber(formattedNumber);
|
|
318
|
+
}
|
|
319
|
+
updateRef(formattedNumber, countryValue);
|
|
320
|
+
} catch {
|
|
321
|
+
setInputValue(phoneNumber);
|
|
322
|
+
if (onChangePhoneNumber) {
|
|
323
|
+
onChangePhoneNumber(phoneNumber);
|
|
324
|
+
}
|
|
325
|
+
updateRef(phoneNumber, countryValue);
|
|
331
326
|
}
|
|
332
|
-
updateRef(formattedNumber, countryValue);
|
|
333
327
|
}
|
|
334
328
|
|
|
335
329
|
function onChangeText(phoneNumber, callingCode) {
|
|
@@ -343,7 +337,6 @@ const PhoneInput = forwardRef(
|
|
|
343
337
|
if (onChangeSelectedCountry) {
|
|
344
338
|
onChangeSelectedCountry(matchingCountry);
|
|
345
339
|
}
|
|
346
|
-
updateRef('', matchingCountry);
|
|
347
340
|
|
|
348
341
|
onChangeText(
|
|
349
342
|
phoneNumber.replace(matchingCountry?.idd?.root, ''),
|
|
@@ -367,9 +360,6 @@ const PhoneInput = forwardRef(
|
|
|
367
360
|
if (onChangeSelectedCountry) {
|
|
368
361
|
onChangeSelectedCountry(country);
|
|
369
362
|
}
|
|
370
|
-
updateRef('', country);
|
|
371
|
-
} else if (countryValue) {
|
|
372
|
-
updateRef(inputValue, countryValue);
|
|
373
363
|
}
|
|
374
364
|
}, []);
|
|
375
365
|
|
|
@@ -380,7 +370,6 @@ const PhoneInput = forwardRef(
|
|
|
380
370
|
if (onChangeSelectedCountry) {
|
|
381
371
|
onChangeSelectedCountry(c);
|
|
382
372
|
}
|
|
383
|
-
updateRef('', c);
|
|
384
373
|
}
|
|
385
374
|
}, [defaultCountry]);
|
|
386
375
|
|
|
@@ -395,7 +384,6 @@ const PhoneInput = forwardRef(
|
|
|
395
384
|
if (onChangeSelectedCountry) {
|
|
396
385
|
onChangeSelectedCountry(matchingCountry);
|
|
397
386
|
}
|
|
398
|
-
updateRef('', matchingCountry);
|
|
399
387
|
} else {
|
|
400
388
|
// console.warn(
|
|
401
389
|
// "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
|
+
"version": "0.11.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",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"react-native-country-select": "0.3.8",
|
|
48
|
-
"libphonenumber-js": "1.12.
|
|
48
|
+
"libphonenumber-js": "1.12.34"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"metro-react-native-babel-preset": "^0.61.0"
|