@jacksonavila/phone-lib 2.0.6 → 2.0.8

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
@@ -61,7 +61,7 @@ You can use PhoneLib directly from CDN without npm / Puedes usar PhoneLib direct
61
61
  <!DOCTYPE html>
62
62
  <html>
63
63
  <head>
64
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.6/phone-lib.css">
64
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/phone-lib.css">
65
65
  </head>
66
66
  <body>
67
67
  <div id="phone-container"></div>
@@ -69,7 +69,7 @@ You can use PhoneLib directly from CDN without npm / Puedes usar PhoneLib direct
69
69
  <script type="importmap">
70
70
  {
71
71
  "imports": {
72
- "@jacksonavila/phone-lib": "https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.6/phone-lib.js",
72
+ "@jacksonavila/phone-lib": "https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/phone-lib.js",
73
73
  "libphonenumber-js": "https://esm.sh/libphonenumber-js@1.11.0"
74
74
  }
75
75
  }
@@ -89,8 +89,8 @@ You can use PhoneLib directly from CDN without npm / Puedes usar PhoneLib direct
89
89
  ```
90
90
 
91
91
  **CDN URLs / URLs de CDN:**
92
- - **jsDelivr:** `https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.6/`
93
- - **unpkg:** `https://unpkg.com/@jacksonavila/phone-lib@2.0.6/`
92
+ - **jsDelivr:** `https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/`
93
+ - **unpkg:** `https://unpkg.com/@jacksonavila/phone-lib@2.0.7/`
94
94
 
95
95
  ### Method 2: Script Tag (All Browsers) / Método 2: Script Tag (Todos los Navegadores)
96
96
 
@@ -98,12 +98,12 @@ You can use PhoneLib directly from CDN without npm / Puedes usar PhoneLib direct
98
98
  <!DOCTYPE html>
99
99
  <html>
100
100
  <head>
101
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.6/phone-lib.css">
101
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/phone-lib.css">
102
102
  </head>
103
103
  <body>
104
104
  <div id="phone-container"></div>
105
105
 
106
- <script src="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.6/phone-lib.cdn.js"></script>
106
+ <script src="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/phone-lib.cdn.js"></script>
107
107
 
108
108
  <script>
109
109
  let phoneLib = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacksonavila/phone-lib",
3
- "version": "2.0.6",
3
+ "version": "2.0.8",
4
4
  "description": "Librería JavaScript para input de teléfono con selector de país y banderas - Compatible con Vanilla JS y React",
5
5
  "main": "phone-lib.js",
6
6
  "module": "phone-lib.js",
package/phone-lib.cdn.js CHANGED
@@ -4,8 +4,8 @@
4
4
  * Carga libphonenumber-js dinámicamente y expone PhoneLib globalmente
5
5
  *
6
6
  * Uso:
7
- * <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.6/phone-lib.css">
8
- * <script src="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.6/phone-lib.cdn.js"></script>
7
+ * <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.8/phone-lib.css">
8
+ * <script src="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.8/phone-lib.cdn.js"></script>
9
9
  * <script>
