@input-kit/phone 0.1.3 → 0.2.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/dist/index.d.ts CHANGED
@@ -17,8 +17,64 @@ declare function localizeCountry(country: Country | undefined, locale?: string |
17
17
  declare function localizeCountries(list: Country[], locale?: string | readonly string[]): Country[];
18
18
  declare function getCountriesByDialCode(dialCode: string): Country[];
19
19
  declare function getCountryByDialCode(dialCode: string): Country | undefined;
20
+ interface CountryOption {
21
+ value: string;
22
+ label: string;
23
+ dialCode: string;
24
+ flag: string;
25
+ }
26
+ interface GetCountryOptionsParams {
27
+ locale?: string | readonly string[];
28
+ preferredCountries?: string[];
29
+ excludeCountries?: string[];
30
+ onlyCountries?: string[];
31
+ }
32
+ declare function getCountryOptions(params?: GetCountryOptionsParams): CountryOption[];
20
33
  declare function detectCountryFromPhone(phone: string): Country | undefined;
21
34
 
35
+ type MaybeCountry = Country | null | undefined;
36
+ type ValidationReason = 'required' | 'too_short' | 'too_long' | 'invalid' | null;
37
+ interface ValidationResult {
38
+ isValid: boolean;
39
+ reason: ValidationReason;
40
+ message: string | null;
41
+ error: string | null;
42
+ }
43
+ interface ParsedPhoneValue {
44
+ country: Country | undefined;
45
+ nationalNumber: string;
46
+ e164: string | undefined;
47
+ isValid: boolean;
48
+ }
49
+ declare function cleanPhone(phone: string): string;
50
+ declare function formatPhone(phone: string, country: MaybeCountry): string;
51
+ declare function unformatPhone(formattedPhone: string): string;
52
+ declare function validatePhoneLength(phone: string, country: MaybeCountry): boolean;
53
+ declare function validatePhone(phone: string, country: MaybeCountry, customValidator?: (phone: string, country: Country | undefined) => boolean): boolean;
54
+ declare function addDialCode(phone: string, country: MaybeCountry): string;
55
+ declare function removeDialCode(phone: string, country: MaybeCountry): string;
56
+ declare function filterCountries(countries: Country[], query: string, preferredCountries?: string[]): Country[];
57
+ declare function getCountryDisplayName(country: Country | undefined): string;
58
+ declare function getPlaceholder(country: MaybeCountry): string;
59
+ declare const stripNonDigits: typeof cleanPhone;
60
+ declare function isInternationalFormat(phone: string): boolean;
61
+ declare function extractDialCode(phone: string): {
62
+ dialCode: string | null;
63
+ nationalNumber: string;
64
+ };
65
+ declare function detectCountry(phone: string): Country | null;
66
+ declare function formatPhoneNumber(phone: string, country: MaybeCountry, format?: 'national' | 'international' | 'e164'): string;
67
+ declare function parseToE164(phone: string, country: MaybeCountry): string;
68
+ declare function getNationalNumber(phone: string, country?: MaybeCountry): string;
69
+ declare function validatePhoneNumber(phone: string, country?: MaybeCountry, required?: boolean, customValidator?: (phone: string, country: Country | undefined) => boolean): ValidationResult;
70
+ declare function parsePhoneValue(phone: string, country?: MaybeCountry): ParsedPhoneValue;
71
+ declare function isPhoneNumberComplete(phone: string, country?: MaybeCountry): boolean;
72
+ declare function formatAsYouType(phone: string, country: MaybeCountry): string;
73
+ declare const normalizePhoneNumber: typeof cleanPhone;
74
+ declare function phoneNumbersEqual(left: string, right: string): boolean;
75
+ declare function getCountryDisplayLabel(country: Country, includeDialCode?: boolean): string;
76
+ declare function limitInputLength(phone: string, country?: MaybeCountry): string;
77
+
22
78
  interface PhoneInputLabels {
23
79
  selectCountry: string;
24
80
  countrySearchPlaceholder: string;
@@ -41,6 +97,7 @@ interface PhoneInputOptions {
41
97
  includeDialCode?: boolean;
42
98
  required?: boolean;
43
99
  validator?: (phone: string, country: Country | undefined) => boolean;
100
+ onValidationChange?: (state: ValidationResult) => void;
44
101
  locale?: string | readonly string[];
45
102
  labels?: Partial<PhoneInputLabels>;
46
103
  }
@@ -49,6 +106,7 @@ interface PhoneInputState {
49
106
  fullPhone: string;
50
107
  country: Country | undefined;
51
108
  isValid: boolean;
109
+ validationReason: ValidationReason;
52
110
  isOpen: boolean;
53
111
  searchQuery: string;
54
112
  labels: PhoneInputLabels;
@@ -82,17 +140,20 @@ interface UsePhoneInputReturn extends PhoneInputState, PhoneInputActions {
82
140
  countrySelectorProps: {
83
141
  onClick: () => void;
84
142
  'aria-expanded': boolean;
85
- 'aria-haspopup': true;
143
+ 'aria-haspopup': 'listbox';
86
144
  'aria-label': string;
87
145
  };
88
146
  dropdownProps: {
89
147
  role: 'listbox';
90
148
  'aria-label': string;
149
+ 'aria-activedescendant'?: string;
91
150
  };
92
151
  filteredCountries: Country[];
93
152
  countries: Country[];
94
153
  selectCountry: (country: Country) => void;
154
+ getCountryOptionId: (code: string) => string;
95
155
  getCountryOptionProps: (country: Country, index: number) => {
156
+ id: string;
96
157
  role: 'option';
97
158
  'aria-selected': boolean;
98
159
  onClick: () => void;
@@ -146,36 +207,4 @@ declare const PhoneInput: react.ForwardRefExoticComponent<PhoneInputProps & reac
146
207
  declare const DEFAULT_PHONE_INPUT_LABELS: PhoneInputLabels;
147
208
  declare function resolvePhoneInputLabels(locale?: string | readonly string[], overrides?: Partial<PhoneInputLabels>): PhoneInputLabels;
148
209
 
149
- type MaybeCountry = Country | null | undefined;
150
- declare function cleanPhone(phone: string): string;
151
- declare function formatPhone(phone: string, country: MaybeCountry): string;
152
- declare function unformatPhone(formattedPhone: string): string;
153
- declare function validatePhoneLength(phone: string, country: MaybeCountry): boolean;
154
- declare function validatePhone(phone: string, country: MaybeCountry, customValidator?: (phone: string, country: Country | undefined) => boolean): boolean;
155
- declare function addDialCode(phone: string, country: MaybeCountry): string;
156
- declare function removeDialCode(phone: string, country: MaybeCountry): string;
157
- declare function filterCountries(countries: Country[], query: string, preferredCountries?: string[]): Country[];
158
- declare function getCountryDisplayName(country: Country | undefined): string;
159
- declare function getPlaceholder(country: MaybeCountry): string;
160
- declare const stripNonDigits: typeof cleanPhone;
161
- declare function isInternationalFormat(phone: string): boolean;
162
- declare function extractDialCode(phone: string): {
163
- dialCode: string | null;
164
- nationalNumber: string;
165
- };
166
- declare function detectCountry(phone: string): Country | null;
167
- declare function formatPhoneNumber(phone: string, country: MaybeCountry, format?: 'national' | 'international' | 'e164'): string;
168
- declare function parseToE164(phone: string, country: MaybeCountry): string;
169
- declare function getNationalNumber(phone: string, country?: MaybeCountry): string;
170
- declare function validatePhoneNumber(phone: string, country?: MaybeCountry, required?: boolean): {
171
- isValid: boolean;
172
- error: string | null;
173
- };
174
- declare function isPhoneNumberComplete(phone: string, country?: MaybeCountry): boolean;
175
- declare function formatAsYouType(phone: string, country: MaybeCountry): string;
176
- declare const normalizePhoneNumber: typeof cleanPhone;
177
- declare function phoneNumbersEqual(left: string, right: string): boolean;
178
- declare function getCountryDisplayLabel(country: Country, includeDialCode?: boolean): string;
179
- declare function limitInputLength(phone: string, country?: MaybeCountry): string;
180
-
181
- export { type Country, DEFAULT_PHONE_INPUT_LABELS, PhoneInput, type PhoneInputActions, type PhoneInputLabels, type PhoneInputOptions, type PhoneInputProps, type PhoneInputRef, type PhoneInputState, type UsePhoneInputReturn, addDialCode, cleanPhone, countries, detectCountry, detectCountryFromPhone, extractDialCode, filterCountries, formatAsYouType, formatPhone, formatPhoneNumber, getCountriesByDialCode, getCountryByCode, getCountryByDialCode, getCountryDisplayLabel, getCountryDisplayName, getLocalizedCountryName, getNationalNumber, getPlaceholder, isInternationalFormat, isPhoneNumberComplete, limitInputLength, localizeCountries, localizeCountry, normalizePhoneNumber, parseToE164, phoneNumbersEqual, removeDialCode, resolvePhoneInputLabels, stripNonDigits, unformatPhone, usePhoneInput, validatePhone, validatePhoneLength, validatePhoneNumber };
210
+ export { type Country, type CountryOption, DEFAULT_PHONE_INPUT_LABELS, type GetCountryOptionsParams, type ParsedPhoneValue, PhoneInput, type PhoneInputActions, type PhoneInputLabels, type PhoneInputOptions, type PhoneInputProps, type PhoneInputRef, type PhoneInputState, type UsePhoneInputReturn, type ValidationReason, type ValidationResult, addDialCode, cleanPhone, countries, detectCountry, detectCountryFromPhone, extractDialCode, filterCountries, formatAsYouType, formatPhone, formatPhoneNumber, getCountriesByDialCode, getCountryByCode, getCountryByDialCode, getCountryDisplayLabel, getCountryDisplayName, getCountryOptions, getLocalizedCountryName, getNationalNumber, getPlaceholder, isInternationalFormat, isPhoneNumberComplete, limitInputLength, localizeCountries, localizeCountry, normalizePhoneNumber, parsePhoneValue, parseToE164, phoneNumbersEqual, removeDialCode, resolvePhoneInputLabels, stripNonDigits, unformatPhone, usePhoneInput, validatePhone, validatePhoneLength, validatePhoneNumber };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import {forwardRef,useRef,useState,useId,useCallback,useEffect,useImperativeHandle,useMemo,useLayoutEffect}from'react';import {getCountries,getCountryCallingCode,getExampleNumber,parsePhoneNumberFromString,AsYouType,isValidPhoneNumber,isPossiblePhoneNumber,validatePhoneNumberLength}from'libphonenumber-js';import Ze from'libphonenumber-js/mobile/examples';import Je from'world-countries';import {jsxs,Fragment,jsx}from'react/jsx-runtime';var et=["US","GB","CA","AU","DE","FR","IT","ES","JP","CN","IN","BR","MX","KR","NL","CH","SE","AE","SG","ZA","NG","SA","TR","ID","PH","TH","VN","MY","NZ","AR"],tt=new Set(getCountries()),nt=new Map(Je.map(e=>[e.cca2,e])),rt={AC:{name:"Ascension Island",dialCode:"+247"},TA:{name:"Tristan da Cunha",dialCode:"+290"},BQ:{name:"Caribbean Netherlands",dialCode:"+599"}};function ot(e,t){let n=e.startsWith("+")?e:`+${e}`,r=t.replace(/\D/g,"");return `${n}${r}`}function it(e){return e.toUpperCase().replace(/./g,t=>String.fromCodePoint(127397+t.charCodeAt(0)))}function at(e,t){let n=e?.idd?.root;if(!n)return [t];let o=(e.idd?.suffixes?.length?e.idd.suffixes:[""]).map(l=>ot(n,l)).filter(l=>/^\+\d+$/.test(l));return Array.from(new Set([t,...o.length?o:[]]))}function st(e){let t=getExampleNumber(e,Ze);return t?t.formatNational().replace(/\d/g,"#"):void 0}function Ee(e){let t=et.indexOf(e);return t===-1?Number.MAX_SAFE_INTEGER:t}var ut=Array.from(tt).map(e=>{let t=nt.get(e),n=rt[e],r=n?.dialCode??`+${getCountryCallingCode(e)}`,o=at(t,r);return {code:e,name:t?.name.common??n?.name??e,dialCode:r,dialCodes:o,flag:t?.flag||n?.flag||it(e),format:st(e)}}).sort((e,t)=>{let n=Ee(e.code)-Ee(t.code);return n!==0?n:e.name.localeCompare(t.name)}),R=ut,lt=new Map(R.map(e=>[e.code,e])),ct=R.reduce((e,t)=>{for(let n of t.dialCodes){let r=e.get(n)??[];r.push(t),e.set(n,r);}return e},new Map),dt=R.flatMap(e=>e.dialCodes.map(t=>({dialCode:t,country:e}))).sort((e,t)=>t.dialCode.length-e.dialCode.length),Me=new Map;function pt(e){return Array.isArray(e)?[...e]:e?[e]:[]}function ft(e){let t=pt(e);if(t.length===0||typeof Intl>"u"||typeof Intl.DisplayNames>"u")return;let n=t.join("|"),r=Me.get(n);if(r)return r;try{let o=new Intl.DisplayNames(t,{type:"region"});return Me.set(n,o),o}catch{return}}function Ct(e){let t=e.replace(/\D/g,"");return t?`+${t}`:""}function j(e){return lt.get(e.toUpperCase())}function Re(e,t){return e?ft(t)?.of(e.code)??e.displayName??e.name:void 0}function X(e,t){if(!e)return;let n=Re(e,t);return n?{...e,displayName:n}:e}function Le(e,t){return t?e.map(n=>X(n,t)??n):e}function Fe(e){return ct.get(Ct(e))??[]}function yt(e){return Fe(e)[0]}function U(e){let t=e.trim();if(!t)return;let n=t.startsWith("+")?`+${t.replace(/\D/g,"")}`:t.startsWith("00")?`+${t.slice(2).replace(/\D/g,"")}`:"";if(!n)return;let r=parsePhoneNumberFromString(n);return r?.country?j(r.country):dt.find(o=>n.startsWith(o.dialCode))?.country}var Ne={selectCountry:"Select country",countrySearchPlaceholder:"Search countries...",noCountriesFound:"No countries found",searchCountriesAriaLabel:"Search countries",countryOptionsAriaLabel:"Country options",countryButtonAriaLabel:"Open country selector"},gt={en:Ne,fr:{selectCountry:"Choisir un pays",countrySearchPlaceholder:"Rechercher un pays...",noCountriesFound:"Aucun pays trouv\xE9",searchCountriesAriaLabel:"Rechercher des pays",countryOptionsAriaLabel:"Options de pays",countryButtonAriaLabel:"Ouvrir le s\xE9lecteur de pays"},de:{selectCountry:"Land ausw\xE4hlen",countrySearchPlaceholder:"Land suchen...",noCountriesFound:"Keine L\xE4nder gefunden",searchCountriesAriaLabel:"L\xE4nder suchen",countryOptionsAriaLabel:"L\xE4nderoptionen",countryButtonAriaLabel:"L\xE4nderauswahl \xF6ffnen"},es:{selectCountry:"Seleccionar pa\xEDs",countrySearchPlaceholder:"Buscar pa\xEDs...",noCountriesFound:"No se encontraron pa\xEDses",searchCountriesAriaLabel:"Buscar pa\xEDses",countryOptionsAriaLabel:"Opciones de pa\xEDs",countryButtonAriaLabel:"Abrir selector de pa\xEDs"},pt:{selectCountry:"Selecionar pa\xEDs",countrySearchPlaceholder:"Pesquisar pa\xEDs...",noCountriesFound:"Nenhum pa\xEDs encontrado",searchCountriesAriaLabel:"Pesquisar pa\xEDses",countryOptionsAriaLabel:"Op\xE7\xF5es de pa\xEDs",countryButtonAriaLabel:"Abrir seletor de pa\xEDs"},it:{selectCountry:"Seleziona paese",countrySearchPlaceholder:"Cerca paese...",noCountriesFound:"Nessun paese trovato",searchCountriesAriaLabel:"Cerca paesi",countryOptionsAriaLabel:"Opzioni paese",countryButtonAriaLabel:"Apri selettore paese"},ja:{selectCountry:"\u56FD\u3092\u9078\u629E",countrySearchPlaceholder:"\u56FD\u3092\u691C\u7D22...",noCountriesFound:"\u56FD\u304C\u898B\u3064\u304B\u308A\u307E\u305B\u3093",searchCountriesAriaLabel:"\u56FD\u3092\u691C\u7D22",countryOptionsAriaLabel:"\u56FD\u306E\u5019\u88DC",countryButtonAriaLabel:"\u56FD\u306E\u30BB\u30EC\u30AF\u30BF\u30FC\u3092\u958B\u304F"},ko:{selectCountry:"\uAD6D\uAC00 \uC120\uD0DD",countrySearchPlaceholder:"\uAD6D\uAC00 \uAC80\uC0C9...",noCountriesFound:"\uAD6D\uAC00\uB97C \uCC3E\uC744 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4",searchCountriesAriaLabel:"\uAD6D\uAC00 \uAC80\uC0C9",countryOptionsAriaLabel:"\uAD6D\uAC00 \uC635\uC158",countryButtonAriaLabel:"\uAD6D\uAC00 \uC120\uD0DD\uAE30 \uC5F4\uAE30"},zh:{selectCountry:"\u9009\u62E9\u56FD\u5BB6",countrySearchPlaceholder:"\u641C\u7D22\u56FD\u5BB6...",noCountriesFound:"\u672A\u627E\u5230\u56FD\u5BB6",searchCountriesAriaLabel:"\u641C\u7D22\u56FD\u5BB6",countryOptionsAriaLabel:"\u56FD\u5BB6\u9009\u9879",countryButtonAriaLabel:"\u6253\u5F00\u56FD\u5BB6\u9009\u62E9\u5668"},ar:{selectCountry:"\u0627\u062E\u062A\u0631 \u0627\u0644\u062F\u0648\u0644\u0629",countrySearchPlaceholder:"\u0627\u0628\u062D\u062B \u0639\u0646 \u062F\u0648\u0644\u0629...",noCountriesFound:"\u0644\u0645 \u064A\u062A\u0645 \u0627\u0644\u0639\u062B\u0648\u0631 \u0639\u0644\u0649 \u062F\u0648\u0644",searchCountriesAriaLabel:"\u0627\u0628\u062D\u062B \u0639\u0646 \u0627\u0644\u062F\u0648\u0644",countryOptionsAriaLabel:"\u062E\u064A\u0627\u0631\u0627\u062A \u0627\u0644\u062F\u0648\u0644",countryButtonAriaLabel:"\u0627\u0641\u062A\u062D \u0645\u062D\u062F\u062F \u0627\u0644\u062F\u0648\u0644\u0629"},hi:{selectCountry:"\u0926\u0947\u0936 \u091A\u0941\u0928\u0947\u0902",countrySearchPlaceholder:"\u0926\u0947\u0936 \u0916\u094B\u091C\u0947\u0902...",noCountriesFound:"\u0915\u094B\u0908 \u0926\u0947\u0936 \u0928\u0939\u0940\u0902 \u092E\u093F\u0932\u093E",searchCountriesAriaLabel:"\u0926\u0947\u0936 \u0916\u094B\u091C\u0947\u0902",countryOptionsAriaLabel:"\u0926\u0947\u0936 \u0935\u093F\u0915\u0932\u094D\u092A",countryButtonAriaLabel:"\u0926\u0947\u0936 \u091A\u092F\u0928 \u0916\u094B\u0932\u0947\u0902"}};function mt(e){let n=(Array.isArray(e)?e:e?[e]:[]).find(Boolean);return n?n.toLowerCase().split("-")[0]:void 0}function xe(e,t){let n=mt(e),r=n?gt[n]:void 0;return {...Ne,...r,...t}}function _(e){let t=e.trim(),n=d(t);return t.startsWith("+")&&!n?"+":n?t.startsWith("+")?`+${n}`:n:""}function Ie(e){return e?.code}function ze(e){return e?.format?.replace(/[^#]/g,"").length}function d(e){return e.replace(/\D/g,"")}function H(e,t){return e&&new AsYouType(Ie(t)).input(_(e))}function le(e){return _(e)}function Te(e,t){return !e||!t?false:e.trim().startsWith("+")?validatePhoneNumberLength(_(e))===void 0:validatePhoneNumberLength(d(e),Ie(t))===void 0}function ce(e,t,n){if(!e)return false;if(n)return n(e,t??void 0);let r=_(e),o=Ie(t);return o?r.startsWith("+")?isValidPhoneNumber(r):isValidPhoneNumber(d(r),o):isPossiblePhoneNumber(r.startsWith("+")?r:`+${d(r)}`)}function B(e,t){if(!t)return e;let n=_(e);if(!n||n==="+")return "";if(n.startsWith("+"))return n;let r=d(n);return t.dialCodes.find(l=>r.startsWith(l.replace("+","")))?`+${r}`:`${t.dialCode}${r}`}function O(e,t){if(!t)return e;let n=_(e),r=d(n),o=d(t.dialCode);if(r.startsWith(o))return r.slice(o.length);for(let l of t.dialCodes){let y=l.replace("+","");if(r.startsWith(y))return r.slice(y.length)}return r}function De(e,t,n){let r=t.toLowerCase().trim();return r?e.filter(o=>o.name.toLowerCase().includes(r)||(o.displayName??"").toLowerCase().includes(r)||o.code.toLowerCase().includes(r)||o.dialCodes.some(l=>l.includes(r))):n?.length?[...e].sort((o,l)=>{let y=n.indexOf(o.code),C=n.indexOf(l.code);return y!==-1&&C!==-1?y-C:y!==-1?-1:C!==-1?1:(o.displayName??o.name).localeCompare(l.displayName??l.name)}):e}function de(e){return e?.displayName??e?.name??""}function ve(e){return e?.format?e.format.replace(/#/g,"0"):e?.dialCode?`${e.dialCode} 000 000 0000`:"Phone number"}var Pt=d;function Z(e){return e.trim().startsWith("+")||e.trim().startsWith("00")}function Lt(e){let t=d(e);if(!Z(e))return {dialCode:null,nationalNumber:t};let n=R.map(o=>o.dialCode).sort((o,l)=>l.length-o.length).find(o=>t.startsWith(d(o)));if(!n)return {dialCode:null,nationalNumber:t};let r=d(n);return {dialCode:r,nationalNumber:t.slice(r.length)}}function Nt(e){return U(e)??null}function xt(e,t,n="national"){if(!e)return "";if(n==="e164")return B(e,t);let r=Z(e)?O(e,t):e;if(!t&&n==="national")return d(r).slice(0,9).replace(/(\d{3})(?=\d)/g,"$1 ").trim();let o=H(r,t);return n==="international"?t?`${t.dialCode} ${o}`:B(e,t):o}function At(e,t){return B(e,t)}function It(e,t){if(t)return O(e,t);let n=U(e);return n?O(e,n):d(e)}function Se(e,t,n=false){let r=d(e);if(!r)return n?{isValid:false,error:"Phone number is required"}:{isValid:true,error:null};let o=ze(t),l=t?d(O(e,t)):r;return l.length<7?{isValid:false,error:"Phone number is too short"}:o&&l.length>o?{isValid:false,error:`Phone number is too long (max ${o} digits)`}:ce(e,t)?{isValid:true,error:null}:{isValid:false,error:"Invalid phone number"}}function Dt(e,t){return t?Te(e,t):d(e).length>=7}function vt(e,t){return Z(e)&&t?`${t.dialCode} ${H(O(e,t),t)}`:H(e,t)}var Ae=d;function St(e,t){return Ae(e)===Ae(t)}function wt(e,t=true){let n=`${e.flag} ${de(e)}`;return t?`${n} (${e.dialCode})`:n}function Bt(e,t){let n=ze(t);if(!n)return e;let r=Z(e),o=d(e),l=t?d(t.dialCode):"",y=r?l.length+n:n,C=o.slice(0,y);return r?`+${C}`:C}function Et(e,t){return e.slice(0,t).replace(/\D/g,"").length}function Mt(e,t){if(t<=0)return e.startsWith("+")?1:0;let n=0;for(let r=0;r<e.length;r+=1)if(/\d/.test(e[r])&&(n+=1,n===t))return r+1;return e.length}function Be(e={}){let{value:t,defaultValue:n="",onChange:r,onCountryChange:o,onBlur:l,onFocus:y,defaultCountry:C="US",preferredCountries:ee,excludeCountries:fe,excludedCountries:Ce,onlyCountries:ye,allowedCountries:ge,autoDetect:q,autoDetectCountry:me,formatOnType:te=true,includeDialCode:m=false,required:ne=false,validator:re,locale:N,labels:$}=e,F=ye??ge,I=fe??Ce,x=q??me??true,D=t!==void 0,z=useRef(null),G=useRef(null),v=useRef(C),p=useMemo(()=>{let i=R;return F?.length&&(i=i.filter(a=>F.includes(a.code))),I?.length&&(i=i.filter(a=>!I.includes(a.code))),i},[F,I]),Q=useMemo(()=>Le(p,N),[p,N]),T=useMemo(()=>xe(N,$),[N,$]),he=p.find(i=>i.code===C)||p[0],[be,oe]=useState(n),[s,W]=useState(he),[k,M]=useState(false),[S,P]=useState(""),u=D?t:be,g=useCallback(i=>X(i,N),[N]);useEffect(()=>{let i=v.current!==C;v.current=C;let a=p.find(Y=>Y.code===C)||p[0],h=s?p.some(Y=>Y.code===s.code):false;a&&(i||!h)&&W(a);},[C,p,s]),useEffect(()=>{if(x&&u){let i=U(u);i&&p.some(a=>a.code===i.code)&&i.code!==s?.code&&(W(i),o?.(g(i)));}},[p,x,u,s?.code,o,g]);let V=useMemo(()=>te?H(m&&u.startsWith("+")?O(u,s):u,s):m&&u.startsWith("+")?O(u,s):u,[te,m,u,s]),Pe=useMemo(()=>m&&s?B(u,s):u,[u,s,m]),ie=useMemo(()=>ce(u,s,re),[u,s,re]),c=useMemo(()=>Se(u,s,ne).error,[u,s,ne]),w=useMemo(()=>X(s,N),[s,N]),L=useMemo(()=>De(Q,S,ee),[Q,S,ee]);useLayoutEffect(()=>{let i=G.current,a=z.current;if(i===null||!a||typeof a.setSelectionRange!="function"||typeof document<"u"&&document.activeElement!==a)return;let h=Mt(V,i);a.setSelectionRange(h,h),G.current=null;},[V]);let A=useCallback(i=>{let a=le(i);D||oe(a),r?.(m?B(a,s):a,g(s));},[D,r,s,m,g]),Ue=useCallback(i=>{let a=j(i);a&&(W(a),o?.(g(a)),r?.(m?B(u,a):u,g(a)));},[u,r,o,m,g]),ae=useCallback(i=>{let a=j(i.code)??i;W(a),M(false),P(""),o?.(g(a)),r?.(m?B(u,a):u,g(a));},[u,r,o,m,g]),se=useCallback(()=>{M(i=>!i),k&&P("");},[k]),Oe=useCallback(()=>{M(true);},[]),ue=useCallback(()=>{M(false),P("");},[]),He=useCallback(()=>{D||oe(""),r?.("",g(s));},[D,r,s,g]),_e=useCallback(i=>{let a=i.target.value,h=i.target.selectionStart??a.length,Y=a.trim().startsWith("+")?`+${a.replace(/\D/g,"")}`:le(a);G.current=Et(a,h),A(Y);},[A]),Ke=useCallback(()=>{y?.();},[y]),qe=useCallback(()=>{l?.();},[l]),Ge=useCallback((i,a)=>({role:"option","aria-selected":i.code===s?.code,onClick:()=>ae(i),onKeyDown:h=>{(h.key==="Enter"||h.key===" ")&&(h.preventDefault(),ae(i)),h.key==="Escape"&&(h.preventDefault(),ue());},tabIndex:a===0?0:-1}),[ue,s?.code,ae]);return {phone:u,value:u,fullPhone:Pe,country:w,isValid:ie,error:c,isOpen:k,searchQuery:S,labels:T,formattedValue:V,setPhone:A,setValue:A,setCountry:Ue,toggleDropdown:se,toggle:se,openDropdown:Oe,open:Oe,closeDropdown:ue,close:ue,setSearchQuery:P,clear:He,inputProps:{ref:z,value:V,onChange:_e,onFocus:Ke,onBlur:qe,type:"tel",inputMode:"tel",autoComplete:"tel",placeholder:ve(s),"aria-invalid":u?!ie:void 0},countryButtonProps:{onClick:se,"aria-expanded":k,"aria-haspopup":"listbox","aria-label":T.countryButtonAriaLabel},countrySelectorProps:{onClick:se,"aria-expanded":k,"aria-haspopup":true,"aria-label":T.countryButtonAriaLabel},dropdownProps:{role:"listbox","aria-label":T.countryOptionsAriaLabel},filteredCountries:L,countries:L,selectCountry:ae,getCountryOptionProps:Ge}}var Tt=({size:e=12})=>jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:jsx("polyline",{points:"18 15 12 9 6 15"})}),Wt=({size:e=12})=>jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:jsx("polyline",{points:"6 9 12 15 18 9"})}),Vt=forwardRef(function(t,n){let{value:r,defaultValue:o,onChange:l,onBlur:y,onFocus:C,inputClassName:ee,selectorClassName:fe,containerClassName:Ce,searchable:ye=true,placeholder:ge,disabled:q,readOnly:me,name:te,id:m,"aria-label":ne,"aria-labelledby":re,...N}=t,$=useRef(null),F=useRef([]),[I,x]=useState(0),D=useId(),{inputProps:z,countryButtonProps:G,country:v,filteredCountries:p,isOpen:Q,isValid:T,searchQuery:he,setSearchQuery:be,setCountry:oe,selectCountry:s,clear:W,openDropdown:k,closeDropdown:M,labels:S}=Be({value:r,defaultValue:o,onChange:l,onBlur:y,onFocus:C,...N}),P=z.ref,u=useCallback(c=>{$.current=c,typeof P=="function"?P(c):P&&"current"in P&&(P.current=c);},[P]),g=useCallback(()=>{$.current?.focus();},[]),V=useCallback(()=>{$.current?.blur();},[]),Pe=useCallback(c=>{s(c),x(0);},[s]),ie=useCallback((c,w,L)=>{switch(c.key){case "Enter":case " ":c.preventDefault(),s(w),x(0);break;case "ArrowDown":if(c.preventDefault(),L<p.length-1){let A=L+1;x(A),F.current[A]?.focus();}break;case "ArrowUp":if(c.preventDefault(),L>0){let A=L-1;x(A),F.current[A]?.focus();}break;case "Escape":c.preventDefault(),M();break}},[s,p.length,M]);return useEffect(()=>{if(p.length===0){I!==0&&x(0);return}I>=p.length&&x(p.length-1);},[p.length,I]),useImperativeHandle(n,()=>({focus:g,blur:V,clear:W,setCountry:oe,getCountry:()=>v,open:k,close:M})),jsxs("div",{className:Ce,"data-valid":T,children:[jsxs("button",{...G,type:"button",className:fe,disabled:q,tabIndex:q?-1:0,"aria-controls":D,children:[v?jsxs(Fragment,{children:[jsx("span",{className:"phone-input-flag",children:v.flag}),jsx("span",{className:"phone-input-dial-code",children:v.dialCode})]}):jsx("span",{className:"phone-input-placeholder",children:S.selectCountry}),jsx("span",{className:"phone-input-arrow","aria-hidden":"true",children:Q?jsx(Tt,{size:12}):jsx(Wt,{size:12})})]}),jsx("input",{...z,ref:u,id:m,name:te,className:ee,placeholder:ge||z.placeholder,disabled:q,readOnly:me,"aria-label":ne,"aria-labelledby":re}),Q&&jsxs("div",{className:"phone-input-dropdown",children:[ye&&jsx("div",{className:"phone-input-search",children:jsx("input",{type:"text",value:he,onChange:c=>be(c.target.value),placeholder:S.countrySearchPlaceholder,autoFocus:true,"aria-controls":D,"aria-label":S.searchCountriesAriaLabel})}),jsxs("ul",{className:"phone-input-country-list",id:D,role:"listbox","aria-label":S.countryOptionsAriaLabel,children:[p.map((c,w)=>jsxs("li",{ref:L=>{F.current[w]=L;},role:"option","aria-selected":c.code===v?.code,className:`phone-input-country-option ${c.code===v?.code?"selected":""} ${w===I?"highlighted":""}`,onClick:()=>Pe(c),onKeyDown:L=>ie(L,c,w),onMouseEnter:()=>x(w),tabIndex:w===I?0:-1,children:[jsx("span",{className:"phone-input-flag",children:c.flag}),jsx("span",{className:"phone-input-country-name",children:de(c)}),jsx("span",{className:"phone-input-dial-code",children:c.dialCode})]},c.code)),p.length===0&&jsx("li",{className:"phone-input-no-results",children:S.noCountriesFound})]})]})]})});export{Ne as DEFAULT_PHONE_INPUT_LABELS,Vt as PhoneInput,B as addDialCode,d as cleanPhone,R as countries,Nt as detectCountry,U as detectCountryFromPhone,Lt as extractDialCode,De as filterCountries,vt as formatAsYouType,H as formatPhone,xt as formatPhoneNumber,Fe as getCountriesByDialCode,j as getCountryByCode,yt as getCountryByDialCode,wt as getCountryDisplayLabel,de as getCountryDisplayName,Re as getLocalizedCountryName,It as getNationalNumber,ve as getPlaceholder,Z as isInternationalFormat,Dt as isPhoneNumberComplete,Bt as limitInputLength,Le as localizeCountries,X as localizeCountry,Ae as normalizePhoneNumber,At as parseToE164,St as phoneNumbersEqual,O as removeDialCode,xe as resolvePhoneInputLabels,Pt as stripNonDigits,le as unformatPhone,Be as usePhoneInput,ce as validatePhone,Te as validatePhoneLength,Se as validatePhoneNumber};//# sourceMappingURL=index.js.map
1
+ import {forwardRef,useRef,useState,useId,useCallback,useEffect,useImperativeHandle,useMemo,useLayoutEffect}from'react';import {getCountries,getCountryCallingCode,getExampleNumber,parsePhoneNumberFromString,AsYouType,isValidPhoneNumber,isPossiblePhoneNumber,validatePhoneNumberLength}from'libphonenumber-js';import rn from'libphonenumber-js/mobile/examples';import on from'world-countries';import {jsxs,Fragment,jsx}from'react/jsx-runtime';var an=["US","GB","CA","AU","DE","FR","IT","ES","JP","CN","IN","BR","MX","KR","NL","CH","SE","AE","SG","ZA","NG","SA","TR","ID","PH","TH","VN","MY","NZ","AR"],sn=new Set(getCountries()),un=new Map(on.map(e=>[e.cca2,e])),ln={AC:{name:"Ascension Island",dialCode:"+247"},TA:{name:"Tristan da Cunha",dialCode:"+290"},BQ:{name:"Caribbean Netherlands",dialCode:"+599"}};function dn(e,n){let t=e.startsWith("+")?e:`+${e}`,r=n.replace(/\D/g,"");return `${t}${r}`}function cn(e){return e.toUpperCase().replace(/./g,n=>String.fromCodePoint(127397+n.charCodeAt(0)))}function pn(e,n){let t=e?.idd?.root;if(!t)return [n];let o=(e.idd?.suffixes?.length?e.idd.suffixes:[""]).map(s=>dn(t,s)).filter(s=>/^\+\d+$/.test(s));return Array.from(new Set([n,...o.length?o:[]]))}function fn(e){let n=getExampleNumber(e,rn);return n?n.formatNational().replace(/\d/g,"#"):void 0}function Ve(e){let n=an.indexOf(e);return n===-1?Number.MAX_SAFE_INTEGER:n}var Cn=Array.from(sn).map(e=>{let n=un.get(e),t=ln[e],r=t?.dialCode??`+${getCountryCallingCode(e)}`,o=pn(n,r);return {code:e,name:n?.name.common??t?.name??e,dialCode:r,dialCodes:o,flag:n?.flag||t?.flag||cn(e),format:fn(e)}}).sort((e,n)=>{let t=Ve(e.code)-Ve(n.code);return t!==0?t:e.name.localeCompare(n.name)}),S=Cn,gn=new Map(S.map(e=>[e.code,e])),yn=S.reduce((e,n)=>{for(let t of n.dialCodes){let r=e.get(t)??[];r.push(n),e.set(t,r);}return e},new Map),mn=S.flatMap(e=>e.dialCodes.map(n=>({dialCode:n,country:e}))).sort((e,n)=>n.dialCode.length-e.dialCode.length),$e=new Map;function hn(e){return Array.isArray(e)?[...e]:e?[e]:[]}function bn(e){let n=hn(e);if(n.length===0||typeof Intl>"u"||typeof Intl.DisplayNames>"u")return;let t=n.join("|"),r=$e.get(t);if(r)return r;try{let o=new Intl.DisplayNames(n,{type:"region"});return $e.set(t,o),o}catch{return}}function Pn(e){let n=e.replace(/\D/g,"");return n?`+${n}`:""}function T(e){return gn.get(e.toUpperCase())}function ke(e,n){return e?bn(n)?.of(e.code)??e.displayName??e.name:void 0}function Z(e,n){if(!e)return;let t=ke(e,n);return t?{...e,displayName:t}:e}function le(e,n){return n?e.map(t=>Z(t,n)??t):e}function ze(e){return yn.get(Pn(e))??[]}function Ln(e){return ze(e)[0]}function xn(e={}){let n=S;if(e.onlyCountries?.length){let t=new Set(e.onlyCountries.map(r=>r.toUpperCase()));n=n.filter(r=>t.has(r.code));}if(e.excludeCountries?.length){let t=new Set(e.excludeCountries.map(r=>r.toUpperCase()));n=n.filter(r=>!t.has(r.code));}if(n=le(n,e.locale),e.preferredCountries?.length){let t=e.preferredCountries.map(r=>r.toUpperCase());n=[...n].sort((r,o)=>{let s=t.indexOf(r.code),c=t.indexOf(o.code);return s!==-1&&c!==-1?s-c:s!==-1?-1:c!==-1?1:(r.displayName??r.name).localeCompare(o.displayName??o.name)});}return n.map(t=>({value:t.code,label:`${t.displayName??t.name} (${t.dialCode})`,dialCode:t.dialCode,flag:t.flag}))}function q(e){let n=e.trim();if(!n)return;let t=n.startsWith("+")?`+${n.replace(/\D/g,"")}`:n.startsWith("00")?`+${n.slice(2).replace(/\D/g,"")}`:"";if(!t)return;let r=parsePhoneNumberFromString(t);return r?.country?T(r.country):mn.find(o=>t.startsWith(o.dialCode))?.country}var De={selectCountry:"Select country",countrySearchPlaceholder:"Search countries...",noCountriesFound:"No countries found",searchCountriesAriaLabel:"Search countries",countryOptionsAriaLabel:"Country options",countryButtonAriaLabel:"Open country selector"},Nn={en:De,fr:{selectCountry:"Choisir un pays",countrySearchPlaceholder:"Rechercher un pays...",noCountriesFound:"Aucun pays trouv\xE9",searchCountriesAriaLabel:"Rechercher des pays",countryOptionsAriaLabel:"Options de pays",countryButtonAriaLabel:"Ouvrir le s\xE9lecteur de pays"},de:{selectCountry:"Land ausw\xE4hlen",countrySearchPlaceholder:"Land suchen...",noCountriesFound:"Keine L\xE4nder gefunden",searchCountriesAriaLabel:"L\xE4nder suchen",countryOptionsAriaLabel:"L\xE4nderoptionen",countryButtonAriaLabel:"L\xE4nderauswahl \xF6ffnen"},es:{selectCountry:"Seleccionar pa\xEDs",countrySearchPlaceholder:"Buscar pa\xEDs...",noCountriesFound:"No se encontraron pa\xEDses",searchCountriesAriaLabel:"Buscar pa\xEDses",countryOptionsAriaLabel:"Opciones de pa\xEDs",countryButtonAriaLabel:"Abrir selector de pa\xEDs"},pt:{selectCountry:"Selecionar pa\xEDs",countrySearchPlaceholder:"Pesquisar pa\xEDs...",noCountriesFound:"Nenhum pa\xEDs encontrado",searchCountriesAriaLabel:"Pesquisar pa\xEDses",countryOptionsAriaLabel:"Op\xE7\xF5es de pa\xEDs",countryButtonAriaLabel:"Abrir seletor de pa\xEDs"},it:{selectCountry:"Seleziona paese",countrySearchPlaceholder:"Cerca paese...",noCountriesFound:"Nessun paese trovato",searchCountriesAriaLabel:"Cerca paesi",countryOptionsAriaLabel:"Opzioni paese",countryButtonAriaLabel:"Apri selettore paese"},ja:{selectCountry:"\u56FD\u3092\u9078\u629E",countrySearchPlaceholder:"\u56FD\u3092\u691C\u7D22...",noCountriesFound:"\u56FD\u304C\u898B\u3064\u304B\u308A\u307E\u305B\u3093",searchCountriesAriaLabel:"\u56FD\u3092\u691C\u7D22",countryOptionsAriaLabel:"\u56FD\u306E\u5019\u88DC",countryButtonAriaLabel:"\u56FD\u306E\u30BB\u30EC\u30AF\u30BF\u30FC\u3092\u958B\u304F"},ko:{selectCountry:"\uAD6D\uAC00 \uC120\uD0DD",countrySearchPlaceholder:"\uAD6D\uAC00 \uAC80\uC0C9...",noCountriesFound:"\uAD6D\uAC00\uB97C \uCC3E\uC744 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4",searchCountriesAriaLabel:"\uAD6D\uAC00 \uAC80\uC0C9",countryOptionsAriaLabel:"\uAD6D\uAC00 \uC635\uC158",countryButtonAriaLabel:"\uAD6D\uAC00 \uC120\uD0DD\uAE30 \uC5F4\uAE30"},zh:{selectCountry:"\u9009\u62E9\u56FD\u5BB6",countrySearchPlaceholder:"\u641C\u7D22\u56FD\u5BB6...",noCountriesFound:"\u672A\u627E\u5230\u56FD\u5BB6",searchCountriesAriaLabel:"\u641C\u7D22\u56FD\u5BB6",countryOptionsAriaLabel:"\u56FD\u5BB6\u9009\u9879",countryButtonAriaLabel:"\u6253\u5F00\u56FD\u5BB6\u9009\u62E9\u5668"},ar:{selectCountry:"\u0627\u062E\u062A\u0631 \u0627\u0644\u062F\u0648\u0644\u0629",countrySearchPlaceholder:"\u0627\u0628\u062D\u062B \u0639\u0646 \u062F\u0648\u0644\u0629...",noCountriesFound:"\u0644\u0645 \u064A\u062A\u0645 \u0627\u0644\u0639\u062B\u0648\u0631 \u0639\u0644\u0649 \u062F\u0648\u0644",searchCountriesAriaLabel:"\u0627\u0628\u062D\u062B \u0639\u0646 \u0627\u0644\u062F\u0648\u0644",countryOptionsAriaLabel:"\u062E\u064A\u0627\u0631\u0627\u062A \u0627\u0644\u062F\u0648\u0644",countryButtonAriaLabel:"\u0627\u0641\u062A\u062D \u0645\u062D\u062F\u062F \u0627\u0644\u062F\u0648\u0644\u0629"},hi:{selectCountry:"\u0926\u0947\u0936 \u091A\u0941\u0928\u0947\u0902",countrySearchPlaceholder:"\u0926\u0947\u0936 \u0916\u094B\u091C\u0947\u0902...",noCountriesFound:"\u0915\u094B\u0908 \u0926\u0947\u0936 \u0928\u0939\u0940\u0902 \u092E\u093F\u0932\u093E",searchCountriesAriaLabel:"\u0926\u0947\u0936 \u0916\u094B\u091C\u0947\u0902",countryOptionsAriaLabel:"\u0926\u0947\u0936 \u0935\u093F\u0915\u0932\u094D\u092A",countryButtonAriaLabel:"\u0926\u0947\u0936 \u091A\u092F\u0928 \u0916\u094B\u0932\u0947\u0902"}};function vn(e){let t=(Array.isArray(e)?e:e?[e]:[]).find(Boolean);return t?t.toLowerCase().split("-")[0]:void 0}function Ae(e,n){let t=vn(e),r=t?Nn[t]:void 0;return {...De,...r,...n}}function W(e){let n=e.trim(),t=p(n);return n.startsWith("+")&&!t?"+":t?n.startsWith("+")?`+${t}`:t:""}function ce(e){return e?.code}function He(e){return e?.format?.replace(/[^#]/g,"").length}function p(e){return e.replace(/\D/g,"")}function K(e,n){return e&&new AsYouType(ce(n)).input(W(e))}function pe(e){return W(e)}function Ge(e,n){return !e||!n?false:e.trim().startsWith("+")?validatePhoneNumberLength(W(e))===void 0:validatePhoneNumberLength(p(e),ce(n))===void 0}function qe(e,n,t){if(!e)return false;if(t)return t(e,n??void 0);let r=W(e),o=ce(n);return o?r.startsWith("+")?isValidPhoneNumber(r):isValidPhoneNumber(p(r),o):isPossiblePhoneNumber(r.startsWith("+")?r:`+${p(r)}`)}function B(e,n){if(!n)return e;let t=W(e);if(!t||t==="+")return "";if(t.startsWith("+"))return t;let r=p(t);return n.dialCodes.find(s=>r.startsWith(s.replace("+","")))?`+${r}`:`${n.dialCode}${r}`}function O(e,n){if(!n)return e;let t=W(e),r=p(t),o=p(n.dialCode);if(r.startsWith(o))return r.slice(o.length);for(let s of n.dialCodes){let c=s.replace("+","");if(r.startsWith(c))return r.slice(c.length)}return r}function we(e,n,t){let r=n.toLowerCase().trim();return r?e.filter(o=>o.name.toLowerCase().includes(r)||(o.displayName??"").toLowerCase().includes(r)||o.code.toLowerCase().includes(r)||o.dialCodes.some(s=>s.includes(r))):t?.length?[...e].sort((o,s)=>{let c=t.indexOf(o.code),y=t.indexOf(s.code);return c!==-1&&y!==-1?c-y:c!==-1?-1:y!==-1?1:(o.displayName??o.name).localeCompare(s.displayName??s.name)}):e}function fe(e){return e?.displayName??e?.name??""}function Re(e){return e?.format?e.format.replace(/#/g,"0"):e?.dialCode?`${e.dialCode} 000 000 0000`:"Phone number"}var An=p;function J(e){return e.trim().startsWith("+")||e.trim().startsWith("00")}function On(e){let n=p(e);if(!J(e))return {dialCode:null,nationalNumber:n};let t=S.map(o=>o.dialCode).sort((o,s)=>s.length-o.length).find(o=>n.startsWith(p(o)));if(!t)return {dialCode:null,nationalNumber:n};let r=p(t);return {dialCode:r,nationalNumber:n.slice(r.length)}}function wn(e){return q(e)??null}function Rn(e,n,t="national"){if(!e)return "";if(t==="e164")return B(e,n);let r=J(e)?O(e,n):e;if(!n&&t==="national")return p(r).slice(0,9).replace(/(\d{3})(?=\d)/g,"$1 ").trim();let o=K(r,n);return t==="international"?n?`${n.dialCode} ${o}`:B(e,n):o}function En(e,n){return B(e,n)}function Sn(e,n){if(n)return O(e,n);let t=q(e);return t?O(e,t):p(e)}function de(e,n){return {isValid:false,reason:e,message:n,error:n}}function _e(){return {isValid:true,reason:null,message:null,error:null}}function Ee(e,n,t=false,r){let o=p(e);if(!o)return t?de("required","Phone number is required"):_e();let s=He(n),c=n?p(O(e,n)):o;return c.length<7?de("too_short","Phone number is too short"):s&&c.length>s?de("too_long",`Phone number is too long (max ${s} digits)`):(r?r(e,n??void 0):qe(e,n))?_e():de("invalid","Invalid phone number")}function Bn(e,n){let t=ce(n),r=W(e),o=r.startsWith("+")?parsePhoneNumberFromString(r):parsePhoneNumberFromString(p(r),t);if(!o){let c=n?O(e,n):p(e);return {country:n??void 0,nationalNumber:c,e164:void 0,isValid:false}}return {country:o.country?T(o.country):n??void 0,nationalNumber:o.nationalNumber,e164:o.isValid()?o.number:void 0,isValid:o.isValid()}}function Mn(e,n){return n?Ge(e,n):p(e).length>=7}function Fn(e,n){return J(e)&&n?`${n.dialCode} ${K(O(e,n),n)}`:K(e,n)}var Oe=p;function Vn(e,n){return Oe(e)===Oe(n)}function $n(e,n=true){let t=`${e.flag} ${fe(e)}`;return n?`${t} (${e.dialCode})`:t}function kn(e,n){let t=He(n);if(!t)return e;let r=J(e),o=p(e),s=n?p(n.dialCode):"",c=r?s.length+t:t,y=o.slice(0,c);return r?`+${y}`:y}function Tn(e,n){return e.slice(0,n).replace(/\D/g,"").length}function Wn(e,n){if(n<=0)return e.startsWith("+")?1:0;let t=0;for(let r=0;r<e.length;r+=1)if(/\d/.test(e[r])&&(t+=1,t===n))return r+1;return e.length}function Me(e={}){let{value:n,defaultValue:t="",onChange:r,onCountryChange:o,onBlur:s,onFocus:c,defaultCountry:y="US",preferredCountries:ne,excludeCountries:ge,excludedCountries:ye,onlyCountries:me,allowedCountries:he,autoDetect:Y,autoDetectCountry:be,formatOnType:te=true,includeDialCode:L=false,required:re=false,validator:oe,onValidationChange:ie,locale:P,labels:U}=e,V=me??he,I=ge??ye,D=Y??be??true,w=n!==void 0,_=useRef(null),j=useRef(null),ae=useRef(y),C=useMemo(()=>{let i=S;return V?.length&&(i=i.filter(a=>V.includes(a.code))),I?.length&&(i=i.filter(a=>!I.includes(a.code))),i},[V,I]),N=useMemo(()=>le(C,P),[C,P]),R=useMemo(()=>Ae(P,U),[P,U]),Pe=C.find(i=>i.code===y)||C[0],[Le,se]=useState(t),[d,E]=useState(Pe),[$,H]=useState(false),[A,k]=useState(""),l=w?n:Le,f=useCallback(i=>Z(i,P),[P]);useEffect(()=>{let i=ae.current!==y;ae.current=y;let a=C.find(X=>X.code===y)||C[0],v=d?C.some(X=>X.code===d.code):false;a&&(i||!v)&&E(a);},[y,C,d]),useEffect(()=>{if(D&&l){let i=q(l);i&&C.some(a=>a.code===i.code)&&i.code!==d?.code&&(E(i),o?.(f(i)));}},[C,D,l,d?.code,o,f]);let G=useMemo(()=>te?K(L&&l.startsWith("+")?O(l,d):l,d):L&&l.startsWith("+")?O(l,d):l,[te,L,l,d]),xe=useMemo(()=>L&&d?B(l,d):l,[l,d,L]),M=useMemo(()=>Ee(l,d,re,oe),[l,d,re,oe]);useEffect(()=>{ie?.(M);},[M,ie]);let Ne=useMemo(()=>Z(d,P),[d,P]),ue=useMemo(()=>we(N,A,ne),[N,A,ne]);useLayoutEffect(()=>{let i=j.current,a=_.current;if(i===null||!a||typeof a.setSelectionRange!="function"||typeof document<"u"&&document.activeElement!==a)return;let v=Wn(G,i);a.setSelectionRange(v,v),j.current=null;},[G]);let z=useCallback(i=>{let a=pe(i);w||se(a),r?.(L?B(a,d):a,f(d));},[w,r,d,L,f]),ve=useCallback(i=>{let a=T(i);a&&(E(a),o?.(f(a)),r?.(L?B(l,a):l,f(a)));},[l,r,o,L,f]),u=useCallback(i=>{let a=T(i.code)??i;E(a),H(false),k(""),o?.(f(a)),r?.(L?B(l,a):l,f(a));},[l,r,o,L,f]),h=useCallback(()=>{H(i=>!i),$&&k("");},[$]),b=useCallback(()=>{H(true);},[]),m=useCallback(()=>{H(false),k("");},[]),Qe=useCallback(()=>{w||se(""),r?.("",f(d));},[w,r,d,f]),Ye=useCallback(i=>{let a=i.target.value,v=i.target.selectionStart??a.length,X=a.trim().startsWith("+")?`+${a.replace(/\D/g,"")}`:pe(a);j.current=Tn(a,v),z(X);},[z]),je=useCallback(()=>{c?.();},[c]),Xe=useCallback(()=>{s?.();},[s]),Ie=useCallback(i=>`phone-input-country-${i}`,[]),Ze=useCallback((i,a)=>({id:Ie(i.code),role:"option","aria-selected":i.code===d?.code,onClick:()=>u(i),onKeyDown:v=>{(v.key==="Enter"||v.key===" ")&&(v.preventDefault(),u(i)),v.key==="Escape"&&(v.preventDefault(),m());},tabIndex:a===0?0:-1}),[m,d?.code,Ie,u]);return {phone:l,value:l,fullPhone:xe,country:Ne,isValid:M.isValid,validationReason:M.reason,error:M.error,isOpen:$,searchQuery:A,labels:R,formattedValue:G,setPhone:z,setValue:z,setCountry:ve,toggleDropdown:h,toggle:h,openDropdown:b,open:b,closeDropdown:m,close:m,setSearchQuery:k,clear:Qe,inputProps:{ref:_,value:G,onChange:Ye,onFocus:je,onBlur:Xe,type:"tel",inputMode:"tel",autoComplete:"tel",placeholder:Re(d),"aria-invalid":l?!M.isValid:void 0},countryButtonProps:{onClick:h,"aria-expanded":$,"aria-haspopup":"listbox","aria-label":R.countryButtonAriaLabel},countrySelectorProps:{onClick:h,"aria-expanded":$,"aria-haspopup":"listbox","aria-label":R.countryButtonAriaLabel},dropdownProps:{role:"listbox","aria-label":R.countryOptionsAriaLabel},getCountryOptionId:Ie,filteredCountries:ue,countries:ue,selectCountry:u,getCountryOptionProps:Ze}}var qn=({size:e=12})=>jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:jsx("polyline",{points:"18 15 12 9 6 15"})}),Kn=({size:e=12})=>jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:jsx("polyline",{points:"6 9 12 15 18 9"})}),Qn=forwardRef(function(n,t){let{value:r,defaultValue:o,onChange:s,onBlur:c,onFocus:y,inputClassName:ne,selectorClassName:ge,containerClassName:ye,searchable:me=true,placeholder:he,disabled:Y,readOnly:be,name:te,id:L,"aria-label":re,"aria-labelledby":oe,...ie}=n,P=useRef(null),U=useRef(null),V=useRef([]),[I,D]=useState(0),w=useId(),{inputProps:_,countryButtonProps:j,dropdownProps:ae,country:C,filteredCountries:N,isOpen:R,isValid:Pe,searchQuery:Le,setSearchQuery:se,setCountry:d,selectCountry:E,clear:$,openDropdown:H,closeDropdown:A,getCountryOptionId:k,labels:l}=Me({value:r,defaultValue:o,onChange:s,onBlur:c,onFocus:y,...ie}),f=_.ref,G=useCallback(u=>{U.current=u,typeof f=="function"?f(u):f&&"current"in f&&(f.current=u);},[f]),xe=useCallback(()=>{U.current?.focus();},[]),M=useCallback(()=>{U.current?.blur();},[]),Ne=useCallback(u=>{E(u),D(0);},[E]),ue=useCallback((u,h,b)=>{switch(u.key){case "Enter":case " ":u.preventDefault(),E(h),D(0);break;case "ArrowDown":if(u.preventDefault(),b<N.length-1){let m=b+1;D(m),V.current[m]?.focus();}break;case "ArrowUp":if(u.preventDefault(),b>0){let m=b-1;D(m),V.current[m]?.focus();}break;case "Escape":u.preventDefault(),A();break}},[E,N.length,A]);useEffect(()=>{if(!R)return;let u=b=>{let m=b.target;P.current&&m&&!P.current.contains(m)&&A();},h=b=>{let m=b.target;P.current&&m&&!P.current.contains(m)&&A();};return document.addEventListener("mousedown",u),document.addEventListener("focusin",h),()=>{document.removeEventListener("mousedown",u),document.removeEventListener("focusin",h);}},[R,A]),useEffect(()=>{if(N.length===0){I!==0&&D(0);return}I>=N.length&&D(N.length-1);},[N.length,I]),useImperativeHandle(t,()=>({focus:xe,blur:M,clear:$,setCountry:d,getCountry:()=>C,open:H,close:A}));let z=N[I],ve=z?k(z.code):void 0;return jsxs("div",{ref:P,className:ye,"data-valid":Pe,children:[jsxs("button",{...j,type:"button",className:ge,disabled:Y,tabIndex:Y?-1:0,"aria-controls":w,children:[C?jsxs(Fragment,{children:[jsx("span",{className:"phone-input-flag",children:C.flag}),jsx("span",{className:"phone-input-dial-code",children:C.dialCode})]}):jsx("span",{className:"phone-input-placeholder",children:l.selectCountry}),jsx("span",{className:"phone-input-arrow","aria-hidden":"true",children:R?jsx(qn,{size:12}):jsx(Kn,{size:12})})]}),jsx("input",{..._,ref:G,id:L,name:te,className:ne,placeholder:he||_.placeholder,disabled:Y,readOnly:be,"aria-label":re,"aria-labelledby":oe}),R&&jsxs("div",{className:"phone-input-dropdown",children:[me&&jsx("div",{className:"phone-input-search",children:jsx("input",{type:"text",value:Le,onChange:u=>se(u.target.value),placeholder:l.countrySearchPlaceholder,autoFocus:true,"aria-controls":w,"aria-label":l.searchCountriesAriaLabel})}),jsxs("ul",{...ae,className:"phone-input-country-list",id:w,"aria-activedescendant":ve,children:[N.map((u,h)=>jsxs("li",{id:k(u.code),ref:b=>{V.current[h]=b;},role:"option","aria-selected":u.code===C?.code,className:`phone-input-country-option ${u.code===C?.code?"selected":""} ${h===I?"highlighted":""}`,onClick:()=>Ne(u),onKeyDown:b=>ue(b,u,h),onMouseEnter:()=>D(h),tabIndex:h===I?0:-1,children:[jsx("span",{className:"phone-input-flag",children:u.flag}),jsx("span",{className:"phone-input-country-name",children:fe(u)}),jsx("span",{className:"phone-input-dial-code",children:u.dialCode})]},u.code)),N.length===0&&jsx("li",{className:"phone-input-no-results",children:l.noCountriesFound})]})]})]})});export{De as DEFAULT_PHONE_INPUT_LABELS,Qn as PhoneInput,B as addDialCode,p as cleanPhone,S as countries,wn as detectCountry,q as detectCountryFromPhone,On as extractDialCode,we as filterCountries,Fn as formatAsYouType,K as formatPhone,Rn as formatPhoneNumber,ze as getCountriesByDialCode,T as getCountryByCode,Ln as getCountryByDialCode,$n as getCountryDisplayLabel,fe as getCountryDisplayName,xn as getCountryOptions,ke as getLocalizedCountryName,Sn as getNationalNumber,Re as getPlaceholder,J as isInternationalFormat,Mn as isPhoneNumberComplete,kn as limitInputLength,le as localizeCountries,Z as localizeCountry,Oe as normalizePhoneNumber,Bn as parsePhoneValue,En as parseToE164,Vn as phoneNumbersEqual,O as removeDialCode,Ae as resolvePhoneInputLabels,An as stripNonDigits,pe as unformatPhone,Me as usePhoneInput,qe as validatePhone,Ge as validatePhoneLength,Ee as validatePhoneNumber};//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map