@input-kit/phone 0.1.2 → 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/README.md +69 -36
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +96 -12
- package/dist/index.d.ts +96 -12
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +9 -4
package/README.md
CHANGED
|
@@ -2,10 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
Headless React phone input with a complete world country-code dataset, searchable country selection, and `libphonenumber-js` powered formatting and validation.
|
|
4
4
|
|
|
5
|
+
## Latest update
|
|
6
|
+
|
|
7
|
+
Version **0.2.0** adds structured validation, `parsePhoneValue`, `getCountryOptions`, Bun maintainer tooling, CI, integration docs, and `PhoneInput` UX improvements. See [CHANGELOG.md](./CHANGELOG.md).
|
|
8
|
+
|
|
5
9
|
## Features
|
|
6
10
|
|
|
7
11
|
- **245 supported calling regions** derived from `libphonenumber-js` metadata and `world-countries`
|
|
8
|
-
- **Headless hook
|
|
12
|
+
- **Headless hook** via `usePhoneInput()` plus an optional **unstyled reference** `PhoneInput` component (class names only — no bundled CSS)
|
|
9
13
|
- **Searchable country selector** with country name, ISO code, and dial-code matching
|
|
10
14
|
- **Real formatting and validation** powered by `libphonenumber-js`
|
|
11
15
|
- **International detection** for pasted or typed `+` / `00` numbers
|
|
@@ -23,6 +27,7 @@ npm install @input-kit/phone
|
|
|
23
27
|
|
|
24
28
|
```tsx
|
|
25
29
|
import { PhoneInput } from '@input-kit/phone';
|
|
30
|
+
import '@your-app/phone-input.css'; // style .phone-input-* classes
|
|
26
31
|
|
|
27
32
|
function Example() {
|
|
28
33
|
return (
|
|
@@ -78,6 +83,58 @@ function Example() {
|
|
|
78
83
|
}
|
|
79
84
|
```
|
|
80
85
|
|
|
86
|
+
## Phone values
|
|
87
|
+
|
|
88
|
+
| Field | Meaning |
|
|
89
|
+
| --- | --- |
|
|
90
|
+
| `phone` | National digits stored by the hook (default) |
|
|
91
|
+
| `fullPhone` | National number plus dial code when `includeDialCode` is `true` |
|
|
92
|
+
| `onChange(phone, country)` | Same contract as `phone` / `includeDialCode` |
|
|
93
|
+
| E.164 for APIs | `parsePhoneValue(phone, country).e164` when valid — prefer this over raw concatenation |
|
|
94
|
+
|
|
95
|
+
## Known behavior
|
|
96
|
+
|
|
97
|
+
Formatting, length checks, and validity follow **[libphonenumber-js](https://www.npmjs.com/package/libphonenumber-js)** (same family as `react-phone-number-input`). The package does not implement per-country rules outside that library.
|
|
98
|
+
|
|
99
|
+
## Form integrations
|
|
100
|
+
|
|
101
|
+
See **[docs/integrations.md](./docs/integrations.md)** for React Hook Form, Formik, Zod, and controlled-input patterns.
|
|
102
|
+
|
|
103
|
+
## Styled example
|
|
104
|
+
|
|
105
|
+
A minimal Vite demo using only the hook lives in **[examples/react-styled/](./examples/react-styled/)**.
|
|
106
|
+
|
|
107
|
+
## Migration from 0.1.x
|
|
108
|
+
|
|
109
|
+
Compatibility aliases remain exported. Prefer the newer names in new code:
|
|
110
|
+
|
|
111
|
+
| Deprecated | Replacement |
|
|
112
|
+
| --- | --- |
|
|
113
|
+
| `setValue` | `setPhone` |
|
|
114
|
+
| `value` (hook) | `phone` |
|
|
115
|
+
| `allowedCountries` | `onlyCountries` |
|
|
116
|
+
| `excludedCountries` | `excludeCountries` |
|
|
117
|
+
| `autoDetectCountry` | `autoDetect` |
|
|
118
|
+
| `toggle` / `open` / `close` | `toggleDropdown` / `openDropdown` / `closeDropdown` |
|
|
119
|
+
| `countries` (hook list) | `filteredCountries` |
|
|
120
|
+
| `countrySelectorProps` | `countryButtonProps` |
|
|
121
|
+
|
|
122
|
+
Validation is unified in 0.2.0: `isValid` and `error` come from the same `validatePhoneNumber` call. Use `validationReason` or `onValidationChange` for structured form messages.
|
|
123
|
+
|
|
124
|
+
## Development
|
|
125
|
+
|
|
126
|
+
Requires [Bun](https://bun.sh) (see `packageManager` in `package.json`).
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
bun install
|
|
130
|
+
bun run test
|
|
131
|
+
bun run typecheck
|
|
132
|
+
bun run build
|
|
133
|
+
bun run lint
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Manual browser check: `test-demo/` (static HTML).
|
|
137
|
+
|
|
81
138
|
## Exports
|
|
82
139
|
|
|
83
140
|
### Components and hooks
|
|
@@ -91,24 +148,22 @@ function Example() {
|
|
|
91
148
|
- `getCountryByCode(code)`
|
|
92
149
|
- `getCountryByDialCode(dialCode)`
|
|
93
150
|
- `getCountriesByDialCode(dialCode)`
|
|
151
|
+
- `getCountryOptions({ locale?, preferredCountries?, excludeCountries?, onlyCountries? })`
|
|
94
152
|
- `detectCountryFromPhone(phone)`
|
|
95
153
|
|
|
96
154
|
### Utilities
|
|
97
155
|
|
|
98
|
-
- `cleanPhone
|
|
99
|
-
- `
|
|
100
|
-
- `
|
|
101
|
-
- `
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
- `removeDialCode(phone, country)`
|
|
105
|
-
- `filterCountries(countries, query, preferredCountries?)`
|
|
106
|
-
- `getPlaceholder(country)`
|
|
156
|
+
- `cleanPhone`, `formatPhone`, `unformatPhone`, `validatePhone`, `validatePhoneLength`
|
|
157
|
+
- `validatePhoneNumber` → `{ isValid, reason, message, error }`
|
|
158
|
+
- `parsePhoneValue` → `{ country, nationalNumber, e164, isValid }`
|
|
159
|
+
- `addDialCode`, `removeDialCode`, `filterCountries`, `getPlaceholder`
|
|
160
|
+
|
|
161
|
+
Compatibility aliases: `stripNonDigits`, `detectCountry`, `formatPhoneNumber`, `parseToE164`, `getNationalNumber`, `isPhoneNumberComplete`, `formatAsYouType`, `normalizePhoneNumber`, `phoneNumbersEqual`, `getCountryDisplayLabel`, `limitInputLength`.
|
|
107
162
|
|
|
108
163
|
## `usePhoneInput(options)`
|
|
109
164
|
|
|
110
165
|
| Option | Type | Default | Description |
|
|
111
|
-
|
|
166
|
+
| --- | --- | --- | --- |
|
|
112
167
|
| `defaultCountry` | `string` | `'US'` | Default selected country |
|
|
113
168
|
| `preferredCountries` | `string[]` | - | Countries shown first in search results |
|
|
114
169
|
| `excludeCountries` | `string[]` | - | Countries to exclude |
|
|
@@ -116,33 +171,11 @@ function Example() {
|
|
|
116
171
|
| `autoDetect` | `boolean` | `true` | Detect country from international numbers |
|
|
117
172
|
| `formatOnType` | `boolean` | `true` | Apply live formatting |
|
|
118
173
|
| `includeDialCode` | `boolean` | `false` | Return values with dial code included |
|
|
174
|
+
| `required` | `boolean` | `false` | Empty value is invalid |
|
|
119
175
|
| `validator` | `(phone, country) => boolean` | - | Custom validation override |
|
|
176
|
+
| `onValidationChange` | `(state) => void` | - | Fires when validation result changes |
|
|
120
177
|
|
|
121
|
-
Important returned fields:
|
|
122
|
-
|
|
123
|
-
- `phone`
|
|
124
|
-
- `fullPhone`
|
|
125
|
-
- `country`
|
|
126
|
-
- `isValid`
|
|
127
|
-
- `isOpen`
|
|
128
|
-
- `searchQuery`
|
|
129
|
-
- `filteredCountries`
|
|
130
|
-
- `inputProps`
|
|
131
|
-
- `countryButtonProps`
|
|
132
|
-
- `setPhone()`
|
|
133
|
-
- `setCountry()`
|
|
134
|
-
- `selectCountry()`
|
|
135
|
-
- `openDropdown()`
|
|
136
|
-
- `closeDropdown()`
|
|
137
|
-
- `toggleDropdown()`
|
|
138
|
-
- `setSearchQuery()`
|
|
139
|
-
- `clear()`
|
|
140
|
-
|
|
141
|
-
## Notes
|
|
142
|
-
|
|
143
|
-
- Country coverage now comes from current `libphonenumber-js` supported regions instead of a short hand-maintained list.
|
|
144
|
-
- Formatting and validation behavior follows `libphonenumber-js`, similar to libraries like `react-phone-number-input`.
|
|
145
|
-
- The selector keeps a separate country button and number field, which matches the common multi-country pattern used by larger phone-input packages.
|
|
178
|
+
Important returned fields: `phone`, `fullPhone`, `country`, `isValid`, `validationReason`, `error`, `onValidationChange`, `filteredCountries`, `inputProps`, `countryButtonProps`, `dropdownProps`, `getCountryOptionId`.
|
|
146
179
|
|
|
147
180
|
## License
|
|
148
181
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var react=require('react'),libphonenumberJs=require('libphonenumber-js'),ke=require('libphonenumber-js/mobile/examples'),Me=require('world-countries'),jsxRuntime=require('react/jsx-runtime');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var ke__default=/*#__PURE__*/_interopDefault(ke);var Me__default=/*#__PURE__*/_interopDefault(Me);var Te=["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"],We=new Set(libphonenumberJs.getCountries());function $e(e,n){let t=e.startsWith("+")?e:`+${e}`,o=n.replace(/\D/g,"");return `${t}${o}`}function Ue(e){let n=e.idd?.root;if(!n)return [];let o=(e.idd?.suffixes?.length?e.idd.suffixes:[""]).map(a=>$e(n,a)).filter(a=>/^\+\d+$/.test(a));return Array.from(new Set(o))}function He(e){let n=libphonenumberJs.getExampleNumber(e,ke__default.default);return n?n.formatNational().replace(/\d/g,"#"):void 0}function Ie(e){let n=Te.indexOf(e);return n===-1?Number.MAX_SAFE_INTEGER:n}var _e=Me__default.default.filter(e=>We.has(e.cca2)).map(e=>{let n=e.cca2,t=Ue(e),o=`+${libphonenumberJs.getCountryCallingCode(n)}`;return !e.flag||t.length===0?null:{code:n,name:e.name.common,dialCode:o,dialCodes:t,flag:e.flag,format:He(n)}}).filter(e=>e!==null).sort((e,n)=>{let t=Ie(e.code)-Ie(n.code);return t!==0?t:e.name.localeCompare(n.name)}),T=_e,Ve=new Map(T.map(e=>[e.code,e])),Ge=T.reduce((e,n)=>{for(let t of n.dialCodes){let o=e.get(t)??[];o.push(n),e.set(t,o);}return e},new Map),Ke=T.flatMap(e=>e.dialCodes.map(n=>({dialCode:n,country:e}))).sort((e,n)=>n.dialCode.length-e.dialCode.length),Ae=new Map;function je(e){return Array.isArray(e)?[...e]:e?[e]:[]}function Qe(e){let n=je(e);if(n.length===0||typeof Intl>"u"||typeof Intl.DisplayNames>"u")return;let t=n.join("|"),o=Ae.get(t);if(o)return o;try{let a=new Intl.DisplayNames(n,{type:"region"});return Ae.set(t,a),a}catch{return}}function Ye(e){let n=e.replace(/\D/g,"");return n?`+${n}`:""}function K(e){return Ve.get(e.toUpperCase())}function Ne(e,n){return e?Qe(n)?.of(e.code)??e.displayName??e.name:void 0}function j(e,n){if(!e)return;let t=Ne(e,n);return t?{...e,displayName:t}:e}function de(e,n){return n?e.map(t=>j(t,n)??t):e}function xe(e){return Ge.get(Ye(e))??[]}function qe(e){return xe(e)[0]}function pe(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 o=libphonenumberJs.parsePhoneNumberFromString(t);return o?.country?K(o.country):Ke.find(a=>t.startsWith(a.dialCode))?.country}var fe={selectCountry:"Select country",countrySearchPlaceholder:"Search countries...",noCountriesFound:"No countries found",searchCountriesAriaLabel:"Search countries",countryOptionsAriaLabel:"Country options",countryButtonAriaLabel:"Open country selector"},Xe={en:fe,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 Ze(e){let t=(Array.isArray(e)?e:e?[e]:[]).find(Boolean);return t?t.toLowerCase().split("-")[0]:void 0}function Ce(e,n){let t=Ze(e),o=t?Xe[t]:void 0;return {...fe,...o,...n}}function W(e){let n=e.trim(),t=w(n);return n.startsWith("+")&&!t?"+":t?n.startsWith("+")?`+${t}`:t:""}function ye(e){return e?.code}function w(e){return e.replace(/\D/g,"")}function he(e,n){return e&&new libphonenumberJs.AsYouType(ye(n)).input(W(e))}function ne(e){return W(e)}function nn(e,n){return !e||!n?false:e.trim().startsWith("+")?libphonenumberJs.validatePhoneNumberLength(W(e))===void 0:libphonenumberJs.validatePhoneNumberLength(w(e),ye(n))===void 0}function ge(e,n,t){if(!e)return false;if(t)return t(e,n);let o=W(e),a=ye(n);return a?o.startsWith("+")?libphonenumberJs.isValidPhoneNumber(o):libphonenumberJs.isValidPhoneNumber(w(o),a):libphonenumberJs.isPossiblePhoneNumber(o.startsWith("+")?o:`+${w(o)}`)}function $(e,n){if(!n)return e;let t=W(e);if(!t||t==="+")return "";if(t.startsWith("+"))return t;let o=w(t);return n.dialCodes.find(d=>o.startsWith(d.replace("+","")))?`+${o}`:`${n.dialCode}${o}`}function te(e,n){if(!n)return e;let t=W(e),o=w(t);for(let a of n.dialCodes){let d=a.replace("+","");if(o.startsWith(d))return o.slice(d.length)}return o}function me(e,n,t){let o=n.toLowerCase().trim();return o?e.filter(a=>a.name.toLowerCase().includes(o)||(a.displayName??"").toLowerCase().includes(o)||a.code.toLowerCase().includes(o)||a.dialCodes.some(d=>d.includes(o))):t?.length?[...e].sort((a,d)=>{let P=t.indexOf(a.code),N=t.indexOf(d.code);return P!==-1&&N!==-1?P-N:P!==-1?-1:N!==-1?1:(a.displayName??a.name).localeCompare(d.displayName??d.name)}):e}function be(e){return e?.displayName??e?.name??""}function Pe(e){return e?.format?e.format.replace(/#/g,"0"):e?.dialCode?`${e.dialCode} 000 000 0000`:"Phone number"}function on(e,n){return e.slice(0,n).replace(/\D/g,"").length}function rn(e,n){if(n<=0)return e.startsWith("+")?1:0;let t=0;for(let o=0;o<e.length;o+=1)if(/\d/.test(e[o])&&(t+=1,t===n))return o+1;return e.length}function Le(e={}){let{value:n,defaultValue:t="",onChange:o,onBlur:a,onFocus:d,defaultCountry:P="US",preferredCountries:N,excludeCountries:H,onlyCountries:_,autoDetect:Y=true,formatOnType:q=true,includeDialCode:p=false,validator:O,locale:m,labels:X}=e,v=n!==void 0,Z=react.useRef(null),V=react.useRef(null),y=react.useMemo(()=>{let r=T;return _?.length&&(r=r.filter(i=>_.includes(i.code))),H?.length&&(r=r.filter(i=>!H.includes(i.code))),r},[_,H]),R=react.useMemo(()=>de(y,m),[y,m]),E=react.useMemo(()=>Ce(m,X),[m,X]),B=y.find(r=>r.code===P)||y[0],[L,F]=react.useState(t),[s,z]=react.useState(B),[h,f]=react.useState(false),[k,M]=react.useState(""),u=v?n:L;react.useEffect(()=>{let r=y.find(i=>i.code===P)||y[0];r&&r.code!==s?.code&&z(r);},[P,y,s?.code]),react.useEffect(()=>{if(Y&&u){let r=pe(u);r&&y.some(i=>i.code===r.code)&&r.code!==s?.code&&z(r);}},[y,Y,u,s?.code]);let G=react.useMemo(()=>q?he(p&&u.startsWith("+")?te(u,s):u,s):p&&u.startsWith("+")?te(u,s):u,[q,p,u,s]),re=react.useMemo(()=>p&&s?$(u,s):u,[u,s,p]),S=react.useMemo(()=>ge(u,s,O),[u,s,O]),ie=react.useMemo(()=>j(s,m),[s,m]),ae=react.useMemo(()=>me(R,k,N),[R,k,N]),C=react.useCallback(r=>j(r,m),[m]);react.useLayoutEffect(()=>{let r=V.current,i=Z.current;if(r===null||!i||typeof i.setSelectionRange!="function"||typeof document<"u"&&document.activeElement!==i)return;let ee=rn(G,r);i.setSelectionRange(ee,ee),V.current=null;},[G]);let I=react.useCallback(r=>{let i=ne(r);v||F(i),o?.(p?$(i,s):i,C(s));},[v,o,s,p,C]),x=react.useCallback(r=>{let i=K(r);i&&(z(i),o?.(p?$(u,i):u,C(i)));},[u,o,p,C]),se=react.useCallback(r=>{let i=K(r.code)??r;z(i),f(false),M(""),o?.(p?$(u,i):u,C(i));},[u,o,p,C]),J=react.useCallback(()=>{f(r=>!r),h&&M("");},[h]),ue=react.useCallback(()=>{f(true);},[]),le=react.useCallback(()=>{f(false),M("");},[]),ce=react.useCallback(()=>{v||F(""),o?.("",C(s));},[v,o,s,C]),l=react.useCallback(r=>{let i=r.target.value,ee=r.target.selectionStart??i.length,Oe=i.trim().startsWith("+")?`+${i.replace(/\D/g,"")}`:ne(i);V.current=on(i,ee),I(Oe);},[I]),A=react.useCallback(()=>{d?.();},[d]),b=react.useCallback(()=>{a?.();},[a]);return {phone:u,fullPhone:re,country:ie,isValid:S,isOpen:h,searchQuery:k,labels:E,setPhone:I,setCountry:x,toggleDropdown:J,openDropdown:ue,closeDropdown:le,setSearchQuery:M,clear:ce,inputProps:{ref:Z,value:G,onChange:l,onFocus:A,onBlur:b,type:"tel",inputMode:"tel",autoComplete:"tel",placeholder:Pe(s),"aria-invalid":u?!S:void 0},countryButtonProps:{onClick:J,"aria-expanded":h,"aria-haspopup":"listbox","aria-label":E.countryButtonAriaLabel},filteredCountries:ae,selectCountry:se}}var dn=({size:e=12})=>jsxRuntime.jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:jsxRuntime.jsx("polyline",{points:"18 15 12 9 6 15"})}),pn=({size:e=12})=>jsxRuntime.jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:jsxRuntime.jsx("polyline",{points:"6 9 12 15 18 9"})}),fn=react.forwardRef(function(n,t){let{value:o,defaultValue:a,onChange:d,onBlur:P,onFocus:N,inputClassName:H,selectorClassName:_,containerClassName:Y,searchable:q=true,placeholder:p,disabled:O,readOnly:m,name:X,id:v,"aria-label":Z,"aria-labelledby":V,...y}=n,R=react.useRef(null),E=react.useRef([]),[B,L]=react.useState(0),F=react.useId(),{inputProps:s,countryButtonProps:z,country:h,filteredCountries:f,isOpen:k,isValid:M,searchQuery:u,setSearchQuery:G,setCountry:re,selectCountry:S,clear:ie,openDropdown:ae,closeDropdown:C,labels:I}=Le({value:o,defaultValue:a,onChange:d,onBlur:P,onFocus:N,...y}),x=s.ref,se=react.useCallback(l=>{R.current=l,typeof x=="function"?x(l):x&&"current"in x&&(x.current=l);},[x]),J=react.useCallback(()=>{R.current?.focus();},[]),ue=react.useCallback(()=>{R.current?.blur();},[]),le=react.useCallback(l=>{S(l),L(0);},[S]),ce=react.useCallback((l,A,b)=>{switch(l.key){case "Enter":case " ":l.preventDefault(),S(A),L(0);break;case "ArrowDown":if(l.preventDefault(),b<f.length-1){let r=b+1;L(r),E.current[r]?.focus();}break;case "ArrowUp":if(l.preventDefault(),b>0){let r=b-1;L(r),E.current[r]?.focus();}break;case "Escape":l.preventDefault(),C();break}},[S,f.length,C]);return react.useEffect(()=>{if(f.length===0){B!==0&&L(0);return}B>=f.length&&L(f.length-1);},[f.length,B]),react.useImperativeHandle(t,()=>({focus:J,blur:ue,clear:ie,setCountry:re,getCountry:()=>h,open:ae,close:C})),jsxRuntime.jsxs("div",{className:Y,"data-valid":M,children:[jsxRuntime.jsxs("button",{...z,type:"button",className:_,disabled:O,tabIndex:O?-1:0,"aria-controls":F,children:[h?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx("span",{className:"phone-input-flag",children:h.flag}),jsxRuntime.jsx("span",{className:"phone-input-dial-code",children:h.dialCode})]}):jsxRuntime.jsx("span",{className:"phone-input-placeholder",children:I.selectCountry}),jsxRuntime.jsx("span",{className:"phone-input-arrow","aria-hidden":"true",children:k?jsxRuntime.jsx(dn,{size:12}):jsxRuntime.jsx(pn,{size:12})})]}),jsxRuntime.jsx("input",{...s,ref:se,id:v,name:X,className:H,placeholder:p||s.placeholder,disabled:O,readOnly:m,"aria-label":Z,"aria-labelledby":V}),k&&jsxRuntime.jsxs("div",{className:"phone-input-dropdown",children:[q&&jsxRuntime.jsx("div",{className:"phone-input-search",children:jsxRuntime.jsx("input",{type:"text",value:u,onChange:l=>G(l.target.value),placeholder:I.countrySearchPlaceholder,autoFocus:true,"aria-controls":F,"aria-label":I.searchCountriesAriaLabel})}),jsxRuntime.jsxs("ul",{className:"phone-input-country-list",id:F,role:"listbox","aria-label":I.countryOptionsAriaLabel,children:[f.map((l,A)=>jsxRuntime.jsxs("li",{ref:b=>{E.current[A]=b;},role:"option","aria-selected":l.code===h?.code,className:`phone-input-country-option ${l.code===h?.code?"selected":""} ${A===B?"highlighted":""}`,onClick:()=>le(l),onKeyDown:b=>ce(b,l,A),onMouseEnter:()=>L(A),tabIndex:A===B?0:-1,children:[jsxRuntime.jsx("span",{className:"phone-input-flag",children:l.flag}),jsxRuntime.jsx("span",{className:"phone-input-country-name",children:be(l)}),jsxRuntime.jsx("span",{className:"phone-input-dial-code",children:l.dialCode})]},l.code)),f.length===0&&jsxRuntime.jsx("li",{className:"phone-input-no-results",children:I.noCountriesFound})]})]})]})});exports.DEFAULT_PHONE_INPUT_LABELS=fe;exports.PhoneInput=fn;exports.addDialCode=$;exports.cleanPhone=w;exports.countries=T;exports.detectCountryFromPhone=pe;exports.filterCountries=me;exports.formatPhone=he;exports.getCountriesByDialCode=xe;exports.getCountryByCode=K;exports.getCountryByDialCode=qe;exports.getCountryDisplayName=be;exports.getLocalizedCountryName=Ne;exports.getPlaceholder=Pe;exports.localizeCountries=de;exports.localizeCountry=j;exports.removeDialCode=te;exports.resolvePhoneInputLabels=Ce;exports.unformatPhone=ne;exports.usePhoneInput=Le;exports.validatePhone=ge;exports.validatePhoneLength=nn;//# sourceMappingURL=index.cjs.map
|
|
1
|
+
'use strict';var react=require('react'),libphonenumberJs=require('libphonenumber-js'),rn=require('libphonenumber-js/mobile/examples'),on=require('world-countries'),jsxRuntime=require('react/jsx-runtime');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var rn__default=/*#__PURE__*/_interopDefault(rn);var on__default=/*#__PURE__*/_interopDefault(on);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(libphonenumberJs.getCountries()),un=new Map(on__default.default.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=libphonenumberJs.getExampleNumber(e,rn__default.default);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??`+${libphonenumberJs.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=libphonenumberJs.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 libphonenumberJs.AsYouType(ce(n)).input(W(e))}function pe(e){return W(e)}function Ge(e,n){return !e||!n?false:e.trim().startsWith("+")?libphonenumberJs.validatePhoneNumberLength(W(e))===void 0:libphonenumberJs.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("+")?libphonenumberJs.isValidPhoneNumber(r):libphonenumberJs.isValidPhoneNumber(p(r),o):libphonenumberJs.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("+")?libphonenumberJs.parsePhoneNumberFromString(r):libphonenumberJs.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,_=react.useRef(null),j=react.useRef(null),ae=react.useRef(y),C=react.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=react.useMemo(()=>le(C,P),[C,P]),R=react.useMemo(()=>Ae(P,U),[P,U]),Pe=C.find(i=>i.code===y)||C[0],[Le,se]=react.useState(t),[d,E]=react.useState(Pe),[$,H]=react.useState(false),[A,k]=react.useState(""),l=w?n:Le,f=react.useCallback(i=>Z(i,P),[P]);react.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]),react.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=react.useMemo(()=>te?K(L&&l.startsWith("+")?O(l,d):l,d):L&&l.startsWith("+")?O(l,d):l,[te,L,l,d]),xe=react.useMemo(()=>L&&d?B(l,d):l,[l,d,L]),M=react.useMemo(()=>Ee(l,d,re,oe),[l,d,re,oe]);react.useEffect(()=>{ie?.(M);},[M,ie]);let Ne=react.useMemo(()=>Z(d,P),[d,P]),ue=react.useMemo(()=>we(N,A,ne),[N,A,ne]);react.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=react.useCallback(i=>{let a=pe(i);w||se(a),r?.(L?B(a,d):a,f(d));},[w,r,d,L,f]),ve=react.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=react.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=react.useCallback(()=>{H(i=>!i),$&&k("");},[$]),b=react.useCallback(()=>{H(true);},[]),m=react.useCallback(()=>{H(false),k("");},[]),Qe=react.useCallback(()=>{w||se(""),r?.("",f(d));},[w,r,d,f]),Ye=react.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=react.useCallback(()=>{c?.();},[c]),Xe=react.useCallback(()=>{s?.();},[s]),Ie=react.useCallback(i=>`phone-input-country-${i}`,[]),Ze=react.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})=>jsxRuntime.jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:jsxRuntime.jsx("polyline",{points:"18 15 12 9 6 15"})}),Kn=({size:e=12})=>jsxRuntime.jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:jsxRuntime.jsx("polyline",{points:"6 9 12 15 18 9"})}),Qn=react.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=react.useRef(null),U=react.useRef(null),V=react.useRef([]),[I,D]=react.useState(0),w=react.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=react.useCallback(u=>{U.current=u,typeof f=="function"?f(u):f&&"current"in f&&(f.current=u);},[f]),xe=react.useCallback(()=>{U.current?.focus();},[]),M=react.useCallback(()=>{U.current?.blur();},[]),Ne=react.useCallback(u=>{E(u),D(0);},[E]),ue=react.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]);react.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]),react.useEffect(()=>{if(N.length===0){I!==0&&D(0);return}I>=N.length&&D(N.length-1);},[N.length,I]),react.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 jsxRuntime.jsxs("div",{ref:P,className:ye,"data-valid":Pe,children:[jsxRuntime.jsxs("button",{...j,type:"button",className:ge,disabled:Y,tabIndex:Y?-1:0,"aria-controls":w,children:[C?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx("span",{className:"phone-input-flag",children:C.flag}),jsxRuntime.jsx("span",{className:"phone-input-dial-code",children:C.dialCode})]}):jsxRuntime.jsx("span",{className:"phone-input-placeholder",children:l.selectCountry}),jsxRuntime.jsx("span",{className:"phone-input-arrow","aria-hidden":"true",children:R?jsxRuntime.jsx(qn,{size:12}):jsxRuntime.jsx(Kn,{size:12})})]}),jsxRuntime.jsx("input",{..._,ref:G,id:L,name:te,className:ne,placeholder:he||_.placeholder,disabled:Y,readOnly:be,"aria-label":re,"aria-labelledby":oe}),R&&jsxRuntime.jsxs("div",{className:"phone-input-dropdown",children:[me&&jsxRuntime.jsx("div",{className:"phone-input-search",children:jsxRuntime.jsx("input",{type:"text",value:Le,onChange:u=>se(u.target.value),placeholder:l.countrySearchPlaceholder,autoFocus:true,"aria-controls":w,"aria-label":l.searchCountriesAriaLabel})}),jsxRuntime.jsxs("ul",{...ae,className:"phone-input-country-list",id:w,"aria-activedescendant":ve,children:[N.map((u,h)=>jsxRuntime.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:[jsxRuntime.jsx("span",{className:"phone-input-flag",children:u.flag}),jsxRuntime.jsx("span",{className:"phone-input-country-name",children:fe(u)}),jsxRuntime.jsx("span",{className:"phone-input-dial-code",children:u.dialCode})]},u.code)),N.length===0&&jsxRuntime.jsx("li",{className:"phone-input-no-results",children:l.noCountriesFound})]})]})]})});exports.DEFAULT_PHONE_INPUT_LABELS=De;exports.PhoneInput=Qn;exports.addDialCode=B;exports.cleanPhone=p;exports.countries=S;exports.detectCountry=wn;exports.detectCountryFromPhone=q;exports.extractDialCode=On;exports.filterCountries=we;exports.formatAsYouType=Fn;exports.formatPhone=K;exports.formatPhoneNumber=Rn;exports.getCountriesByDialCode=ze;exports.getCountryByCode=T;exports.getCountryByDialCode=Ln;exports.getCountryDisplayLabel=$n;exports.getCountryDisplayName=fe;exports.getCountryOptions=xn;exports.getLocalizedCountryName=ke;exports.getNationalNumber=Sn;exports.getPlaceholder=Re;exports.isInternationalFormat=J;exports.isPhoneNumberComplete=Mn;exports.limitInputLength=kn;exports.localizeCountries=le;exports.localizeCountry=Z;exports.normalizePhoneNumber=Oe;exports.parsePhoneValue=Bn;exports.parseToE164=En;exports.phoneNumbersEqual=Vn;exports.removeDialCode=O;exports.resolvePhoneInputLabels=Ae;exports.stripNonDigits=An;exports.unformatPhone=pe;exports.usePhoneInput=Me;exports.validatePhone=qe;exports.validatePhoneLength=Ge;exports.validatePhoneNumber=Ee;//# sourceMappingURL=index.cjs.map
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|