10
10
  * document.addEventListener('phoneLibReady', () => {
11
11
  * const phoneLib = new PhoneLib('#container', {...});
@@ -17,7 +17,7 @@
17
17
  'use strict';
18
18
 
19
19
  // Versión del paquete (actualizar cuando se publique nueva versión)
20
- const PACKAGE_VERSION = '2.0.6';
20
+ const PACKAGE_VERSION = '2.0.8';
21
21
  const PACKAGE_NAME = '@jacksonavila/phone-lib';
22
22
 
23
23
  // URLs de CDN
package/phone-lib.js CHANGED
@@ -254,68 +254,256 @@ class PhoneLib {
254
254
  */
255
255
  getCountryName(iso2) {
256
256
  const countryNames = {
257
- 'US': 'Estados Unidos',
258
- 'CO': 'Colombia',
259
- 'ES': 'España',
260
- 'MX': 'México',
257
+ 'AD': 'Andorra',
258
+ 'AE': 'Emiratos Árabes Unidos',
259
+ 'AF': 'Afganistán',
260
+ 'AG': 'Antigua y Barbuda',
261
+ 'AI': 'Anguila',
262
+ 'AL': 'Albania',
263
+ 'AM': 'Armenia',
264
+ 'AO': 'Angola',
265
+ 'AQ': 'Antártida',
261
266
  'AR': 'Argentina',
262
- 'CL': 'Chile',
263
- 'PE': 'Perú',
264
- 'VE': 'Venezuela',
265
- 'EC': 'Ecuador',
267
+ 'AS': 'Samoa Americana',
268
+ 'AT': 'Austria',
269
+ 'AU': 'Australia',
270
+ 'AW': 'Aruba',
271
+ 'AX': 'Islas Åland',
272
+ 'AZ': 'Azerbaiyán',
273
+ 'BA': 'Bosnia y Herzegovina',
274
+ 'BB': 'Barbados',
275
+ 'BD': 'Bangladesh',
276
+ 'BE': 'Bélgica',
277
+ 'BF': 'Burkina Faso',
278
+ 'BG': 'Bulgaria',
279
+ 'BH': 'Baréin',
280
+ 'BI': 'Burundi',
281
+ 'BJ': 'Benín',
282
+ 'BL': 'San Bartolomé',
283
+ 'BM': 'Bermudas',
284
+ 'BN': 'Brunéi',
266
285
  'BO': 'Bolivia',
267
- 'PY': 'Paraguay',
268
- 'UY': 'Uruguay',
269
- 'CR': 'Costa Rica',
270
- 'PA': 'Panamá',
271
- 'GT': 'Guatemala',
272
- 'HN': 'Honduras',
273
- 'NI': 'Nicaragua',
274
- 'SV': 'El Salvador',
275
- 'DO': 'República Dominicana',
276
- 'CU': 'Cuba',
277
- 'PR': 'Puerto Rico',
286
+ 'BQ': 'Caribe Neerlandés',
278
287
  'BR': 'Brasil',
288
+ 'BS': 'Bahamas',
289
+ 'BT': 'Bután',
290
+ 'BV': 'Isla Bouvet',
291
+ 'BW': 'Botsuana',
292
+ 'BY': 'Bielorrusia',
293
+ 'BZ': 'Belice',
279
294
  'CA': 'Canadá',
280
- 'GB': 'Reino Unido',
281
- 'FR': 'Francia',
282
- 'DE': 'Alemania',
283
- 'IT': 'Italia',
284
- 'PT': 'Portugal',
285
- 'NL': 'Países Bajos',
286
- 'BE': 'Bélgica',
295
+ 'CC': 'Islas Cocos',
296
+ 'CD': 'República Democrática del Congo',
297
+ 'CF': 'República Centroafricana',
298
+ 'CG': 'Congo',
287
299
  'CH': 'Suiza',
288
- 'AT': 'Austria',
289
- 'SE': 'Suecia',
290
- 'NO': 'Noruega',
300
+ 'CI': 'Costa de Marfil',
301
+ 'CK': 'Islas Cook',
302
+ 'CL': 'Chile',
303
+ 'CM': 'Camerún',
304
+ 'CN': 'China',
305
+ 'CO': 'Colombia',
306
+ 'CR': 'Costa Rica',
307
+ 'CU': 'Cuba',
308
+ 'CV': 'Cabo Verde',
309
+ 'CW': 'Curazao',
310
+ 'CX': 'Isla de Navidad',
311
+ 'CY': 'Chipre',
312
+ 'CZ': 'República Checa',
313
+ 'DE': 'Alemania',
314
+ 'DJ': 'Yibuti',
291
315
  'DK': 'Dinamarca',
316
+ 'DM': 'Dominica',
317
+ 'DO': 'República Dominicana',
318
+ 'DZ': 'Argelia',
319
+ 'EC': 'Ecuador',
320
+ 'EE': 'Estonia',
321
+ 'EG': 'Egipto',
322
+ 'EH': 'Sahara Occidental',
323
+ 'ER': 'Eritrea',
324
+ 'ES': 'España',
325
+ 'ET': 'Etiopía',
292
326
  'FI': 'Finlandia',
293
- 'PL': 'Polonia',
294
- 'CZ': 'República Checa',
327
+ 'FJ': 'Fiyi',
328
+ 'FK': 'Islas Malvinas',
329
+ 'FM': 'Micronesia',
330
+ 'FO': 'Islas Feroe',
331
+ 'FR': 'Francia',
332
+ 'GA': 'Gabón',
333
+ 'GB': 'Reino Unido',
334
+ 'GD': 'Granada',
335
+ 'GE': 'Georgia',
336
+ 'GF': 'Guayana Francesa',
337
+ 'GG': 'Guernsey',
338
+ 'GH': 'Ghana',
339
+ 'GI': 'Gibraltar',
340
+ 'GL': 'Groenlandia',
341
+ 'GM': 'Gambia',
342
+ 'GN': 'Guinea',
343
+ 'GP': 'Guadalupe',
344
+ 'GQ': 'Guinea Ecuatorial',
295
345
  'GR': 'Grecia',
346
+ 'GS': 'Georgia del Sur e Islas Sandwich del Sur',
347
+ 'GT': 'Guatemala',
348
+ 'GU': 'Guam',
349
+ 'GW': 'Guinea-Bisáu',
350
+ 'GY': 'Guyana',
351
+ 'HK': 'Hong Kong',
352
+ 'HM': 'Islas Heard y McDonald',
353
+ 'HN': 'Honduras',
354
+ 'HR': 'Croacia',
355
+ 'HT': 'Haití',
356
+ 'HU': 'Hungría',
357
+ 'ID': 'Indonesia',
296
358
  'IE': 'Irlanda',
297
- 'NZ': 'Nueva Zelanda',
298
- 'AU': 'Australia',
299
- 'JP': 'Japón',
300
- 'CN': 'China',
359
+ 'IL': 'Israel',
360
+ 'IM': 'Isla de Man',
301
361
  'IN': 'India',
362
+ 'IO': 'Territorio Británico del Océano Índico',
363
+ 'IQ': 'Irak',
364
+ 'IR': 'Irán',
365
+ 'IS': 'Islandia',
366
+ 'IT': 'Italia',
367
+ 'JE': 'Jersey',
368
+ 'JM': 'Jamaica',
369
+ 'JO': 'Jordania',
370
+ 'JP': 'Japón',
371
+ 'KE': 'Kenia',
372
+ 'KG': 'Kirguistán',
373
+ 'KH': 'Camboya',
374
+ 'KI': 'Kiribati',
375
+ 'KM': 'Comoras',
376
+ 'KN': 'San Cristóbal y Nieves',
377
+ 'KP': 'Corea del Norte',
302
378
  'KR': 'Corea del Sur',
303
- 'SG': 'Singapur',
379
+ 'KW': 'Kuwait',
380
+ 'KY': 'Islas Caimán',
381
+ 'KZ': 'Kazajistán',
382
+ 'LA': 'Laos',
383
+ 'LB': 'Líbano',
384
+ 'LC': 'Santa Lucía',
385
+ 'LI': 'Liechtenstein',
386
+ 'LK': 'Sri Lanka',
387
+ 'LR': 'Liberia',
388
+ 'LS': 'Lesoto',
389
+ 'LT': 'Lituania',
390
+ 'LU': 'Luxemburgo',
391
+ 'LV': 'Letonia',
392
+ 'LY': 'Libia',
393
+ 'MA': 'Marruecos',
394
+ 'MC': 'Mónaco',
395
+ 'MD': 'Moldavia',
396
+ 'ME': 'Montenegro',
397
+ 'MF': 'San Martín',
398
+ 'MG': 'Madagascar',
399
+ 'MH': 'Islas Marshall',
400
+ 'MK': 'Macedonia del Norte',
401
+ 'ML': 'Malí',
402
+ 'MM': 'Myanmar',
403
+ 'MN': 'Mongolia',
404
+ 'MO': 'Macao',
405
+ 'MP': 'Islas Marianas del Norte',
406
+ 'MQ': 'Martinica',
407
+ 'MR': 'Mauritania',
408
+ 'MS': 'Montserrat',
409
+ 'MT': 'Malta',
410
+ 'MU': 'Mauricio',
411
+ 'MV': 'Maldivas',
412
+ 'MW': 'Malaui',
413
+ 'MX': 'México',
304
414
  'MY': 'Malasia',
305
- 'TH': 'Tailandia',
415
+ 'MZ': 'Mozambique',
416
+ 'NA': 'Namibia',
417
+ 'NC': 'Nueva Caledonia',
418
+ 'NE': 'Níger',
419
+ 'NF': 'Isla Norfolk',
420
+ 'NG': 'Nigeria',
421
+ 'NI': 'Nicaragua',
422
+ 'NL': 'Países Bajos',
423
+ 'NO': 'Noruega',
424
+ 'NP': 'Nepal',
425
+ 'NR': 'Nauru',
426
+ 'NU': 'Niue',
427
+ 'NZ': 'Nueva Zelanda',
428
+ 'OM': 'Omán',
429
+ 'PA': 'Panamá',
430
+ 'PE': 'Perú',
431
+ 'PF': 'Polinesia Francesa',
432
+ 'PG': 'Papúa Nueva Guinea',
306
433
  'PH': 'Filipinas',
307
- 'ID': 'Indonesia',
308
- 'VN': 'Vietnam',
309
- 'AE': 'Emiratos Árabes Unidos',
434
+ 'PK': 'Pakistán',
435
+ 'PL': 'Polonia',
436
+ 'PM': 'San Pedro y Miquelón',
437
+ 'PN': 'Islas Pitcairn',
438
+ 'PR': 'Puerto Rico',
439
+ 'PS': 'Palestina',
440
+ 'PT': 'Portugal',
441
+ 'PW': 'Palaos',
442
+ 'PY': 'Paraguay',
443
+ 'QA': 'Catar',
444
+ 'RE': 'Reunión',
445
+ 'RO': 'Rumania',
446
+ 'RS': 'Serbia',
447
+ 'RU': 'Rusia',
448
+ 'RW': 'Ruanda',
310
449
  'SA': 'Arabia Saudí',
311
- 'IL': 'Israel',
450
+ 'SB': 'Islas Salomón',
451
+ 'SC': 'Seychelles',
452
+ 'SD': 'Sudán',
453
+ 'SE': 'Suecia',
454
+ 'SG': 'Singapur',
455
+ 'SH': 'Santa Elena',
456
+ 'SI': 'Eslovenia',
457
+ 'SJ': 'Svalbard y Jan Mayen',
458
+ 'SK': 'Eslovaquia',
459
+ 'SL': 'Sierra Leona',
460
+ 'SM': 'San Marino',
461
+ 'SN': 'Senegal',
462
+ 'SO': 'Somalia',
463
+ 'SR': 'Surinam',
464
+ 'SS': 'Sudán del Sur',
465
+ 'ST': 'Santo Tomé y Príncipe',
466
+ 'SV': 'El Salvador',
467
+ 'SX': 'Sint Maarten',
468
+ 'SY': 'Siria',
469
+ 'SZ': 'Suazilandia',
470
+ 'TC': 'Islas Turcas y Caicos',
471
+ 'TD': 'Chad',
472
+ 'TF': 'Territorios Australes Franceses',
473
+ 'TG': 'Togo',
474
+ 'TH': 'Tailandia',
475
+ 'TJ': 'Tayikistán',
476
+ 'TK': 'Tokelau',
477
+ 'TL': 'Timor Oriental',
478
+ 'TM': 'Turkmenistán',
479
+ 'TN': 'Túnez',
480
+ 'TO': 'Tonga',
312
481
  'TR': 'Turquía',
313
- 'RU': 'Rusia',
482
+ 'TT': 'Trinidad y Tobago',
483
+ 'TV': 'Tuvalu',
484
+ 'TW': 'Taiwán',
485
+ 'TZ': 'Tanzania',
486
+ 'UA': 'Ucrania',
487
+ 'UG': 'Uganda',
488
+ 'UM': 'Islas Ultramarinas de Estados Unidos',
489
+ 'US': 'Estados Unidos',
490
+ 'UY': 'Uruguay',
491
+ 'UZ': 'Uzbekistán',
492
+ 'VA': 'Ciudad del Vaticano',
493
+ 'VC': 'San Vicente y las Granadinas',
494
+ 'VE': 'Venezuela',
495
+ 'VG': 'Islas Vírgenes Británicas',
496
+ 'VI': 'Islas Vírgenes de los Estados Unidos',
497
+ 'VN': 'Vietnam',
498
+ 'VU': 'Vanuatu',
499
+ 'WF': 'Wallis y Futuna',
500
+ 'WS': 'Samoa',
501
+ 'XK': 'Kosovo',
502
+ 'YE': 'Yemen',
503
+ 'YT': 'Mayotte',
314
504
  'ZA': 'Sudáfrica',
315
- 'EG': 'Egipto',
316
- 'NG': 'Nigeria',
317
- 'KE': 'Kenia',
318
- 'GH': 'Ghana'
505
+ 'ZM': 'Zambia',
506
+ 'ZW': 'Zimbabue'
319
507
  };
320
508
 
321
509
  return countryNames[iso2] || iso2;
@@ -845,17 +1033,34 @@ class PhoneLib {
845
1033
  */
846
1034
  handlePhoneInput(value) {
847
1035
  const previousNumber = this.phoneNumber;
1036
+ const previousCountry = this.selectedCountry;
848
1037
  this.phoneNumber = value;
849
1038
 
850
1039
  // Detección automática de país si está habilitada
1040
+ // IMPORTANTE: Hacer esto ANTES de formatear para que el formateo use el país correcto
851
1041
  if (this.options.autoDetectCountry && value) {
852
1042
  this.autoDetectCountry(value);
853
1043
  }
854
1044
 
1045
+ // Actualizar el número formateado (usará el país correcto si se detectó)
855
1046
  this.updatePhoneNumber();
856
1047
 
857
1048
  // Ejecutar callbacks y eventos (validar sin actualizar visuals para obtener estado actual)
858
1049
  const isValid = this.phoneNumber ? this.validatePhone(false) : false;
1050
+
1051
+ // Emitir evento de cambio de país si cambió
1052
+ if (previousCountry !== this.selectedCountry) {
1053
+ const dialCode = this.getDialCode();
1054
+ const countryData = this.countries.find(c => c.iso2 === this.selectedCountry);
1055
+ this.executeCallback('onCountryChange', this.selectedCountry, dialCode, countryData?.name);
1056
+ this.emitEvent('countryChange', {
1057
+ country: this.selectedCountry,
1058
+ dialCode,
1059
+ countryName: countryData?.name,
1060
+ previousCountry
1061
+ });
1062
+ }
1063
+
859
1064
  this.executeCallback('onPhoneChange', this.phoneNumber, isValid, this.selectedCountry);
860
1065
  this.emitEvent('phoneChange', {
861
1066
  phoneNumber: this.phoneNumber,
@@ -906,7 +1111,25 @@ class PhoneLib {
906
1111
  }
907
1112
 
908
1113
  try {
909
- const formatter = new AsYouType(this.selectedCountry);
1114
+ // Si el número empieza con +, intentar detectar el país para formatear correctamente
1115
+ let countryForFormatting = this.selectedCountry;
1116
+ if (this.phoneNumber && this.phoneNumber.startsWith('+')) {
1117
+ try {
1118
+ const parsed = parsePhoneNumber(this.phoneNumber);
1119
+ if (parsed && parsed.country) {
1120
+ // Usar el país detectado para formatear, si está disponible
1121
+ const detectedCountry = parsed.country;
1122
+ const countryAvailable = this.countries.find(c => c.iso2 === detectedCountry && !c.disabled);
1123
+ if (countryAvailable) {
1124
+ countryForFormatting = detectedCountry;
1125
+ }
1126
+ }
1127
+ } catch (e) {
1128
+ // Si no se puede detectar, usar el país seleccionado actual
1129
+ }
1130
+ }
1131
+
1132
+ const formatter = new AsYouType(countryForFormatting);
910
1133
  const formatted = formatter.input(this.phoneNumber);
911
1134
 
912
1135
  // Solo actualizar si el valor formateado es diferente