telpick 2.0.0 → 2.0.1
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/dist/telpick.es.js +97 -79
- package/dist/telpick.umd.js +1 -1
- package/package.json +1 -1
- package/src/demo-init.js +4 -0
- package/src/telpick.js +45 -6
- package/src/telpick.react.tsx +93 -47
- package/src/telpick.ts +13 -7
- package/src/telpick.vue.ts +80 -33
- package/dist/assets/flags/lista.txt +0 -0
- package/dist/assets/flags/se (1).webp +0 -0
- package/src/assets/flags/lista.txt +0 -0
- package/src/assets/flags/se (1).webp +0 -0
package/dist/telpick.es.js
CHANGED
|
@@ -563,59 +563,75 @@ function P() {
|
|
|
563
563
|
return i ? `${i.replace(/\/$/, "")}/assets/flags` : "";
|
|
564
564
|
}
|
|
565
565
|
class T {
|
|
566
|
-
constructor({ code:
|
|
567
|
-
}, styleOverrides: o = {}, baseFlagUrl:
|
|
568
|
-
this.code =
|
|
566
|
+
constructor({ code: c = null, onChange: r = () => {
|
|
567
|
+
}, styleOverrides: o = {}, baseFlagUrl: t = "" } = {}) {
|
|
568
|
+
this.code = c, this.onChange = r, this.styleOverrides = o, this.baseFlagUrl = t || P(), this.codes = [], this.selectedCode = c, this.isDropdownOpen = !1, this.searchQuery = "", this.container = null, this.dropdown = null, this._outsideHandler = null, this._scrollResizeCleanup = null, this._boundUpdatePosition = () => this._updateDropdownPosition(), this._getFlagUrl = this._getFlagUrl.bind(this);
|
|
569
569
|
}
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
570
|
+
_updateDropdownPosition() {
|
|
571
|
+
if (!this.dropdown || !this.container)
|
|
572
|
+
return;
|
|
573
|
+
const c = this.container.getBoundingClientRect();
|
|
574
|
+
this.dropdown.style.position = "fixed", this.dropdown.style.top = `${c.bottom + 4}px`, this.dropdown.style.left = `${c.left}px`, this.dropdown.style.marginTop = "0";
|
|
573
575
|
}
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
576
|
+
_getFlagUrl(c, r) {
|
|
577
|
+
const o = c ? c.replace(/^.*\//, "") : r ? `${String(r).toLowerCase()}.webp` : "";
|
|
578
|
+
if (!o && !r)
|
|
579
|
+
return "";
|
|
580
|
+
if (this.baseFlagUrl) {
|
|
581
|
+
const t = this.baseFlagUrl.replace(/\/$/, "");
|
|
582
|
+
if (t.includes("flagcdn.com")) {
|
|
583
|
+
const l = String(r || o && o.replace(/\.[^.]+$/, "") || "").toLowerCase();
|
|
584
|
+
return l ? `${t}/${l}.png` : "";
|
|
585
|
+
}
|
|
586
|
+
return `${t}/${o}`;
|
|
587
|
+
}
|
|
588
|
+
return c || (r ? `/flags/${String(r).toLowerCase()}.webp` : "");
|
|
589
|
+
}
|
|
590
|
+
async init(c) {
|
|
591
|
+
if (this.container = c, this.codes = [...S].sort((o, t) => o.country.localeCompare(t.country, "es")), this.code) {
|
|
592
|
+
const o = this.codes.find((t) => t.country_code === this.code);
|
|
577
593
|
o && (this.selectedCode = o.country_code);
|
|
578
594
|
} else {
|
|
579
595
|
const o = [
|
|
580
596
|
async () => {
|
|
581
597
|
try {
|
|
582
|
-
const
|
|
583
|
-
if (
|
|
584
|
-
return this.codes.find((e) => e.country_code ===
|
|
598
|
+
const s = await (await fetch("https://ip-api.com/json/?fields=countryCode")).json();
|
|
599
|
+
if (s.countryCode)
|
|
600
|
+
return this.codes.find((e) => e.country_code === s.countryCode);
|
|
585
601
|
} catch {
|
|
586
602
|
}
|
|
587
603
|
return null;
|
|
588
604
|
},
|
|
589
605
|
async () => {
|
|
590
606
|
try {
|
|
591
|
-
const
|
|
592
|
-
if (
|
|
593
|
-
return this.codes.find((e) => e.country_code ===
|
|
607
|
+
const s = await (await fetch("https://get.geojs.io/v1/ip/country.json")).json();
|
|
608
|
+
if (s.country)
|
|
609
|
+
return this.codes.find((e) => e.country_code === s.country);
|
|
594
610
|
} catch {
|
|
595
611
|
}
|
|
596
612
|
return null;
|
|
597
613
|
},
|
|
598
614
|
async () => {
|
|
599
615
|
try {
|
|
600
|
-
const
|
|
601
|
-
if (
|
|
602
|
-
return this.codes.find((e) => e.country_code ===
|
|
616
|
+
const s = await (await fetch("https://ipapi.co/json/")).json();
|
|
617
|
+
if (s.country_code)
|
|
618
|
+
return this.codes.find((e) => e.country_code === s.country_code);
|
|
603
619
|
} catch {
|
|
604
620
|
}
|
|
605
621
|
return null;
|
|
606
622
|
}
|
|
607
623
|
];
|
|
608
|
-
let
|
|
624
|
+
let t = null;
|
|
609
625
|
for (const l of o)
|
|
610
626
|
try {
|
|
611
|
-
if (
|
|
627
|
+
if (t = await Promise.race([
|
|
612
628
|
l(),
|
|
613
|
-
new Promise((
|
|
614
|
-
]),
|
|
629
|
+
new Promise((s) => setTimeout(() => s(null), 3e3))
|
|
630
|
+
]), t)
|
|
615
631
|
break;
|
|
616
632
|
} catch {
|
|
617
633
|
}
|
|
618
|
-
if (!
|
|
634
|
+
if (!t)
|
|
619
635
|
try {
|
|
620
636
|
const l = Intl.DateTimeFormat().resolvedOptions().timeZone, e = {
|
|
621
637
|
"America/Lima": "PE",
|
|
@@ -680,105 +696,107 @@ class T {
|
|
|
680
696
|
"Africa/Nairobi": "KE",
|
|
681
697
|
"Africa/Lagos": "NG"
|
|
682
698
|
}[l];
|
|
683
|
-
e && (
|
|
699
|
+
e && (t = this.codes.find((u) => u.country_code === e));
|
|
684
700
|
} catch {
|
|
685
701
|
}
|
|
686
|
-
if (
|
|
687
|
-
this.selectedCode =
|
|
702
|
+
if (t)
|
|
703
|
+
this.selectedCode = t.country_code;
|
|
688
704
|
else {
|
|
689
|
-
const l = this.codes.find((
|
|
705
|
+
const l = this.codes.find((s) => s.country_code === "CO");
|
|
690
706
|
l && (this.selectedCode = l.country_code);
|
|
691
707
|
}
|
|
692
708
|
}
|
|
693
709
|
this.render();
|
|
694
|
-
const
|
|
695
|
-
|
|
710
|
+
const r = this.codes.find((o) => o.country_code === this.selectedCode);
|
|
711
|
+
r && this.onChange(r), this._setupOutsideClick();
|
|
696
712
|
}
|
|
697
713
|
render() {
|
|
698
|
-
this.container.innerHTML = "";
|
|
699
|
-
const
|
|
700
|
-
|
|
714
|
+
this._scrollResizeCleanup && (this._scrollResizeCleanup(), this._scrollResizeCleanup = null), this.dropdown && this.dropdown.parentNode && (this.dropdown.parentNode.removeChild(this.dropdown), this.dropdown = null), this.container.innerHTML = "";
|
|
715
|
+
const c = document.createElement("button");
|
|
716
|
+
c.className = "telpick-btn", Object.assign(c.style, this.styleOverrides), c.onclick = () => {
|
|
701
717
|
this.isDropdownOpen = !this.isDropdownOpen, this.render();
|
|
702
718
|
};
|
|
703
|
-
const
|
|
704
|
-
|
|
705
|
-
const o = this.codes.find((e) => e.country_code === this.selectedCode) || { country_code: "", code: "", country: "", flag: "" },
|
|
706
|
-
if (
|
|
719
|
+
const r = document.createElement("div");
|
|
720
|
+
r.className = "telpick-flag";
|
|
721
|
+
const o = this.codes.find((e) => e.country_code === this.selectedCode) || { country_code: "", code: "", country: "", flag: "" }, t = this._getFlagUrl(o.flag, o.country_code);
|
|
722
|
+
if (t) {
|
|
707
723
|
const e = document.createElement("img");
|
|
708
|
-
e.src =
|
|
724
|
+
e.src = t, e.className = "w-full h-full object-cover", e.alt = o.country || "flag", e.loading = "lazy", e.referrerPolicy = "no-referrer", r.appendChild(e);
|
|
709
725
|
}
|
|
710
|
-
|
|
726
|
+
c.appendChild(r);
|
|
711
727
|
const l = document.createElement("span");
|
|
712
|
-
l.textContent = o.code,
|
|
713
|
-
const
|
|
714
|
-
if (
|
|
715
|
-
this.dropdown = document.createElement("div"), this.dropdown.className = "telpick-dropdown", this.dropdown.onclick = (
|
|
728
|
+
l.textContent = o.code, c.appendChild(l);
|
|
729
|
+
const s = document.createElement("span");
|
|
730
|
+
if (s.className = "ml-auto", s.textContent = "▼", c.appendChild(s), this.container.appendChild(c), this.isDropdownOpen) {
|
|
731
|
+
this.dropdown = document.createElement("div"), this.dropdown.className = "telpick-dropdown", this.dropdown.onclick = (n) => n.stopPropagation(), this.dropdown.onmousedown = (n) => n.stopPropagation();
|
|
716
732
|
const e = document.createElement("input");
|
|
717
|
-
e.className = "telpick-search", e.type = "text", e.placeholder = "Buscar país...", e.value = this.searchQuery, e.oninput = (
|
|
733
|
+
e.className = "telpick-search", e.type = "text", e.placeholder = "Buscar país...", e.value = this.searchQuery, e.oninput = (n) => {
|
|
718
734
|
var h;
|
|
719
|
-
|
|
720
|
-
const d =
|
|
721
|
-
this.searchQuery =
|
|
722
|
-
const
|
|
723
|
-
|
|
724
|
-
const
|
|
725
|
-
|
|
726
|
-
this.selectedCode =
|
|
735
|
+
n.stopPropagation();
|
|
736
|
+
const d = n.target, _ = d.selectionStart || 0, f = d.value;
|
|
737
|
+
this.searchQuery = f;
|
|
738
|
+
const p = (h = this.dropdown) == null ? void 0 : h.querySelector("ul");
|
|
739
|
+
p ? (p.innerHTML = "", (this.searchQuery ? this.codes.filter((a) => a.country.toLowerCase().includes(this.searchQuery.toLowerCase())) : this.codes).forEach((a) => {
|
|
740
|
+
const g = document.createElement("li"), E = a.country_code === this.selectedCode && this.selectedCode !== null && this.selectedCode !== void 0;
|
|
741
|
+
g.className = `telpick-item ${E ? "telpick-item-selected" : ""}`, g.onclick = () => {
|
|
742
|
+
this.selectedCode = a.country_code, this.onChange(a), this.isDropdownOpen = !1, this.searchQuery = "", this.render();
|
|
727
743
|
};
|
|
728
744
|
const b = document.createElement("div");
|
|
729
745
|
b.className = "telpick-flag";
|
|
730
|
-
const C = this._getFlagUrl(
|
|
746
|
+
const C = this._getFlagUrl(a.flag, a.country_code);
|
|
731
747
|
if (C) {
|
|
732
|
-
const
|
|
733
|
-
|
|
748
|
+
const y = document.createElement("img");
|
|
749
|
+
y.src = C, y.className = "w-full h-full object-cover", y.alt = a.country || "flag", y.loading = "lazy", y.referrerPolicy = "no-referrer", b.appendChild(y);
|
|
734
750
|
}
|
|
735
|
-
|
|
751
|
+
g.appendChild(b);
|
|
736
752
|
const A = document.createElement("span");
|
|
737
|
-
A.textContent =
|
|
753
|
+
A.textContent = a.country, g.appendChild(A);
|
|
738
754
|
const m = document.createElement("span");
|
|
739
|
-
m.className = "ml-auto", m.textContent =
|
|
755
|
+
m.className = "ml-auto", m.textContent = a.code, g.appendChild(m), p.appendChild(g);
|
|
740
756
|
}), requestAnimationFrame(() => {
|
|
741
757
|
if (e) {
|
|
742
758
|
e.focus();
|
|
743
|
-
const
|
|
744
|
-
e.setSelectionRange(
|
|
759
|
+
const a = Math.min(_ + 1, f.length);
|
|
760
|
+
e.setSelectionRange(a, a);
|
|
745
761
|
}
|
|
746
762
|
})) : this.render();
|
|
747
|
-
}, e.onclick = (
|
|
763
|
+
}, e.onclick = (n) => n.stopPropagation(), e.onmousedown = (n) => n.stopPropagation(), this.dropdown.appendChild(e);
|
|
748
764
|
const u = document.createElement("ul");
|
|
749
|
-
u.style.maxHeight = "130px", u.style.overflowY = "auto", (this.searchQuery ? this.codes.filter((
|
|
750
|
-
const d = document.createElement("li"), _ =
|
|
765
|
+
u.style.maxHeight = "130px", u.style.overflowY = "auto", (this.searchQuery ? this.codes.filter((n) => n.country.toLowerCase().includes(this.searchQuery.toLowerCase())) : this.codes).forEach((n) => {
|
|
766
|
+
const d = document.createElement("li"), _ = n.country_code === this.selectedCode && this.selectedCode !== null && this.selectedCode !== void 0;
|
|
751
767
|
d.className = `telpick-item ${_ ? "telpick-item-selected" : ""}`, d.setAttribute("aria-selected", _), d.onclick = () => {
|
|
752
|
-
this.selectedCode =
|
|
768
|
+
this.selectedCode = n.country_code, this.onChange(n), this.isDropdownOpen = !1, this.searchQuery = "", this.render();
|
|
753
769
|
};
|
|
754
|
-
const
|
|
755
|
-
|
|
756
|
-
const
|
|
757
|
-
if (
|
|
758
|
-
const
|
|
759
|
-
|
|
770
|
+
const f = document.createElement("div");
|
|
771
|
+
f.className = "telpick-flag";
|
|
772
|
+
const p = this._getFlagUrl(n.flag, n.country_code);
|
|
773
|
+
if (p) {
|
|
774
|
+
const a = document.createElement("img");
|
|
775
|
+
a.src = p, a.className = "w-full h-full object-cover", a.alt = n.country || "flag", a.loading = "lazy", a.referrerPolicy = "no-referrer", f.appendChild(a);
|
|
760
776
|
}
|
|
761
|
-
d.appendChild(
|
|
777
|
+
d.appendChild(f);
|
|
762
778
|
const h = document.createElement("span");
|
|
763
|
-
h.textContent =
|
|
779
|
+
h.textContent = n.country, d.appendChild(h);
|
|
764
780
|
const w = document.createElement("span");
|
|
765
|
-
w.className = "ml-auto", w.textContent =
|
|
766
|
-
}), this.dropdown.appendChild(u),
|
|
781
|
+
w.className = "ml-auto", w.textContent = n.code, d.appendChild(w), u.appendChild(d);
|
|
782
|
+
}), this.dropdown.appendChild(u), document.body.appendChild(this.dropdown), this._updateDropdownPosition(), this._scrollResizeCleanup = () => {
|
|
783
|
+
window.removeEventListener("scroll", this._boundUpdatePosition, !0), window.removeEventListener("resize", this._boundUpdatePosition);
|
|
784
|
+
}, window.addEventListener("scroll", this._boundUpdatePosition, !0), window.addEventListener("resize", this._boundUpdatePosition), requestAnimationFrame(() => {
|
|
767
785
|
e && e.focus();
|
|
768
786
|
});
|
|
769
787
|
}
|
|
770
788
|
}
|
|
771
789
|
_setupOutsideClick() {
|
|
772
|
-
this._outsideHandler && document.removeEventListener("click", this._outsideHandler), this._outsideHandler = (
|
|
773
|
-
const
|
|
774
|
-
if (this.isDropdownOpen && this.container &&
|
|
775
|
-
const o = this.container.
|
|
776
|
-
!
|
|
790
|
+
this._outsideHandler && document.removeEventListener("click", this._outsideHandler), this._outsideHandler = (c) => {
|
|
791
|
+
const r = c.target;
|
|
792
|
+
if (this.isDropdownOpen && this.container && r) {
|
|
793
|
+
const o = this.container.contains(r), t = this.dropdown && this.dropdown.contains(r);
|
|
794
|
+
!o && !t && (this.isDropdownOpen = !1, this.render());
|
|
777
795
|
}
|
|
778
796
|
}, document.addEventListener("click", this._outsideHandler, !0);
|
|
779
797
|
}
|
|
780
798
|
destroy() {
|
|
781
|
-
this._outsideHandler && document.removeEventListener("
|
|
799
|
+
this._outsideHandler && document.removeEventListener("click", this._outsideHandler), this._scrollResizeCleanup && (this._scrollResizeCleanup(), this._scrollResizeCleanup = null), this.dropdown && this.dropdown.parentNode && (this.dropdown.parentNode.removeChild(this.dropdown), this.dropdown = null), this.container.innerHTML = "";
|
|
782
800
|
}
|
|
783
801
|
}
|
|
784
802
|
window.Telpick = T;
|
package/dist/telpick.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i,w){typeof exports=="object"&&typeof module<"u"?w(exports):typeof define=="function"&&define.amd?define(["exports"],w):(i=typeof globalThis<"u"?globalThis:i||self,w(i.Telpick={}))})(this,function(i){"use strict";const w="",P=[{country:"Afganistán",code:"+93",flag:"/flags/af.png",country_code:"AF"},{country:"Albania",code:"+355",flag:"/flags/al.webp",country_code:"AL"},{country:"Alemania",code:"+49",flag:"/flags/de.webp",country_code:"DE"},{country:"Andorra",code:"+376",flag:"/flags/ad.webp",country_code:"AD"},{country:"Antigua y Barbuda",code:"+1",flag:"/flags/ag.webp",country_code:"AG"},{country:"Arabia Saudita",code:"+966",flag:"/flags/sa.webp",country_code:"SA"},{country:"Argentina",code:"+54",flag:"/flags/ar.webp",country_code:"AR"},{country:"Australia",code:"+61",flag:"/flags/au.webp",country_code:"AU"},{country:"Austria",code:"+43",flag:"/flags/at.webp",country_code:"AT"},{country:"Bahamas",code:"+1",flag:"/flags/bs.webp",country_code:"BS"},{country:"Bangladés",code:"+880",flag:"/flags/bd.webp",country_code:"BD"},{country:"Barbados",code:"+1",flag:"/flags/bb.webp",country_code:"BB"},{country:"Bélgica",code:"+32",flag:"/flags/be.webp",country_code:"BE"},{country:"Belice",code:"+501",flag:"/flags/bz.webp",country_code:"BZ"},{country:"Bielorrusia",code:"+375",flag:"/flags/by.webp",country_code:"BY"},{country:"Bolivia",code:"+591",flag:"/flags/bo.webp",country_code:"BO"},{country:"Bosnia y Herzegovina",code:"+387",flag:"/flags/ba.webp",country_code:"BA"},{country:"Brasil",code:"+55",flag:"/flags/br.webp",country_code:"BR"},{country:"Bulgaria",code:"+359",flag:"/flags/bg.webp",country_code:"BG"},{country:"Canadá",code:"+1",flag:"/flags/ca.webp",country_code:"CA"},{country:"Chile",code:"+56",flag:"/flags/cl.webp",country_code:"CL"},{country:"China",code:"+86",flag:"/flags/cn.webp",country_code:"CN"},{country:"Colombia",code:"+57",flag:"/flags/co.webp",country_code:"CO"},{country:"Corea del Sur",code:"+82",flag:"/flags/kr.webp",country_code:"KR"},{country:"Costa Rica",code:"+506",flag:"/flags/cr.webp",country_code:"CR"},{country:"Croacia",code:"+385",flag:"/flags/hr.webp",country_code:"HR"},{country:"Cuba",code:"+53",flag:"/flags/cu.webp",country_code:"CU"},{country:"Dinamarca",code:"+45",flag:"/flags/dk.webp",country_code:"DK"},{country:"Dominica",code:"+1",flag:"/flags/dm.webp",country_code:"DM"},{country:"Ecuador",code:"+593",flag:"/flags/ec.webp",country_code:"EC"},{country:"Egipto",code:"+20",flag:"/flags/eg.webp",country_code:"EG"},{country:"El Salvador",code:"+503",flag:"/flags/sv.webp",country_code:"SV"},{country:"España",code:"+34",flag:"/flags/es.webp",country_code:"ES"},{country:"Estados Unidos",code:"+1",flag:"/flags/us.webp",country_code:"US"},{country:"Estonia",code:"+372",flag:"/flags/ee.webp",country_code:"EE"},{country:"Filipinas",code:"+63",flag:"/flags/ph.webp",country_code:"PH"},{country:"Finlandia",code:"+358",flag:"/flags/fi.webp",country_code:"FI"},{country:"Francia",code:"+33",flag:"/flags/fr.webp",country_code:"FR"},{country:"Granada",code:"+1",flag:"/flags/gd.webp",country_code:"GD"},{country:"Grecia",code:"+30",flag:"/flags/gr.webp",country_code:"GR"},{country:"Guatemala",code:"+502",flag:"/flags/gt.webp",country_code:"GT"},{country:"Guyana",code:"+592",flag:"/flags/gy.webp",country_code:"GY"},{country:"Haití",code:"+509",flag:"/flags/ht.webp",country_code:"HT"},{country:"Honduras",code:"+504",flag:"/flags/hn.webp",country_code:"HN"},{country:"Hungría",code:"+36",flag:"/flags/hu.webp",country_code:"HU"},{country:"India",code:"+91",flag:"/flags/in.webp",country_code:"IN"},{country:"Indonesia",code:"+62",flag:"/flags/id.webp",country_code:"ID"},{country:"Irak",code:"+964",flag:"/flags/iq.webp",country_code:"IQ"},{country:"Irlanda",code:"+353",flag:"/flags/ie.webp",country_code:"IE"},{country:"Islandia",code:"+354",flag:"/flags/is.webp",country_code:"IS"},{country:"Israel",code:"+972",flag:"/flags/il.webp",country_code:"IL"},{country:"Italia",code:"+39",flag:"/flags/it.webp",country_code:"IT"},{country:"Jamaica",code:"+1",flag:"/flags/jm.webp",country_code:"JM"},{country:"Japón",code:"+81",flag:"/flags/jp.webp",country_code:"JP"},{country:"Kenia",code:"+254",flag:"/flags/ke.webp",country_code:"KE"},{country:"Letonia",code:"+371",flag:"/flags/lv.webp",country_code:"LV"},{country:"Lituania",code:"+370",flag:"/flags/lt.webp",country_code:"LT"},{country:"Luxemburgo",code:"+352",flag:"/flags/lu.webp",country_code:"LU"},{country:"Malasia",code:"+60",flag:"/flags/my.webp",country_code:"MY"},{country:"Malta",code:"+356",flag:"/flags/mt.webp",country_code:"MT"},{country:"México",code:"+52",flag:"/flags/mx.webp",country_code:"MX"},{country:"Mónaco",code:"+377",flag:"/flags/mc.webp",country_code:"MC"},{country:"Nicaragua",code:"+505",flag:"/flags/ni.webp",country_code:"NI"},{country:"Nigeria",code:"+234",flag:"/flags/ng.webp",country_code:"NG"},{country:"Noruega",code:"+47",flag:"/flags/no.webp",country_code:"NO"},{country:"Nueva Zelanda",code:"+64",flag:"/flags/nz.webp",country_code:"NZ"},{country:"Países Bajos",code:"+31",flag:"/flags/nl.webp",country_code:"NL"},{country:"Pakistán",code:"+92",flag:"/flags/pk.webp",country_code:"PK"},{country:"Panamá",code:"+507",flag:"/flags/pa.webp",country_code:"PA"},{country:"Paraguay",code:"+595",flag:"/flags/py.webp",country_code:"PY"},{country:"Perú",code:"+51",flag:"/flags/pe.webp",country_code:"PE"},{country:"Polonia",code:"+48",flag:"/flags/pl.webp",country_code:"PL"},{country:"Portugal",code:"+351",flag:"/flags/pt.webp",country_code:"PT"},{country:"Reino Unido",code:"+44",flag:"/flags/gb.webp",country_code:"GB"},{country:"República Checa",code:"+420",flag:"/flags/cz.webp",country_code:"CZ"},{country:"República Dominicana",code:"+1",flag:"/flags/do.webp",country_code:"DO"},{country:"Rumanía",code:"+40",flag:"/flags/ro.webp",country_code:"RO"},{country:"Rusia",code:"+7",flag:"/flags/ru.webp",country_code:"RU"},{country:"San Cristóbal y Nieves",code:"+1",flag:"/flags/kn.webp",country_code:"KN"},{country:"San Vicente y las Granadinas",code:"+1",flag:"/flags/vc.webp",country_code:"VC"},{country:"Santa Lucía",code:"+1",flag:"/flags/lc.webp",country_code:"LC"},{country:"Singapur",code:"+65",flag:"/flags/sg.webp",country_code:"SG"},{country:"Sudáfrica",code:"+27",flag:"/flags/za.webp",country_code:"ZA"},{country:"Suecia",code:"+46",flag:"/flags/se.webp",country_code:"SE"},{country:"Suiza",code:"+41",flag:"/flags/ch.webp",country_code:"CH"},{country:"Surinam",code:"+597",flag:"/flags/sr.webp",country_code:"SR"},{country:"Tailandia",code:"+66",flag:"/flags/th.webp",country_code:"TH"},{country:"Trinidad y Tobago",code:"+1",flag:"/flags/tt.webp",country_code:"TT"},{country:"Ucrania",code:"+380",flag:"/flags/ua.webp",country_code:"UA"},{country:"Uruguay",code:"+598",flag:"/flags/uy.webp",country_code:"UY"},{country:"Venezuela",code:"+58",flag:"/flags/ve.webp",country_code:"VE"},{country:"Vietnam",code:"+84",flag:"/flags/vn.webp",country_code:"VN"}];(function(){if(typeof document<"u"&&document.currentScript&&document.currentScript.src){const u=document.currentScript.src.replace(/\/[^/]*$/,"");typeof globalThis<"u"?globalThis.__TELPICK_SCRIPT_BASE__=u:typeof window<"u"&&(window.__TELPICK_SCRIPT_BASE__=u)}})();function k(){const u=typeof globalThis<"u"&&globalThis.__TELPICK_SCRIPT_BASE__?globalThis.__TELPICK_SCRIPT_BASE__:typeof window<"u"&&window.__TELPICK_SCRIPT_BASE__||"";return u?`${u.replace(/\/$/,"")}/assets/flags`:""}class E{constructor({code:a=null,onChange:n=()=>{},styleOverrides:o={},baseFlagUrl:s=""}={}){this.code=a,this.onChange=n,this.styleOverrides=o,this.baseFlagUrl=s||k(),this.codes=[],this.selectedCode=a,this.isDropdownOpen=!1,this.searchQuery="",this.container=null,this.dropdown=null,this._outsideHandler=null,this._getFlagUrl=this._getFlagUrl.bind(this)}_getFlagUrl(a,n){const o=a?a.replace(/^.*\//,""):n?`${String(n).toLowerCase()}.webp`:"";return o?this.baseFlagUrl?`${this.baseFlagUrl.replace(/\/$/,"")}/${o}`:a||(n?`/flags/${String(n).toLowerCase()}.webp`:""):""}async init(a){if(this.container=a,this.codes=[...P].sort((o,s)=>o.country.localeCompare(s.country,"es")),this.code){const o=this.codes.find(s=>s.country_code===this.code);o&&(this.selectedCode=o.country_code)}else{const o=[async()=>{try{const r=await(await fetch("https://ip-api.com/json/?fields=countryCode")).json();if(r.countryCode)return this.codes.find(e=>e.country_code===r.countryCode)}catch{}return null},async()=>{try{const r=await(await fetch("https://get.geojs.io/v1/ip/country.json")).json();if(r.country)return this.codes.find(e=>e.country_code===r.country)}catch{}return null},async()=>{try{const r=await(await fetch("https://ipapi.co/json/")).json();if(r.country_code)return this.codes.find(e=>e.country_code===r.country_code)}catch{}return null}];let s=null;for(const l of o)try{if(s=await Promise.race([l(),new Promise(r=>setTimeout(()=>r(null),3e3))]),s)break}catch{}if(!s)try{const l=Intl.DateTimeFormat().resolvedOptions().timeZone,e={"America/Lima":"PE","America/Bogota":"CO","America/Mexico_City":"MX","America/Argentina/Buenos_Aires":"AR","America/Santiago":"CL","America/Caracas":"VE","America/Montevideo":"UY","America/Asuncion":"PY","America/La_Paz":"BO","America/Guayaquil":"EC","America/Panama":"PA","America/Costa_Rica":"CR","America/Managua":"NI","America/Tegucigalpa":"HN","America/Guatemala":"GT","America/El_Salvador":"SV","America/Havana":"CU","America/Santo_Domingo":"DO","America/Jamaica":"JM","America/Port-au-Prince":"HT","Europe/Madrid":"ES","Europe/London":"GB","Europe/Paris":"FR","Europe/Berlin":"DE","Europe/Rome":"IT","Europe/Amsterdam":"NL","Europe/Brussels":"BE","Europe/Vienna":"AT","Europe/Zurich":"CH","Europe/Stockholm":"SE","Europe/Oslo":"NO","Europe/Copenhagen":"DK","Europe/Helsinki":"FI","Europe/Warsaw":"PL","Europe/Prague":"CZ","Europe/Bucharest":"RO","Europe/Moscow":"RU","America/New_York":"US","America/Chicago":"US","America/Denver":"US","America/Los_Angeles":"US","America/Toronto":"CA","America/Vancouver":"CA","Asia/Tokyo":"JP","Asia/Shanghai":"CN","Asia/Hong_Kong":"CN","Asia/Seoul":"KR","Asia/Singapore":"SG","Asia/Bangkok":"TH","Asia/Jakarta":"ID","Asia/Manila":"PH","Asia/Kolkata":"IN","Asia/Dubai":"AE","Asia/Riyadh":"SA","Australia/Sydney":"AU","Australia/Melbourne":"AU","Pacific/Auckland":"NZ","Africa/Cairo":"EG","Africa/Johannesburg":"ZA","Africa/Nairobi":"KE","Africa/Lagos":"NG"}[l];e&&(s=this.codes.find(f=>f.country_code===e))}catch{}if(s)this.selectedCode=s.country_code;else{const l=this.codes.find(r=>r.country_code==="CO");l&&(this.selectedCode=l.country_code)}}this.render();const n=this.codes.find(o=>o.country_code===this.selectedCode);n&&this.onChange(n),this._setupOutsideClick()}render(){this.container.innerHTML="";const a=document.createElement("button");a.className="telpick-btn",Object.assign(a.style,this.styleOverrides),a.onclick=()=>{this.isDropdownOpen=!this.isDropdownOpen,this.render()};const n=document.createElement("div");n.className="telpick-flag";const o=this.codes.find(e=>e.country_code===this.selectedCode)||{country_code:"",code:"",country:"",flag:""},s=this._getFlagUrl(o.flag,o.country_code);if(s){const e=document.createElement("img");e.src=s,e.className="w-full h-full object-cover",e.alt=o.country||"flag",e.loading="lazy",e.referrerPolicy="no-referrer",n.appendChild(e)}a.appendChild(n);const l=document.createElement("span");l.textContent=o.code,a.appendChild(l);const r=document.createElement("span");if(r.className="ml-auto",r.textContent="▼",a.appendChild(r),this.container.appendChild(a),this.isDropdownOpen){this.dropdown=document.createElement("div"),this.dropdown.className="telpick-dropdown",this.dropdown.onclick=c=>c.stopPropagation(),this.dropdown.onmousedown=c=>c.stopPropagation();const e=document.createElement("input");e.className="telpick-search",e.type="text",e.placeholder="Buscar país...",e.value=this.searchQuery,e.oninput=c=>{var _;c.stopPropagation();const d=c.target,b=d.selectionStart||0,g=d.value;this.searchQuery=g;const y=(_=this.dropdown)==null?void 0:_.querySelector("ul");y?(y.innerHTML="",(this.searchQuery?this.codes.filter(t=>t.country.toLowerCase().includes(this.searchQuery.toLowerCase())):this.codes).forEach(t=>{const p=document.createElement("li"),B=t.country_code===this.selectedCode&&this.selectedCode!==null&&this.selectedCode!==void 0;p.className=`telpick-item ${B?"telpick-item-selected":""}`,p.onclick=()=>{this.selectedCode=t.country_code,this.onChange(t),this.isDropdownOpen=!1,this.searchQuery="",this.render()};const C=document.createElement("div");C.className="telpick-flag";const S=this._getFlagUrl(t.flag,t.country_code);if(S){const h=document.createElement("img");h.src=S,h.className="w-full h-full object-cover",h.alt=t.country||"flag",h.loading="lazy",h.referrerPolicy="no-referrer",C.appendChild(h)}p.appendChild(C);const T=document.createElement("span");T.textContent=t.country,p.appendChild(T);const A=document.createElement("span");A.className="ml-auto",A.textContent=t.code,p.appendChild(A),y.appendChild(p)}),requestAnimationFrame(()=>{if(e){e.focus();const t=Math.min(b+1,g.length);e.setSelectionRange(t,t)}})):this.render()},e.onclick=c=>c.stopPropagation(),e.onmousedown=c=>c.stopPropagation(),this.dropdown.appendChild(e);const f=document.createElement("ul");f.style.maxHeight="130px",f.style.overflowY="auto",(this.searchQuery?this.codes.filter(c=>c.country.toLowerCase().includes(this.searchQuery.toLowerCase())):this.codes).forEach(c=>{const d=document.createElement("li"),b=c.country_code===this.selectedCode&&this.selectedCode!==null&&this.selectedCode!==void 0;d.className=`telpick-item ${b?"telpick-item-selected":""}`,d.setAttribute("aria-selected",b),d.onclick=()=>{this.selectedCode=c.country_code,this.onChange(c),this.isDropdownOpen=!1,this.searchQuery="",this.render()};const g=document.createElement("div");g.className="telpick-flag";const y=this._getFlagUrl(c.flag,c.country_code);if(y){const t=document.createElement("img");t.src=y,t.className="w-full h-full object-cover",t.alt=c.country||"flag",t.loading="lazy",t.referrerPolicy="no-referrer",g.appendChild(t)}d.appendChild(g);const _=document.createElement("span");_.textContent=c.country,d.appendChild(_);const m=document.createElement("span");m.className="ml-auto",m.textContent=c.code,d.appendChild(m),f.appendChild(d)}),this.dropdown.appendChild(f),this.container.appendChild(this.dropdown),requestAnimationFrame(()=>{e&&e.focus()})}}_setupOutsideClick(){this._outsideHandler&&document.removeEventListener("click",this._outsideHandler),this._outsideHandler=a=>{const n=a.target;if(this.isDropdownOpen&&this.container&&n){const o=this.container.querySelector(".telpick-dropdown");!this.container.contains(n)&&(!o||!o.contains(n))&&(this.isDropdownOpen=!1,this.render())}},document.addEventListener("click",this._outsideHandler,!0)}destroy(){this._outsideHandler&&document.removeEventListener("mousedown",this._outsideHandler),this.container.innerHTML=""}}window.Telpick=E,i.Telpick=E,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(l,w){typeof exports=="object"&&typeof module<"u"?w(exports):typeof define=="function"&&define.amd?define(["exports"],w):(l=typeof globalThis<"u"?globalThis:l||self,w(l.Telpick={}))})(this,function(l){"use strict";const w="",T=[{country:"Afganistán",code:"+93",flag:"/flags/af.png",country_code:"AF"},{country:"Albania",code:"+355",flag:"/flags/al.webp",country_code:"AL"},{country:"Alemania",code:"+49",flag:"/flags/de.webp",country_code:"DE"},{country:"Andorra",code:"+376",flag:"/flags/ad.webp",country_code:"AD"},{country:"Antigua y Barbuda",code:"+1",flag:"/flags/ag.webp",country_code:"AG"},{country:"Arabia Saudita",code:"+966",flag:"/flags/sa.webp",country_code:"SA"},{country:"Argentina",code:"+54",flag:"/flags/ar.webp",country_code:"AR"},{country:"Australia",code:"+61",flag:"/flags/au.webp",country_code:"AU"},{country:"Austria",code:"+43",flag:"/flags/at.webp",country_code:"AT"},{country:"Bahamas",code:"+1",flag:"/flags/bs.webp",country_code:"BS"},{country:"Bangladés",code:"+880",flag:"/flags/bd.webp",country_code:"BD"},{country:"Barbados",code:"+1",flag:"/flags/bb.webp",country_code:"BB"},{country:"Bélgica",code:"+32",flag:"/flags/be.webp",country_code:"BE"},{country:"Belice",code:"+501",flag:"/flags/bz.webp",country_code:"BZ"},{country:"Bielorrusia",code:"+375",flag:"/flags/by.webp",country_code:"BY"},{country:"Bolivia",code:"+591",flag:"/flags/bo.webp",country_code:"BO"},{country:"Bosnia y Herzegovina",code:"+387",flag:"/flags/ba.webp",country_code:"BA"},{country:"Brasil",code:"+55",flag:"/flags/br.webp",country_code:"BR"},{country:"Bulgaria",code:"+359",flag:"/flags/bg.webp",country_code:"BG"},{country:"Canadá",code:"+1",flag:"/flags/ca.webp",country_code:"CA"},{country:"Chile",code:"+56",flag:"/flags/cl.webp",country_code:"CL"},{country:"China",code:"+86",flag:"/flags/cn.webp",country_code:"CN"},{country:"Colombia",code:"+57",flag:"/flags/co.webp",country_code:"CO"},{country:"Corea del Sur",code:"+82",flag:"/flags/kr.webp",country_code:"KR"},{country:"Costa Rica",code:"+506",flag:"/flags/cr.webp",country_code:"CR"},{country:"Croacia",code:"+385",flag:"/flags/hr.webp",country_code:"HR"},{country:"Cuba",code:"+53",flag:"/flags/cu.webp",country_code:"CU"},{country:"Dinamarca",code:"+45",flag:"/flags/dk.webp",country_code:"DK"},{country:"Dominica",code:"+1",flag:"/flags/dm.webp",country_code:"DM"},{country:"Ecuador",code:"+593",flag:"/flags/ec.webp",country_code:"EC"},{country:"Egipto",code:"+20",flag:"/flags/eg.webp",country_code:"EG"},{country:"El Salvador",code:"+503",flag:"/flags/sv.webp",country_code:"SV"},{country:"España",code:"+34",flag:"/flags/es.webp",country_code:"ES"},{country:"Estados Unidos",code:"+1",flag:"/flags/us.webp",country_code:"US"},{country:"Estonia",code:"+372",flag:"/flags/ee.webp",country_code:"EE"},{country:"Filipinas",code:"+63",flag:"/flags/ph.webp",country_code:"PH"},{country:"Finlandia",code:"+358",flag:"/flags/fi.webp",country_code:"FI"},{country:"Francia",code:"+33",flag:"/flags/fr.webp",country_code:"FR"},{country:"Granada",code:"+1",flag:"/flags/gd.webp",country_code:"GD"},{country:"Grecia",code:"+30",flag:"/flags/gr.webp",country_code:"GR"},{country:"Guatemala",code:"+502",flag:"/flags/gt.webp",country_code:"GT"},{country:"Guyana",code:"+592",flag:"/flags/gy.webp",country_code:"GY"},{country:"Haití",code:"+509",flag:"/flags/ht.webp",country_code:"HT"},{country:"Honduras",code:"+504",flag:"/flags/hn.webp",country_code:"HN"},{country:"Hungría",code:"+36",flag:"/flags/hu.webp",country_code:"HU"},{country:"India",code:"+91",flag:"/flags/in.webp",country_code:"IN"},{country:"Indonesia",code:"+62",flag:"/flags/id.webp",country_code:"ID"},{country:"Irak",code:"+964",flag:"/flags/iq.webp",country_code:"IQ"},{country:"Irlanda",code:"+353",flag:"/flags/ie.webp",country_code:"IE"},{country:"Islandia",code:"+354",flag:"/flags/is.webp",country_code:"IS"},{country:"Israel",code:"+972",flag:"/flags/il.webp",country_code:"IL"},{country:"Italia",code:"+39",flag:"/flags/it.webp",country_code:"IT"},{country:"Jamaica",code:"+1",flag:"/flags/jm.webp",country_code:"JM"},{country:"Japón",code:"+81",flag:"/flags/jp.webp",country_code:"JP"},{country:"Kenia",code:"+254",flag:"/flags/ke.webp",country_code:"KE"},{country:"Letonia",code:"+371",flag:"/flags/lv.webp",country_code:"LV"},{country:"Lituania",code:"+370",flag:"/flags/lt.webp",country_code:"LT"},{country:"Luxemburgo",code:"+352",flag:"/flags/lu.webp",country_code:"LU"},{country:"Malasia",code:"+60",flag:"/flags/my.webp",country_code:"MY"},{country:"Malta",code:"+356",flag:"/flags/mt.webp",country_code:"MT"},{country:"México",code:"+52",flag:"/flags/mx.webp",country_code:"MX"},{country:"Mónaco",code:"+377",flag:"/flags/mc.webp",country_code:"MC"},{country:"Nicaragua",code:"+505",flag:"/flags/ni.webp",country_code:"NI"},{country:"Nigeria",code:"+234",flag:"/flags/ng.webp",country_code:"NG"},{country:"Noruega",code:"+47",flag:"/flags/no.webp",country_code:"NO"},{country:"Nueva Zelanda",code:"+64",flag:"/flags/nz.webp",country_code:"NZ"},{country:"Países Bajos",code:"+31",flag:"/flags/nl.webp",country_code:"NL"},{country:"Pakistán",code:"+92",flag:"/flags/pk.webp",country_code:"PK"},{country:"Panamá",code:"+507",flag:"/flags/pa.webp",country_code:"PA"},{country:"Paraguay",code:"+595",flag:"/flags/py.webp",country_code:"PY"},{country:"Perú",code:"+51",flag:"/flags/pe.webp",country_code:"PE"},{country:"Polonia",code:"+48",flag:"/flags/pl.webp",country_code:"PL"},{country:"Portugal",code:"+351",flag:"/flags/pt.webp",country_code:"PT"},{country:"Reino Unido",code:"+44",flag:"/flags/gb.webp",country_code:"GB"},{country:"República Checa",code:"+420",flag:"/flags/cz.webp",country_code:"CZ"},{country:"República Dominicana",code:"+1",flag:"/flags/do.webp",country_code:"DO"},{country:"Rumanía",code:"+40",flag:"/flags/ro.webp",country_code:"RO"},{country:"Rusia",code:"+7",flag:"/flags/ru.webp",country_code:"RU"},{country:"San Cristóbal y Nieves",code:"+1",flag:"/flags/kn.webp",country_code:"KN"},{country:"San Vicente y las Granadinas",code:"+1",flag:"/flags/vc.webp",country_code:"VC"},{country:"Santa Lucía",code:"+1",flag:"/flags/lc.webp",country_code:"LC"},{country:"Singapur",code:"+65",flag:"/flags/sg.webp",country_code:"SG"},{country:"Sudáfrica",code:"+27",flag:"/flags/za.webp",country_code:"ZA"},{country:"Suecia",code:"+46",flag:"/flags/se.webp",country_code:"SE"},{country:"Suiza",code:"+41",flag:"/flags/ch.webp",country_code:"CH"},{country:"Surinam",code:"+597",flag:"/flags/sr.webp",country_code:"SR"},{country:"Tailandia",code:"+66",flag:"/flags/th.webp",country_code:"TH"},{country:"Trinidad y Tobago",code:"+1",flag:"/flags/tt.webp",country_code:"TT"},{country:"Ucrania",code:"+380",flag:"/flags/ua.webp",country_code:"UA"},{country:"Uruguay",code:"+598",flag:"/flags/uy.webp",country_code:"UY"},{country:"Venezuela",code:"+58",flag:"/flags/ve.webp",country_code:"VE"},{country:"Vietnam",code:"+84",flag:"/flags/vn.webp",country_code:"VN"}];(function(){if(typeof document<"u"&&document.currentScript&&document.currentScript.src){const u=document.currentScript.src.replace(/\/[^/]*$/,"");typeof globalThis<"u"?globalThis.__TELPICK_SCRIPT_BASE__=u:typeof window<"u"&&(window.__TELPICK_SCRIPT_BASE__=u)}})();function v(){const u=typeof globalThis<"u"&&globalThis.__TELPICK_SCRIPT_BASE__?globalThis.__TELPICK_SCRIPT_BASE__:typeof window<"u"&&window.__TELPICK_SCRIPT_BASE__||"";return u?`${u.replace(/\/$/,"")}/assets/flags`:""}class E{constructor({code:t=null,onChange:r=()=>{},styleOverrides:o={},baseFlagUrl:c=""}={}){this.code=t,this.onChange=r,this.styleOverrides=o,this.baseFlagUrl=c||v(),this.codes=[],this.selectedCode=t,this.isDropdownOpen=!1,this.searchQuery="",this.container=null,this.dropdown=null,this._outsideHandler=null,this._scrollResizeCleanup=null,this._boundUpdatePosition=()=>this._updateDropdownPosition(),this._getFlagUrl=this._getFlagUrl.bind(this)}_updateDropdownPosition(){if(!this.dropdown||!this.container)return;const t=this.container.getBoundingClientRect();this.dropdown.style.position="fixed",this.dropdown.style.top=`${t.bottom+4}px`,this.dropdown.style.left=`${t.left}px`,this.dropdown.style.marginTop="0"}_getFlagUrl(t,r){const o=t?t.replace(/^.*\//,""):r?`${String(r).toLowerCase()}.webp`:"";if(!o&&!r)return"";if(this.baseFlagUrl){const c=this.baseFlagUrl.replace(/\/$/,"");if(c.includes("flagcdn.com")){const i=String(r||o&&o.replace(/\.[^.]+$/,"")||"").toLowerCase();return i?`${c}/${i}.png`:""}return`${c}/${o}`}return t||(r?`/flags/${String(r).toLowerCase()}.webp`:"")}async init(t){if(this.container=t,this.codes=[...T].sort((o,c)=>o.country.localeCompare(c.country,"es")),this.code){const o=this.codes.find(c=>c.country_code===this.code);o&&(this.selectedCode=o.country_code)}else{const o=[async()=>{try{const s=await(await fetch("https://ip-api.com/json/?fields=countryCode")).json();if(s.countryCode)return this.codes.find(e=>e.country_code===s.countryCode)}catch{}return null},async()=>{try{const s=await(await fetch("https://get.geojs.io/v1/ip/country.json")).json();if(s.country)return this.codes.find(e=>e.country_code===s.country)}catch{}return null},async()=>{try{const s=await(await fetch("https://ipapi.co/json/")).json();if(s.country_code)return this.codes.find(e=>e.country_code===s.country_code)}catch{}return null}];let c=null;for(const i of o)try{if(c=await Promise.race([i(),new Promise(s=>setTimeout(()=>s(null),3e3))]),c)break}catch{}if(!c)try{const i=Intl.DateTimeFormat().resolvedOptions().timeZone,e={"America/Lima":"PE","America/Bogota":"CO","America/Mexico_City":"MX","America/Argentina/Buenos_Aires":"AR","America/Santiago":"CL","America/Caracas":"VE","America/Montevideo":"UY","America/Asuncion":"PY","America/La_Paz":"BO","America/Guayaquil":"EC","America/Panama":"PA","America/Costa_Rica":"CR","America/Managua":"NI","America/Tegucigalpa":"HN","America/Guatemala":"GT","America/El_Salvador":"SV","America/Havana":"CU","America/Santo_Domingo":"DO","America/Jamaica":"JM","America/Port-au-Prince":"HT","Europe/Madrid":"ES","Europe/London":"GB","Europe/Paris":"FR","Europe/Berlin":"DE","Europe/Rome":"IT","Europe/Amsterdam":"NL","Europe/Brussels":"BE","Europe/Vienna":"AT","Europe/Zurich":"CH","Europe/Stockholm":"SE","Europe/Oslo":"NO","Europe/Copenhagen":"DK","Europe/Helsinki":"FI","Europe/Warsaw":"PL","Europe/Prague":"CZ","Europe/Bucharest":"RO","Europe/Moscow":"RU","America/New_York":"US","America/Chicago":"US","America/Denver":"US","America/Los_Angeles":"US","America/Toronto":"CA","America/Vancouver":"CA","Asia/Tokyo":"JP","Asia/Shanghai":"CN","Asia/Hong_Kong":"CN","Asia/Seoul":"KR","Asia/Singapore":"SG","Asia/Bangkok":"TH","Asia/Jakarta":"ID","Asia/Manila":"PH","Asia/Kolkata":"IN","Asia/Dubai":"AE","Asia/Riyadh":"SA","Australia/Sydney":"AU","Australia/Melbourne":"AU","Pacific/Auckland":"NZ","Africa/Cairo":"EG","Africa/Johannesburg":"ZA","Africa/Nairobi":"KE","Africa/Lagos":"NG"}[i];e&&(c=this.codes.find(f=>f.country_code===e))}catch{}if(c)this.selectedCode=c.country_code;else{const i=this.codes.find(s=>s.country_code==="CO");i&&(this.selectedCode=i.country_code)}}this.render();const r=this.codes.find(o=>o.country_code===this.selectedCode);r&&this.onChange(r),this._setupOutsideClick()}render(){this._scrollResizeCleanup&&(this._scrollResizeCleanup(),this._scrollResizeCleanup=null),this.dropdown&&this.dropdown.parentNode&&(this.dropdown.parentNode.removeChild(this.dropdown),this.dropdown=null),this.container.innerHTML="";const t=document.createElement("button");t.className="telpick-btn",Object.assign(t.style,this.styleOverrides),t.onclick=()=>{this.isDropdownOpen=!this.isDropdownOpen,this.render()};const r=document.createElement("div");r.className="telpick-flag";const o=this.codes.find(e=>e.country_code===this.selectedCode)||{country_code:"",code:"",country:"",flag:""},c=this._getFlagUrl(o.flag,o.country_code);if(c){const e=document.createElement("img");e.src=c,e.className="w-full h-full object-cover",e.alt=o.country||"flag",e.loading="lazy",e.referrerPolicy="no-referrer",r.appendChild(e)}t.appendChild(r);const i=document.createElement("span");i.textContent=o.code,t.appendChild(i);const s=document.createElement("span");if(s.className="ml-auto",s.textContent="▼",t.appendChild(s),this.container.appendChild(t),this.isDropdownOpen){this.dropdown=document.createElement("div"),this.dropdown.className="telpick-dropdown",this.dropdown.onclick=n=>n.stopPropagation(),this.dropdown.onmousedown=n=>n.stopPropagation();const e=document.createElement("input");e.className="telpick-search",e.type="text",e.placeholder="Buscar país...",e.value=this.searchQuery,e.oninput=n=>{var _;n.stopPropagation();const d=n.target,b=d.selectionStart||0,p=d.value;this.searchQuery=p;const g=(_=this.dropdown)==null?void 0:_.querySelector("ul");g?(g.innerHTML="",(this.searchQuery?this.codes.filter(a=>a.country.toLowerCase().includes(this.searchQuery.toLowerCase())):this.codes).forEach(a=>{const y=document.createElement("li"),N=a.country_code===this.selectedCode&&this.selectedCode!==null&&this.selectedCode!==void 0;y.className=`telpick-item ${N?"telpick-item-selected":""}`,y.onclick=()=>{this.selectedCode=a.country_code,this.onChange(a),this.isDropdownOpen=!1,this.searchQuery="",this.render()};const C=document.createElement("div");C.className="telpick-flag";const S=this._getFlagUrl(a.flag,a.country_code);if(S){const h=document.createElement("img");h.src=S,h.className="w-full h-full object-cover",h.alt=a.country||"flag",h.loading="lazy",h.referrerPolicy="no-referrer",C.appendChild(h)}y.appendChild(C);const P=document.createElement("span");P.textContent=a.country,y.appendChild(P);const A=document.createElement("span");A.className="ml-auto",A.textContent=a.code,y.appendChild(A),g.appendChild(y)}),requestAnimationFrame(()=>{if(e){e.focus();const a=Math.min(b+1,p.length);e.setSelectionRange(a,a)}})):this.render()},e.onclick=n=>n.stopPropagation(),e.onmousedown=n=>n.stopPropagation(),this.dropdown.appendChild(e);const f=document.createElement("ul");f.style.maxHeight="130px",f.style.overflowY="auto",(this.searchQuery?this.codes.filter(n=>n.country.toLowerCase().includes(this.searchQuery.toLowerCase())):this.codes).forEach(n=>{const d=document.createElement("li"),b=n.country_code===this.selectedCode&&this.selectedCode!==null&&this.selectedCode!==void 0;d.className=`telpick-item ${b?"telpick-item-selected":""}`,d.setAttribute("aria-selected",b),d.onclick=()=>{this.selectedCode=n.country_code,this.onChange(n),this.isDropdownOpen=!1,this.searchQuery="",this.render()};const p=document.createElement("div");p.className="telpick-flag";const g=this._getFlagUrl(n.flag,n.country_code);if(g){const a=document.createElement("img");a.src=g,a.className="w-full h-full object-cover",a.alt=n.country||"flag",a.loading="lazy",a.referrerPolicy="no-referrer",p.appendChild(a)}d.appendChild(p);const _=document.createElement("span");_.textContent=n.country,d.appendChild(_);const m=document.createElement("span");m.className="ml-auto",m.textContent=n.code,d.appendChild(m),f.appendChild(d)}),this.dropdown.appendChild(f),document.body.appendChild(this.dropdown),this._updateDropdownPosition(),this._scrollResizeCleanup=()=>{window.removeEventListener("scroll",this._boundUpdatePosition,!0),window.removeEventListener("resize",this._boundUpdatePosition)},window.addEventListener("scroll",this._boundUpdatePosition,!0),window.addEventListener("resize",this._boundUpdatePosition),requestAnimationFrame(()=>{e&&e.focus()})}}_setupOutsideClick(){this._outsideHandler&&document.removeEventListener("click",this._outsideHandler),this._outsideHandler=t=>{const r=t.target;if(this.isDropdownOpen&&this.container&&r){const o=this.container.contains(r),c=this.dropdown&&this.dropdown.contains(r);!o&&!c&&(this.isDropdownOpen=!1,this.render())}},document.addEventListener("click",this._outsideHandler,!0)}destroy(){this._outsideHandler&&document.removeEventListener("click",this._outsideHandler),this._scrollResizeCleanup&&(this._scrollResizeCleanup(),this._scrollResizeCleanup=null),this.dropdown&&this.dropdown.parentNode&&(this.dropdown.parentNode.removeChild(this.dropdown),this.dropdown=null),this.container.innerHTML=""}}window.Telpick=E,l.Telpick=E,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "telpick",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Multi-platform country and phone code selector with a modern design, smooth animations, and automatic IP detection.",
|
|
5
5
|
"main": "dist/telpick.es.js",
|
|
6
6
|
"module": "dist/telpick.es.js",
|
package/src/demo-init.js
CHANGED
|
@@ -6,12 +6,16 @@ import Telpick from './telpick.js'
|
|
|
6
6
|
|
|
7
7
|
window.Telpick = Telpick
|
|
8
8
|
|
|
9
|
+
// Base para banderas locales (src/assets/flags). En otros proyectos pasa baseFlagUrl al constructor o usa 'https://flagcdn.com/w40' si no tienes banderas.
|
|
10
|
+
const flagsBase = new URL('./assets/flags', import.meta.url).href
|
|
11
|
+
|
|
9
12
|
function initDemo() {
|
|
10
13
|
const container = document.getElementById('telpick-container')
|
|
11
14
|
if (!container) return
|
|
12
15
|
|
|
13
16
|
const telpick = new Telpick({
|
|
14
17
|
code: null,
|
|
18
|
+
baseFlagUrl: flagsBase,
|
|
15
19
|
onChange: (country) => {
|
|
16
20
|
const infoEl = document.getElementById('selected-info')
|
|
17
21
|
if (infoEl) {
|
package/src/telpick.js
CHANGED
|
@@ -29,14 +29,29 @@ class Telpick {
|
|
|
29
29
|
this.container = null
|
|
30
30
|
this.dropdown = null
|
|
31
31
|
this._outsideHandler = null
|
|
32
|
+
this._scrollResizeCleanup = null
|
|
33
|
+
this._boundUpdatePosition = () => this._updateDropdownPosition()
|
|
32
34
|
this._getFlagUrl = this._getFlagUrl.bind(this)
|
|
33
35
|
}
|
|
34
36
|
|
|
37
|
+
_updateDropdownPosition() {
|
|
38
|
+
if (!this.dropdown || !this.container) return
|
|
39
|
+
const rect = this.container.getBoundingClientRect()
|
|
40
|
+
this.dropdown.style.position = 'fixed'
|
|
41
|
+
this.dropdown.style.top = `${rect.bottom + 4}px`
|
|
42
|
+
this.dropdown.style.left = `${rect.left}px`
|
|
43
|
+
this.dropdown.style.marginTop = '0'
|
|
44
|
+
}
|
|
45
|
+
|
|
35
46
|
_getFlagUrl(flagPathFromJson, countryCode) {
|
|
36
47
|
const filename = flagPathFromJson ? flagPathFromJson.replace(/^.*\//, '') : (countryCode ? `${String(countryCode).toLowerCase()}.webp` : '')
|
|
37
|
-
if (!filename) return ''
|
|
48
|
+
if (!filename && !countryCode) return ''
|
|
38
49
|
if (this.baseFlagUrl) {
|
|
39
50
|
const base = this.baseFlagUrl.replace(/\/$/, '')
|
|
51
|
+
if (base.includes('flagcdn.com')) {
|
|
52
|
+
const code = String(countryCode || (filename && filename.replace(/\.[^.]+$/, '')) || '').toLowerCase()
|
|
53
|
+
return code ? `${base}/${code}.png` : ''
|
|
54
|
+
}
|
|
40
55
|
return `${base}/${filename}`
|
|
41
56
|
}
|
|
42
57
|
return flagPathFromJson || (countryCode ? `/flags/${String(countryCode).toLowerCase()}.webp` : '')
|
|
@@ -141,6 +156,14 @@ class Telpick {
|
|
|
141
156
|
}
|
|
142
157
|
|
|
143
158
|
render() {
|
|
159
|
+
if (this._scrollResizeCleanup) {
|
|
160
|
+
this._scrollResizeCleanup()
|
|
161
|
+
this._scrollResizeCleanup = null
|
|
162
|
+
}
|
|
163
|
+
if (this.dropdown && this.dropdown.parentNode) {
|
|
164
|
+
this.dropdown.parentNode.removeChild(this.dropdown)
|
|
165
|
+
this.dropdown = null
|
|
166
|
+
}
|
|
144
167
|
this.container.innerHTML = ''
|
|
145
168
|
const btn = document.createElement('button')
|
|
146
169
|
btn.className = 'telpick-btn'
|
|
@@ -279,8 +302,15 @@ class Telpick {
|
|
|
279
302
|
ul.appendChild(li)
|
|
280
303
|
})
|
|
281
304
|
this.dropdown.appendChild(ul)
|
|
282
|
-
|
|
283
|
-
|
|
305
|
+
document.body.appendChild(this.dropdown)
|
|
306
|
+
this._updateDropdownPosition()
|
|
307
|
+
this._scrollResizeCleanup = () => {
|
|
308
|
+
window.removeEventListener('scroll', this._boundUpdatePosition, true)
|
|
309
|
+
window.removeEventListener('resize', this._boundUpdatePosition)
|
|
310
|
+
}
|
|
311
|
+
window.addEventListener('scroll', this._boundUpdatePosition, true)
|
|
312
|
+
window.addEventListener('resize', this._boundUpdatePosition)
|
|
313
|
+
|
|
284
314
|
requestAnimationFrame(() => {
|
|
285
315
|
if (input) {
|
|
286
316
|
input.focus()
|
|
@@ -294,8 +324,9 @@ class Telpick {
|
|
|
294
324
|
this._outsideHandler = e => {
|
|
295
325
|
const target = e.target
|
|
296
326
|
if (this.isDropdownOpen && this.container && target) {
|
|
297
|
-
const
|
|
298
|
-
|
|
327
|
+
const inContainer = this.container.contains(target)
|
|
328
|
+
const inDropdown = this.dropdown && this.dropdown.contains(target)
|
|
329
|
+
if (!inContainer && !inDropdown) {
|
|
299
330
|
this.isDropdownOpen = false
|
|
300
331
|
this.render()
|
|
301
332
|
}
|
|
@@ -305,7 +336,15 @@ class Telpick {
|
|
|
305
336
|
}
|
|
306
337
|
|
|
307
338
|
destroy() {
|
|
308
|
-
if (this._outsideHandler) document.removeEventListener('
|
|
339
|
+
if (this._outsideHandler) document.removeEventListener('click', this._outsideHandler)
|
|
340
|
+
if (this._scrollResizeCleanup) {
|
|
341
|
+
this._scrollResizeCleanup()
|
|
342
|
+
this._scrollResizeCleanup = null
|
|
343
|
+
}
|
|
344
|
+
if (this.dropdown && this.dropdown.parentNode) {
|
|
345
|
+
this.dropdown.parentNode.removeChild(this.dropdown)
|
|
346
|
+
this.dropdown = null
|
|
347
|
+
}
|
|
309
348
|
this.container.innerHTML = ''
|
|
310
349
|
}
|
|
311
350
|
}
|
package/src/telpick.react.tsx
CHANGED
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import React, { useState, useEffect, useRef, useMemo } from 'react'
|
|
2
|
+
import { createPortal } from 'react-dom'
|
|
2
3
|
import { getDefaultCountry, useClickOutside, CountryCode, TelpickProps } from './telpick'
|
|
3
4
|
import './telpick.css'
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
function getFlagUrl(baseFlagUrl: string, flagPath: string, countryCode: string): string {
|
|
7
|
+
const base = (baseFlagUrl || '').replace(/\/$/, '')
|
|
8
|
+
if (!base) return flagPath || (countryCode ? `https://flagcdn.com/w40/${countryCode.toLowerCase()}.png` : '')
|
|
9
|
+
if (base.includes('flagcdn.com')) {
|
|
10
|
+
const code = (countryCode || (flagPath && flagPath.replace(/^.*\//, '').replace(/\.[^.]+$/, '')) || '').toLowerCase()
|
|
11
|
+
return code ? `${base}/${code}.png` : ''
|
|
12
|
+
}
|
|
13
|
+
const filename = flagPath ? flagPath.replace(/^.*\//, '') : `${(countryCode || '').toLowerCase()}.webp`
|
|
14
|
+
return filename ? `${base}/${filename}` : ''
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function TelpickReact({ code, onChange, styleOverrides, baseFlagUrl = '' }: TelpickProps) {
|
|
6
18
|
const [codes, setCodes] = useState<CountryCode[]>([])
|
|
7
19
|
const [selectedCode, setSelectedCode] = useState(code)
|
|
8
20
|
const [isDropdownOpen, setDropdownOpen] = useState(false)
|
|
9
21
|
const [searchQuery, setSearchQuery] = useState('')
|
|
10
|
-
const
|
|
22
|
+
const [dropdownPosition, setDropdownPosition] = useState({ top: 0, left: 0 })
|
|
23
|
+
const wrapperRef = useRef<HTMLDivElement>(null)
|
|
24
|
+
const buttonRef = useRef<HTMLButtonElement>(null)
|
|
25
|
+
const portalDropdownRef = useRef<HTMLDivElement>(null)
|
|
11
26
|
const searchInputRef = useRef<HTMLInputElement>(null)
|
|
12
27
|
|
|
13
28
|
useEffect(() => {
|
|
@@ -35,12 +50,31 @@ export function TelpickReact({ code, onChange, styleOverrides }: TelpickProps) {
|
|
|
35
50
|
}
|
|
36
51
|
}, [code])
|
|
37
52
|
|
|
53
|
+
function updateDropdownPosition() {
|
|
54
|
+
if (buttonRef.current) {
|
|
55
|
+
const rect = buttonRef.current.getBoundingClientRect()
|
|
56
|
+
setDropdownPosition({ top: rect.bottom + 4, left: rect.left })
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (isDropdownOpen) {
|
|
62
|
+
updateDropdownPosition()
|
|
63
|
+
window.addEventListener('scroll', updateDropdownPosition, true)
|
|
64
|
+
window.addEventListener('resize', updateDropdownPosition)
|
|
65
|
+
return () => {
|
|
66
|
+
window.removeEventListener('scroll', updateDropdownPosition, true)
|
|
67
|
+
window.removeEventListener('resize', updateDropdownPosition)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}, [isDropdownOpen])
|
|
71
|
+
|
|
38
72
|
useEffect(() => {
|
|
39
|
-
if (
|
|
40
|
-
const remove = useClickOutside(
|
|
73
|
+
if (wrapperRef.current) {
|
|
74
|
+
const remove = useClickOutside(wrapperRef.current, () => setDropdownOpen(false), () => portalDropdownRef.current)
|
|
41
75
|
return remove
|
|
42
76
|
}
|
|
43
|
-
}, [
|
|
77
|
+
}, [])
|
|
44
78
|
|
|
45
79
|
useEffect(() => {
|
|
46
80
|
if (isDropdownOpen && searchInputRef.current) {
|
|
@@ -80,9 +114,58 @@ export function TelpickReact({ code, onChange, styleOverrides }: TelpickProps) {
|
|
|
80
114
|
})
|
|
81
115
|
}
|
|
82
116
|
|
|
117
|
+
const dropdownContent = isDropdownOpen ? (
|
|
118
|
+
<div
|
|
119
|
+
ref={portalDropdownRef}
|
|
120
|
+
className="telpick-dropdown"
|
|
121
|
+
role="listbox"
|
|
122
|
+
style={{
|
|
123
|
+
position: 'fixed',
|
|
124
|
+
top: dropdownPosition.top,
|
|
125
|
+
left: dropdownPosition.left,
|
|
126
|
+
marginTop: 0
|
|
127
|
+
}}
|
|
128
|
+
onClick={(e) => e.stopPropagation()}
|
|
129
|
+
onMouseDown={(e) => e.stopPropagation()}
|
|
130
|
+
>
|
|
131
|
+
<input
|
|
132
|
+
ref={searchInputRef}
|
|
133
|
+
value={searchQuery}
|
|
134
|
+
onChange={handleSearchChange}
|
|
135
|
+
onClick={(e) => e.stopPropagation()}
|
|
136
|
+
onMouseDown={(e) => e.stopPropagation()}
|
|
137
|
+
type="text"
|
|
138
|
+
placeholder="Buscar país..."
|
|
139
|
+
className="telpick-search"
|
|
140
|
+
autoFocus
|
|
141
|
+
/>
|
|
142
|
+
<ul>
|
|
143
|
+
{filteredCodes.map((item, index) => {
|
|
144
|
+
const isSelected = item.country_code === selectedCode && selectedCode !== null && selectedCode !== undefined
|
|
145
|
+
return (
|
|
146
|
+
<li
|
|
147
|
+
key={`${item.country_code}-${index}`}
|
|
148
|
+
onClick={() => selectCode(item)}
|
|
149
|
+
className={`telpick-item ${isSelected ? 'telpick-item-selected' : ''}`}
|
|
150
|
+
role="option"
|
|
151
|
+
aria-selected={isSelected}
|
|
152
|
+
>
|
|
153
|
+
<div className="telpick-flag">
|
|
154
|
+
<img src={getFlagUrl(baseFlagUrl, item.flag, item.country_code)} alt={item.country} />
|
|
155
|
+
</div>
|
|
156
|
+
<span>{item.country}</span>
|
|
157
|
+
<span className="ml-auto">{item.code}</span>
|
|
158
|
+
</li>
|
|
159
|
+
)
|
|
160
|
+
})}
|
|
161
|
+
</ul>
|
|
162
|
+
</div>
|
|
163
|
+
) : null
|
|
164
|
+
|
|
83
165
|
return (
|
|
84
|
-
<div className="telpick-wrapper relative" ref={
|
|
166
|
+
<div className="telpick-wrapper relative" ref={wrapperRef}>
|
|
85
167
|
<button
|
|
168
|
+
ref={buttonRef}
|
|
86
169
|
onClick={() => setDropdownOpen(!isDropdownOpen)}
|
|
87
170
|
className="telpick-btn"
|
|
88
171
|
style={styleOverrides}
|
|
@@ -90,51 +173,14 @@ export function TelpickReact({ code, onChange, styleOverrides }: TelpickProps) {
|
|
|
90
173
|
aria-haspopup="listbox"
|
|
91
174
|
>
|
|
92
175
|
<div className="telpick-flag">
|
|
93
|
-
{
|
|
176
|
+
{getFlagUrl(baseFlagUrl, selectedCountry.flag, selectedCountry.country_code) && (
|
|
177
|
+
<img src={getFlagUrl(baseFlagUrl, selectedCountry.flag, selectedCountry.country_code)} alt={selectedCountry.country || 'flag'} />
|
|
178
|
+
)}
|
|
94
179
|
</div>
|
|
95
180
|
<span>{selectedCountry.code}</span>
|
|
96
181
|
<span className="telpick-arrow ml-auto">▼</span>
|
|
97
182
|
</button>
|
|
98
|
-
{
|
|
99
|
-
<div
|
|
100
|
-
className="telpick-dropdown"
|
|
101
|
-
role="listbox"
|
|
102
|
-
onClick={(e) => e.stopPropagation()}
|
|
103
|
-
onMouseDown={(e) => e.stopPropagation()}
|
|
104
|
-
>
|
|
105
|
-
<input
|
|
106
|
-
ref={searchInputRef}
|
|
107
|
-
value={searchQuery}
|
|
108
|
-
onChange={handleSearchChange}
|
|
109
|
-
onClick={(e) => e.stopPropagation()}
|
|
110
|
-
onMouseDown={(e) => e.stopPropagation()}
|
|
111
|
-
type="text"
|
|
112
|
-
placeholder="Buscar país..."
|
|
113
|
-
className="telpick-search"
|
|
114
|
-
autoFocus
|
|
115
|
-
/>
|
|
116
|
-
<ul>
|
|
117
|
-
{filteredCodes.map((item, index) => {
|
|
118
|
-
const isSelected = item.country_code === selectedCode && selectedCode !== null && selectedCode !== undefined
|
|
119
|
-
return (
|
|
120
|
-
<li
|
|
121
|
-
key={`${item.country_code}-${index}`}
|
|
122
|
-
onClick={() => selectCode(item)}
|
|
123
|
-
className={`telpick-item ${isSelected ? 'telpick-item-selected' : ''}`}
|
|
124
|
-
role="option"
|
|
125
|
-
aria-selected={isSelected}
|
|
126
|
-
>
|
|
127
|
-
<div className="telpick-flag">
|
|
128
|
-
<img src={item.flag} alt={item.country} />
|
|
129
|
-
</div>
|
|
130
|
-
<span>{item.country}</span>
|
|
131
|
-
<span className="ml-auto">{item.code}</span>
|
|
132
|
-
</li>
|
|
133
|
-
)
|
|
134
|
-
})}
|
|
135
|
-
</ul>
|
|
136
|
-
</div>
|
|
137
|
-
)}
|
|
183
|
+
{createPortal(dropdownContent, document.body)}
|
|
138
184
|
</div>
|
|
139
185
|
)
|
|
140
186
|
}
|
package/src/telpick.ts
CHANGED
|
@@ -9,6 +9,7 @@ export interface TelpickProps {
|
|
|
9
9
|
code?: string;
|
|
10
10
|
onChange?: (country: CountryCode) => void;
|
|
11
11
|
styleOverrides?: Partial<Record<string, string>>;
|
|
12
|
+
baseFlagUrl?: string;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export async function getDefaultCountry(codes: CountryCode[]): Promise<CountryCode | null> {
|
|
@@ -142,15 +143,20 @@ export async function getDefaultCountry(codes: CountryCode[]): Promise<CountryCo
|
|
|
142
143
|
return codes.find(c => c.country_code === 'CO') || null
|
|
143
144
|
}
|
|
144
145
|
|
|
145
|
-
export function useClickOutside(
|
|
146
|
+
export function useClickOutside(
|
|
147
|
+
ref: HTMLElement,
|
|
148
|
+
cb: () => void,
|
|
149
|
+
/** Cuando el dropdown se renderiza en un portal/Teleport, pasar su ref (o getter) para no cerrar al hacer click dentro */
|
|
150
|
+
ignoreRef?: HTMLElement | null | (() => HTMLElement | null)
|
|
151
|
+
) {
|
|
146
152
|
function handler(e: MouseEvent) {
|
|
147
153
|
const target = e.target as Node
|
|
148
|
-
if (ref
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
+
if (!ref || !target) return
|
|
155
|
+
const ignore = typeof ignoreRef === 'function' ? ignoreRef() : ignoreRef
|
|
156
|
+
if (ignore && ignore.contains(target)) return
|
|
157
|
+
const dropdown = ref.querySelector('.telpick-dropdown')
|
|
158
|
+
const inDropdown = dropdown ? dropdown.contains(target) : false
|
|
159
|
+
if (!ref.contains(target) && !inDropdown) cb()
|
|
154
160
|
}
|
|
155
161
|
document.addEventListener('click', handler, true)
|
|
156
162
|
return () => document.removeEventListener('click', handler, true)
|
package/src/telpick.vue.ts
CHANGED
|
@@ -6,7 +6,8 @@ export const TelpickVue = defineComponent({
|
|
|
6
6
|
name: 'TelpickVue',
|
|
7
7
|
props: {
|
|
8
8
|
code: { type: String, default: null },
|
|
9
|
-
styleOverrides: { type: Object, default: () => ({}) }
|
|
9
|
+
styleOverrides: { type: Object, default: () => ({}) },
|
|
10
|
+
baseFlagUrl: { type: String, default: '' }
|
|
10
11
|
},
|
|
11
12
|
emits: ['update:code'],
|
|
12
13
|
setup(props, { emit }) {
|
|
@@ -14,8 +15,11 @@ export const TelpickVue = defineComponent({
|
|
|
14
15
|
const selectedCode = ref(props.code)
|
|
15
16
|
const isDropdownOpen = ref(false)
|
|
16
17
|
const searchQuery = ref('')
|
|
18
|
+
const wrapperRef = ref<HTMLElement | null>(null)
|
|
19
|
+
const buttonRef = ref<HTMLElement | null>(null)
|
|
17
20
|
const dropdownRef = ref<HTMLElement | null>(null)
|
|
18
21
|
const searchInputRef = ref<HTMLInputElement | null>(null)
|
|
22
|
+
const dropdownStyle = ref<Record<string, string>>({})
|
|
19
23
|
|
|
20
24
|
const handleSearchInput = (e: Event) => {
|
|
21
25
|
const input = e.target as HTMLInputElement
|
|
@@ -49,6 +53,28 @@ export const TelpickVue = defineComponent({
|
|
|
49
53
|
)
|
|
50
54
|
})
|
|
51
55
|
|
|
56
|
+
function getFlagUrl(flagPath: string, countryCode: string) {
|
|
57
|
+
const base = (props.baseFlagUrl || '').replace(/\/$/, '')
|
|
58
|
+
if (!base) return flagPath || (countryCode ? `https://flagcdn.com/w40/${countryCode.toLowerCase()}.png` : '')
|
|
59
|
+
if (base.includes('flagcdn.com')) {
|
|
60
|
+
const code = (countryCode || (flagPath && flagPath.replace(/^.*\//, '').replace(/\.[^.]+$/, '')) || '').toLowerCase()
|
|
61
|
+
return code ? `${base}/${code}.png` : ''
|
|
62
|
+
}
|
|
63
|
+
const filename = flagPath ? flagPath.replace(/^.*\//, '') : `${(countryCode || '').toLowerCase()}.webp`
|
|
64
|
+
return filename ? `${base}/${filename}` : ''
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function updateDropdownPosition() {
|
|
68
|
+
if (!buttonRef.value) return
|
|
69
|
+
const rect = buttonRef.value.getBoundingClientRect()
|
|
70
|
+
dropdownStyle.value = {
|
|
71
|
+
position: 'fixed',
|
|
72
|
+
top: `${rect.bottom + 4}px`,
|
|
73
|
+
left: `${rect.left}px`,
|
|
74
|
+
marginTop: '0'
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
52
78
|
const filteredCodes = computed(() => {
|
|
53
79
|
if (!searchQuery.value) return codes.value
|
|
54
80
|
return codes.value.filter((c) =>
|
|
@@ -62,14 +88,28 @@ export const TelpickVue = defineComponent({
|
|
|
62
88
|
}
|
|
63
89
|
})
|
|
64
90
|
|
|
91
|
+
const scrollResizeCleanup = ref<(() => void) | null>(null)
|
|
65
92
|
watch(isDropdownOpen, async () => {
|
|
93
|
+
if (scrollResizeCleanup.value) {
|
|
94
|
+
scrollResizeCleanup.value()
|
|
95
|
+
scrollResizeCleanup.value = null
|
|
96
|
+
}
|
|
66
97
|
if (isDropdownOpen.value) {
|
|
67
98
|
await nextTick()
|
|
68
|
-
|
|
69
|
-
|
|
99
|
+
updateDropdownPosition()
|
|
100
|
+
if (searchInputRef.value) searchInputRef.value.focus()
|
|
101
|
+
const onScrollResize = () => updateDropdownPosition()
|
|
102
|
+
window.addEventListener('scroll', onScrollResize, true)
|
|
103
|
+
window.addEventListener('resize', onScrollResize)
|
|
104
|
+
scrollResizeCleanup.value = () => {
|
|
105
|
+
window.removeEventListener('scroll', onScrollResize, true)
|
|
106
|
+
window.removeEventListener('resize', onScrollResize)
|
|
70
107
|
}
|
|
71
108
|
}
|
|
72
109
|
})
|
|
110
|
+
onUnmounted(() => {
|
|
111
|
+
if (scrollResizeCleanup.value) scrollResizeCleanup.value()
|
|
112
|
+
})
|
|
73
113
|
|
|
74
114
|
onMounted(async () => {
|
|
75
115
|
const response = await fetch('/resources/api/country-codes.json')
|
|
@@ -79,10 +119,10 @@ export const TelpickVue = defineComponent({
|
|
|
79
119
|
const def = await getDefaultCountry(codes.value)
|
|
80
120
|
if (def) selectCode(def)
|
|
81
121
|
}
|
|
82
|
-
if (
|
|
83
|
-
const remove = useClickOutside(
|
|
122
|
+
if (wrapperRef.value) {
|
|
123
|
+
const remove = useClickOutside(wrapperRef.value, () => {
|
|
84
124
|
isDropdownOpen.value = false
|
|
85
|
-
})
|
|
125
|
+
}, () => dropdownRef.value)
|
|
86
126
|
onUnmounted(remove)
|
|
87
127
|
}
|
|
88
128
|
})
|
|
@@ -95,14 +135,19 @@ export const TelpickVue = defineComponent({
|
|
|
95
135
|
selectCode,
|
|
96
136
|
selectedCountry,
|
|
97
137
|
filteredCodes,
|
|
138
|
+
wrapperRef,
|
|
139
|
+
buttonRef,
|
|
98
140
|
dropdownRef,
|
|
141
|
+
dropdownStyle,
|
|
99
142
|
searchInputRef,
|
|
100
|
-
handleSearchInput
|
|
143
|
+
handleSearchInput,
|
|
144
|
+
getFlagUrl
|
|
101
145
|
}
|
|
102
146
|
},
|
|
103
147
|
template: `
|
|
104
|
-
<div class="telpick-wrapper relative" ref="
|
|
148
|
+
<div class="telpick-wrapper relative" ref="wrapperRef">
|
|
105
149
|
<button
|
|
150
|
+
ref="buttonRef"
|
|
106
151
|
@click="isDropdownOpen = !isDropdownOpen"
|
|
107
152
|
class="telpick-btn"
|
|
108
153
|
:style="styleOverrides"
|
|
@@ -110,35 +155,37 @@ export const TelpickVue = defineComponent({
|
|
|
110
155
|
aria-haspopup="listbox"
|
|
111
156
|
>
|
|
112
157
|
<div class="telpick-flag">
|
|
113
|
-
<img v-if="selectedCountry.flag" :src="selectedCountry.flag" :alt="selectedCountry.country || 'flag'" />
|
|
158
|
+
<img v-if="getFlagUrl(selectedCountry.flag, selectedCountry.country_code)" :src="getFlagUrl(selectedCountry.flag, selectedCountry.country_code)" :alt="selectedCountry.country || 'flag'" />
|
|
114
159
|
</div>
|
|
115
160
|
<span>{{ selectedCountry.code }}</span>
|
|
116
161
|
<span class="telpick-arrow ml-auto">▼</span>
|
|
117
162
|
</button>
|
|
118
|
-
<
|
|
119
|
-
<
|
|
120
|
-
<
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
<
|
|
133
|
-
<
|
|
134
|
-
<
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
163
|
+
<Teleport to="body">
|
|
164
|
+
<Transition name="telpick-dropdown">
|
|
165
|
+
<div v-if="isDropdownOpen" ref="dropdownRef" class="telpick-dropdown" role="listbox" :style="dropdownStyle" @click.stop @mousedown.stop>
|
|
166
|
+
<input
|
|
167
|
+
ref="searchInputRef"
|
|
168
|
+
v-model="searchQuery"
|
|
169
|
+
type="text"
|
|
170
|
+
placeholder="Buscar país..."
|
|
171
|
+
class="telpick-search"
|
|
172
|
+
@input="handleSearchInput"
|
|
173
|
+
@click.stop
|
|
174
|
+
@mousedown.stop
|
|
175
|
+
autofocus
|
|
176
|
+
/>
|
|
177
|
+
<ul>
|
|
178
|
+
<li v-for="(item, index) in filteredCodes" :key="\`\${item.country_code}-\${index}\`" @click="selectCode(item)" :class="['telpick-item', { 'telpick-item-selected': item.country_code === selectedCode && selectedCode !== null && selectedCode !== undefined }]" role="option" :aria-selected="item.country_code === selectedCode && selectedCode !== null && selectedCode !== undefined">
|
|
179
|
+
<div class="telpick-flag">
|
|
180
|
+
<img :src="getFlagUrl(item.flag, item.country_code)" :alt="item.country" />
|
|
181
|
+
</div>
|
|
182
|
+
<span>{{ item.country }}</span>
|
|
183
|
+
<span class="ml-auto">{{ item.code }}</span>
|
|
184
|
+
</li>
|
|
185
|
+
</ul>
|
|
186
|
+
</div>
|
|
187
|
+
</Transition>
|
|
188
|
+
</Teleport>
|
|
142
189
|
</div>
|
|
143
190
|
`
|
|
144
191
|
})
|
|
File without changes
|
|
Binary file
|
|
File without changes
|
|
Binary file
|