lite-phone-input 0.3.0 → 0.5.0
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 +30 -0
- package/dist/core/index.cjs +27 -2
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +3 -1
- package/dist/core/index.d.ts +3 -1
- package/dist/core/index.js +27 -2
- package/dist/core/index.js.map +1 -1
- package/dist/preact/index.cjs +54 -10
- package/dist/preact/index.cjs.map +1 -1
- package/dist/preact/index.d.cts +1 -0
- package/dist/preact/index.d.ts +1 -0
- package/dist/preact/index.js +54 -10
- package/dist/preact/index.js.map +1 -1
- package/dist/react/index.cjs +54 -10
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +1 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +54 -10
- package/dist/react/index.js.map +1 -1
- package/dist/vanilla/index.cjs +52 -10
- package/dist/vanilla/index.cjs.map +1 -1
- package/dist/vanilla/index.d.cts +3 -0
- package/dist/vanilla/index.d.ts +3 -0
- package/dist/vanilla/index.global.js +52 -10
- package/dist/vanilla/index.global.js.map +1 -1
- package/dist/vanilla/index.js +52 -10
- package/dist/vanilla/index.js.map +1 -1
- package/package.json +1 -1
package/dist/vanilla/index.d.cts
CHANGED
|
@@ -42,6 +42,7 @@ interface PhoneInputOptions {
|
|
|
42
42
|
initialValue?: string;
|
|
43
43
|
containerClass?: string;
|
|
44
44
|
dropdownContainer?: HTMLElement;
|
|
45
|
+
geoIpLookup?: (callback: (countryCode: string | null) => void) => void;
|
|
45
46
|
onChange?: (e164: string, country: Country, validation: ValidationResult) => void;
|
|
46
47
|
onCountryChange?: (country: Country) => void;
|
|
47
48
|
onValidationChange?: (validation: ValidationResult) => void;
|
|
@@ -56,6 +57,7 @@ declare class PhoneInput {
|
|
|
56
57
|
private selectedCountry;
|
|
57
58
|
private nationalDigits;
|
|
58
59
|
private displayInternational;
|
|
60
|
+
private userHasInteracted;
|
|
59
61
|
private get isNationalInput();
|
|
60
62
|
private lastValidation;
|
|
61
63
|
private dropdown;
|
|
@@ -83,6 +85,7 @@ declare class PhoneInput {
|
|
|
83
85
|
validate(): ValidationResult;
|
|
84
86
|
setOptions(opts: Partial<PhoneInputOptions>): void;
|
|
85
87
|
destroy(): void;
|
|
88
|
+
private invokeGeoIpLookup;
|
|
86
89
|
private buildDOM;
|
|
87
90
|
private buildTrigger;
|
|
88
91
|
private rebuildTrigger;
|
package/dist/vanilla/index.d.ts
CHANGED
|
@@ -42,6 +42,7 @@ interface PhoneInputOptions {
|
|
|
42
42
|
initialValue?: string;
|
|
43
43
|
containerClass?: string;
|
|
44
44
|
dropdownContainer?: HTMLElement;
|
|
45
|
+
geoIpLookup?: (callback: (countryCode: string | null) => void) => void;
|
|
45
46
|
onChange?: (e164: string, country: Country, validation: ValidationResult) => void;
|
|
46
47
|
onCountryChange?: (country: Country) => void;
|
|
47
48
|
onValidationChange?: (validation: ValidationResult) => void;
|
|
@@ -56,6 +57,7 @@ declare class PhoneInput {
|
|
|
56
57
|
private selectedCountry;
|
|
57
58
|
private nationalDigits;
|
|
58
59
|
private displayInternational;
|
|
60
|
+
private userHasInteracted;
|
|
59
61
|
private get isNationalInput();
|
|
60
62
|
private lastValidation;
|
|
61
63
|
private dropdown;
|
|
@@ -83,6 +85,7 @@ declare class PhoneInput {
|
|
|
83
85
|
validate(): ValidationResult;
|
|
84
86
|
setOptions(opts: Partial<PhoneInputOptions>): void;
|
|
85
87
|
destroy(): void;
|
|
88
|
+
private invokeGeoIpLookup;
|
|
86
89
|
private buildDOM;
|
|
87
90
|
private buildTrigger;
|
|
88
91
|
private rebuildTrigger;
|
|
@@ -2831,7 +2831,27 @@ var LitePhoneInput = (() => {
|
|
|
2831
2831
|
// src/core/format.ts
|
|
2832
2832
|
var TRAILING_SEP = /[\s\-]+$/;
|
|
2833
2833
|
var FALLBACK_GROUP = /(.{4})(?=.)/g;
|
|
2834
|
-
var
|
|
2834
|
+
var NUMERAL_BASES = [
|
|
2835
|
+
1632,
|
|
2836
|
+
// Arabic-Indic ٠-٩
|
|
2837
|
+
1776,
|
|
2838
|
+
// Persian ۰-۹
|
|
2839
|
+
2406,
|
|
2840
|
+
// Devanagari ०-९
|
|
2841
|
+
2534,
|
|
2842
|
+
// Bengali ০-৯
|
|
2843
|
+
3664,
|
|
2844
|
+
// Thai ๐-๙
|
|
2845
|
+
3792,
|
|
2846
|
+
// Lao ໐-໙
|
|
2847
|
+
4160,
|
|
2848
|
+
// Myanmar ၀-၉
|
|
2849
|
+
6112,
|
|
2850
|
+
// Khmer ០-៩
|
|
2851
|
+
65296
|
|
2852
|
+
// Fullwidth 0-9
|
|
2853
|
+
];
|
|
2854
|
+
var NON_ASCII_DIGITS = /[\u0660-\u0669\u06F0-\u06F9\u0966-\u096F\u09E6-\u09EF\u0E50-\u0E59\u0ED0-\u0ED9\u1040-\u1049\u17E0-\u17E9\uFF10-\uFF19]/g;
|
|
2835
2855
|
function isRelevantChar(ch) {
|
|
2836
2856
|
const c = ch.charCodeAt(0);
|
|
2837
2857
|
return c >= 48 && c <= 57 || c === 43;
|
|
@@ -2881,9 +2901,18 @@ var LitePhoneInput = (() => {
|
|
|
2881
2901
|
function normalizeNumerals(value) {
|
|
2882
2902
|
return value.replace(NON_ASCII_DIGITS, (c) => {
|
|
2883
2903
|
const code = c.charCodeAt(0);
|
|
2884
|
-
|
|
2904
|
+
for (const base of NUMERAL_BASES) {
|
|
2905
|
+
if (code >= base && code <= base + 9) {
|
|
2906
|
+
return String(code - base);
|
|
2907
|
+
}
|
|
2908
|
+
}
|
|
2909
|
+
return c;
|
|
2885
2910
|
});
|
|
2886
2911
|
}
|
|
2912
|
+
function isNonAsciiDigit(ch) {
|
|
2913
|
+
const code = ch.charCodeAt(0);
|
|
2914
|
+
return NUMERAL_BASES.some((base) => code >= base && code <= base + 9);
|
|
2915
|
+
}
|
|
2887
2916
|
|
|
2888
2917
|
// src/core/countries.ts
|
|
2889
2918
|
function getFlag(code) {
|
|
@@ -3296,6 +3325,7 @@ var LitePhoneInput = (() => {
|
|
|
3296
3325
|
constructor(el, options) {
|
|
3297
3326
|
this.nationalDigits = "";
|
|
3298
3327
|
this.displayInternational = false;
|
|
3328
|
+
this.userHasInteracted = false;
|
|
3299
3329
|
this.lastValidation = null;
|
|
3300
3330
|
this.dropdown = null;
|
|
3301
3331
|
this.dialCodeEl = null;
|
|
@@ -3313,6 +3343,7 @@ var LitePhoneInput = (() => {
|
|
|
3313
3343
|
if (this.opts.initialValue) {
|
|
3314
3344
|
this.setValueInternal(this.opts.initialValue, true);
|
|
3315
3345
|
}
|
|
3346
|
+
this.invokeGeoIpLookup();
|
|
3316
3347
|
}
|
|
3317
3348
|
}
|
|
3318
3349
|
get isNationalInput() {
|
|
@@ -3350,8 +3381,9 @@ var LitePhoneInput = (() => {
|
|
|
3350
3381
|
return validatePhone(this.nationalDigits, this.selectedCountry);
|
|
3351
3382
|
}
|
|
3352
3383
|
setOptions(opts) {
|
|
3384
|
+
const { geoIpLookup: _, ...rest } = opts;
|
|
3353
3385
|
const prev = { ...this.opts };
|
|
3354
|
-
Object.assign(this.opts,
|
|
3386
|
+
Object.assign(this.opts, rest);
|
|
3355
3387
|
if (opts.allowedCountries !== void 0 || opts.excludedCountries !== void 0) {
|
|
3356
3388
|
this.countries = this.filterCountries(getAllCountries());
|
|
3357
3389
|
if (!this.countries.find((c) => c.code === this.selectedCountry.code)) {
|
|
@@ -3393,6 +3425,16 @@ var LitePhoneInput = (() => {
|
|
|
3393
3425
|
}
|
|
3394
3426
|
this.el.innerHTML = "";
|
|
3395
3427
|
}
|
|
3428
|
+
invokeGeoIpLookup() {
|
|
3429
|
+
const lookup = this.opts.geoIpLookup;
|
|
3430
|
+
if (!lookup) return;
|
|
3431
|
+
this.opts.geoIpLookup = void 0;
|
|
3432
|
+
const signal = this.ac.signal;
|
|
3433
|
+
lookup((countryCode) => {
|
|
3434
|
+
if (signal.aborted || this.userHasInteracted || !countryCode) return;
|
|
3435
|
+
this.setCountry(countryCode);
|
|
3436
|
+
});
|
|
3437
|
+
}
|
|
3396
3438
|
// --- DOM Construction ---
|
|
3397
3439
|
buildDOM() {
|
|
3398
3440
|
this.containerEl = document.createElement("div");
|
|
@@ -3492,6 +3534,7 @@ var LitePhoneInput = (() => {
|
|
|
3492
3534
|
this.inputEl.addEventListener("paste", (e) => this.handlePaste(e), { signal });
|
|
3493
3535
|
}
|
|
3494
3536
|
handleInput(e) {
|
|
3537
|
+
this.userHasInteracted = true;
|
|
3495
3538
|
if (this.opts.strict !== false && e.data === "+" && this.isNationalInput) {
|
|
3496
3539
|
this.inputEl.value = this.inputEl.value.replace("+", "");
|
|
3497
3540
|
}
|
|
@@ -3507,7 +3550,7 @@ var LitePhoneInput = (() => {
|
|
|
3507
3550
|
const formatted = this.formatNationalValue(digits);
|
|
3508
3551
|
const np = this.shouldPrependPrefix ? this.selectedCountry.nationalPrefix : "";
|
|
3509
3552
|
const display = !digits && np && extractDigits(raw).length > 0 ? np : digits && np ? np + formatted : formatted;
|
|
3510
|
-
const newCursor = this.getNationalCursor(
|
|
3553
|
+
const newCursor = this.getNationalCursor(raw, oldCursor, formatted);
|
|
3511
3554
|
this.inputEl.value = display;
|
|
3512
3555
|
this.inputEl.setSelectionRange(newCursor, newCursor);
|
|
3513
3556
|
} else {
|
|
@@ -3534,7 +3577,7 @@ var LitePhoneInput = (() => {
|
|
|
3534
3577
|
} else {
|
|
3535
3578
|
formatted = this.formatNationalValue(national);
|
|
3536
3579
|
}
|
|
3537
|
-
const newCursor = getCursorPosition(
|
|
3580
|
+
const newCursor = getCursorPosition(raw, oldCursor, formatted);
|
|
3538
3581
|
this.inputEl.value = formatted;
|
|
3539
3582
|
this.inputEl.setSelectionRange(newCursor, newCursor);
|
|
3540
3583
|
}
|
|
@@ -3549,14 +3592,12 @@ var LitePhoneInput = (() => {
|
|
|
3549
3592
|
if (!this.isNationalInput && e.key === "+" && this.inputEl.selectionStart === 0) {
|
|
3550
3593
|
return;
|
|
3551
3594
|
}
|
|
3552
|
-
if (e.key.length === 1 && !/\d/.test(e.key)) {
|
|
3553
|
-
|
|
3554
|
-
if (!(code >= 1632 && code <= 1641 || code >= 1776 && code <= 1785)) {
|
|
3555
|
-
e.preventDefault();
|
|
3556
|
-
}
|
|
3595
|
+
if (e.key.length === 1 && !/\d/.test(e.key) && !isNonAsciiDigit(e.key)) {
|
|
3596
|
+
e.preventDefault();
|
|
3557
3597
|
}
|
|
3558
3598
|
}
|
|
3559
3599
|
handlePaste(e) {
|
|
3600
|
+
this.userHasInteracted = true;
|
|
3560
3601
|
e.preventDefault();
|
|
3561
3602
|
const text = e.clipboardData?.getData("text") ?? "";
|
|
3562
3603
|
if (!text) return;
|
|
@@ -3595,6 +3636,7 @@ var LitePhoneInput = (() => {
|
|
|
3595
3636
|
// --- Dropdown ---
|
|
3596
3637
|
openDropdown() {
|
|
3597
3638
|
if (this.dropdown) return;
|
|
3639
|
+
this.userHasInteracted = true;
|
|
3598
3640
|
this.dropdown = new Dropdown({
|
|
3599
3641
|
countries: this.countries,
|
|
3600
3642
|
preferredCountries: this.opts.preferredCountries ?? [],
|