@jacksonavila/phone-lib 2.0.6 → 2.0.7

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.7",
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.7/phone-lib.css">
8
+ * <script src="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/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.7';
21
21
  const PACKAGE_NAME = '@jacksonavila/phone-lib';
22
22
 
23
23
  // URLs de CDN
package/phone-lib.js CHANGED
@@ -845,17 +845,34 @@ class PhoneLib {
845
845
  */
846
846
  handlePhoneInput(value) {
847
847
  const previousNumber = this.phoneNumber;
848
+ const previousCountry = this.selectedCountry;
848
849
  this.phoneNumber = value;
849
850
 
850
851
  // Detección automática de país si está habilitada
852
+ // IMPORTANTE: Hacer esto ANTES de formatear para que el formateo use el país correcto
851
853
  if (this.options.autoDetectCountry && value) {
852
854
  this.autoDetectCountry(value);
853
855
  }
854
856
 
857
+ // Actualizar el número formateado (usará el país correcto si se detectó)
855
858
  this.updatePhoneNumber();
856
859
 
857
860
  // Ejecutar callbacks y eventos (validar sin actualizar visuals para obtener estado actual)
858
861
  const isValid = this.phoneNumber ? this.validatePhone(false) : false;
862
+
863
+ // Emitir evento de cambio de país si cambió
864
+ if (previousCountry !== this.selectedCountry) {
865
+ const dialCode = this.getDialCode();
866
+ const countryData = this.countries.find(c => c.iso2 === this.selectedCountry);
867
+ this.executeCallback('onCountryChange', this.selectedCountry, dialCode, countryData?.name);
868
+ this.emitEvent('countryChange', {
869
+ country: this.selectedCountry,
870
+ dialCode,
871
+ countryName: countryData?.name,
872
+ previousCountry
873
+ });
874
+ }
875
+
859
876
  this.executeCallback('onPhoneChange', this.phoneNumber, isValid, this.selectedCountry);
860
877
  this.emitEvent('phoneChange', {
861
878
  phoneNumber: this.phoneNumber,
@@ -906,7 +923,25 @@ class PhoneLib {
906
923
  }
907
924
 
908
925
  try {
909
- const formatter = new AsYouType(this.selectedCountry);
926
+ // Si el número empieza con +, intentar detectar el país para formatear correctamente
927
+ let countryForFormatting = this.selectedCountry;
928
+ if (this.phoneNumber && this.phoneNumber.startsWith('+')) {
929
+ try {
930
+ const parsed = parsePhoneNumber(this.phoneNumber);
931
+ if (parsed && parsed.country) {
932
+ // Usar el país detectado para formatear, si está disponible
933
+ const detectedCountry = parsed.country;
934
+ const countryAvailable = this.countries.find(c => c.iso2 === detectedCountry && !c.disabled);
935
+ if (countryAvailable) {
936
+ countryForFormatting = detectedCountry;
937
+ }
938
+ }
939
+ } catch (e) {
940
+ // Si no se puede detectar, usar el país seleccionado actual
941
+ }
942
+ }
943
+
944
+ const formatter = new AsYouType(countryForFormatting);
910
945
  const formatted = formatter.input(this.phoneNumber);
911
946
 
912
947
  // Solo actualizar si el valor formateado es diferente