@alikhalilll/a-tel-input 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +124 -0
- package/dist/index.cjs +5846 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +791 -0
- package/dist/index.d.ts +791 -0
- package/dist/index.js +5804 -0
- package/dist/index.js.map +1 -0
- package/dist/nuxt/index.cjs +30 -0
- package/dist/nuxt/index.cjs.map +1 -0
- package/dist/nuxt/index.d.cts +15 -0
- package/dist/nuxt/index.d.ts +15 -0
- package/dist/nuxt/index.js +30 -0
- package/dist/nuxt/index.js.map +1 -0
- package/dist/resolver/index.cjs +25 -0
- package/dist/resolver/index.cjs.map +1 -0
- package/dist/resolver/index.d.cts +14 -0
- package/dist/resolver/index.d.ts +14 -0
- package/dist/resolver/index.js +25 -0
- package/dist/resolver/index.js.map +1 -0
- package/dist/styles.css +520 -0
- package/package.json +123 -0
- package/src/components/ACountryFlag.vue +78 -0
- package/src/components/ACountrySelect.vue +674 -0
- package/src/components/ATelInput.vue +742 -0
- package/src/composables/useCountryDetection.ts +247 -0
- package/src/composables/useCountryMatching.ts +213 -0
- package/src/composables/usePhoneValidation.ts +573 -0
- package/src/composables/useTelInputValidation.ts +136 -0
- package/src/composables/useTypingPhase.ts +88 -0
- package/src/icons/AlertCircleIcon.vue +17 -0
- package/src/icons/CheckCircleIcon.vue +16 -0
- package/src/icons/CheckIcon.vue +15 -0
- package/src/icons/ChevronDownIcon.vue +15 -0
- package/src/icons/SearchIcon.vue +16 -0
- package/src/icons/SpinnerIcon.vue +28 -0
- package/src/icons/index.ts +6 -0
- package/src/index.ts +36 -0
- package/src/nuxt/index.ts +37 -0
- package/src/resolver/index.ts +29 -0
- package/src/types.ts +389 -0
- package/src/utils/digits.ts +42 -0
- package/src/utils/flag-url.ts +10 -0
- package/web-types.json +526 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import type { HTMLAttributes } from 'vue';
|
|
2
|
+
import { cva, type VariantProps } from 'class-variance-authority';
|
|
3
|
+
import type { DetectionStrategy, DetectCountryOptions } from './composables/useCountryDetection';
|
|
4
|
+
import type {
|
|
5
|
+
CountryOption,
|
|
6
|
+
PhoneValidationReason,
|
|
7
|
+
PhoneValidationResult,
|
|
8
|
+
} from './composables/usePhoneValidation';
|
|
9
|
+
import type { FlagUrlBuilder } from './utils/flag-url';
|
|
10
|
+
import type { Size } from '@alikhalilll/a-ui-base';
|
|
11
|
+
|
|
12
|
+
/** Alias for the shared `Size` scale — kept for backwards-friendly naming. */
|
|
13
|
+
export type ATelInputSize = Size;
|
|
14
|
+
|
|
15
|
+
// Field styling now lives entirely in ATelInput.vue's scoped CSS — sizes/states are driven
|
|
16
|
+
// by the `data-size` / `data-state` attributes set in the template. The cva wrapper survives
|
|
17
|
+
// so consumers can still call `aTelInputVariants({ size: 'md' })` (returns the field class);
|
|
18
|
+
// the per-size class slots are empty placeholders that exist only to preserve the type.
|
|
19
|
+
export const aTelInputVariants = cva('a-tel-input__field', {
|
|
20
|
+
variants: {
|
|
21
|
+
size: { xs: '', sm: '', md: '', lg: '', xl: '' },
|
|
22
|
+
},
|
|
23
|
+
defaultVariants: { size: 'md' },
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export type ATelInputVariants = VariantProps<typeof aTelInputVariants>;
|
|
27
|
+
|
|
28
|
+
/** Text direction for the field. `'auto'` (or omitting the prop) inherits from the
|
|
29
|
+
* nearest `[dir]` ancestor / `<html dir>`; `'ltr'` / `'rtl'` force it. */
|
|
30
|
+
export type ATelInputDir = 'ltr' | 'rtl' | 'auto';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Every user-facing string in the tel-input UI, bundled so a consumer can localize the
|
|
34
|
+
* component in one prop. Each key has an English default in {@link DEFAULT_MESSAGES}.
|
|
35
|
+
*/
|
|
36
|
+
export interface TelInputMessages {
|
|
37
|
+
/** Placeholder of the country-picker search box. */
|
|
38
|
+
searchPlaceholder: string;
|
|
39
|
+
/** Shown when a search yields no countries. */
|
|
40
|
+
emptyText: string;
|
|
41
|
+
/** Shown while the country list is loading. */
|
|
42
|
+
loadingText: string;
|
|
43
|
+
/** Header of the "Suggested" group (current + recent picks). */
|
|
44
|
+
suggestedLabel: string;
|
|
45
|
+
/** Header of the full country list. */
|
|
46
|
+
allCountriesLabel: string;
|
|
47
|
+
/** Validation error text, keyed by reason. */
|
|
48
|
+
errorMessages: Record<PhoneValidationReason, string>;
|
|
49
|
+
/** Prefix of the country trigger's `aria-label`, e.g. `"Country: Egypt"`. */
|
|
50
|
+
countryLabel: string;
|
|
51
|
+
/** `aria-label` of the country trigger when no country is selected. */
|
|
52
|
+
selectCountryLabel: string;
|
|
53
|
+
/** `aria-label` of the phone input element. */
|
|
54
|
+
phoneInputLabel: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Partial override shape for the `messages` prop — every key (and every error reason) is optional. */
|
|
58
|
+
export type TelInputMessagesInput = Partial<Omit<TelInputMessages, 'errorMessages'>> & {
|
|
59
|
+
errorMessages?: Partial<Record<PhoneValidationReason, string>>;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export interface ATelInputProps {
|
|
63
|
+
class?: HTMLAttributes['class'];
|
|
64
|
+
placeholder?: string;
|
|
65
|
+
disabled?: boolean;
|
|
66
|
+
loading?: boolean;
|
|
67
|
+
size?: ATelInputSize;
|
|
68
|
+
/**
|
|
69
|
+
* Text direction. Omit (or pass `'auto'`) to inherit from the page — RTL pages get an
|
|
70
|
+
* RTL field automatically. Pass `'ltr'` / `'rtl'` to force it.
|
|
71
|
+
*/
|
|
72
|
+
dir?: ATelInputDir;
|
|
73
|
+
/**
|
|
74
|
+
* BCP-47 locale (e.g. `'ar'`, `'fr'`). When set, country names render localized via
|
|
75
|
+
* `Intl.DisplayNames` and the format hint uses the locale's numerals.
|
|
76
|
+
*/
|
|
77
|
+
locale?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Localized UI strings. A single bag covering the picker, validation errors, and a11y
|
|
80
|
+
* labels. Individual props (`searchPlaceholder`, `emptyText`, `loadingText`,
|
|
81
|
+
* `errorMessages`) take precedence over the matching `messages` key when both are set.
|
|
82
|
+
*/
|
|
83
|
+
messages?: TelInputMessagesInput;
|
|
84
|
+
/**
|
|
85
|
+
* Whitelist of allowed dial-digit codes (no `+`), e.g. `['20', '966']`.
|
|
86
|
+
* Countries outside this list are still shown in the picker but rendered as disabled.
|
|
87
|
+
*/
|
|
88
|
+
allowedDialCodes?: string[];
|
|
89
|
+
/** Light up the field's validation styling — coloured border + ring on the input and the
|
|
90
|
+
* error message line below — when the number is valid / invalid. Default `false`, so the
|
|
91
|
+
* field stays neutral and validation surfacing is left to the consumer (via the
|
|
92
|
+
* `validation` ref exposure). */
|
|
93
|
+
showValidation?: boolean;
|
|
94
|
+
/** Show the green check / red alert icon at the end of the field. Default `false`; opt
|
|
95
|
+
* in with `true`. Independent of `showValidation` — you can show the icon without the
|
|
96
|
+
* coloured field, or vice versa. The slots `#valid-icon` / `#error-icon` still apply. */
|
|
97
|
+
showValidationIcon?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Country auto-detect strategy. Defaults to `'auto'` — try IP geolocation first, then
|
|
100
|
+
* timezone, then `navigator.language`, finally `defaultCountry`.
|
|
101
|
+
*/
|
|
102
|
+
detectCountry?: DetectionStrategy;
|
|
103
|
+
/**
|
|
104
|
+
* Initial country. Accepts either an ISO2 code (`'EG'`) or a dial-digit string
|
|
105
|
+
* (`'20'`, `'+20'`). When set, the picker is visible at mount with this country
|
|
106
|
+
* pre-selected — overrides the hidden-until-detected default.
|
|
107
|
+
*/
|
|
108
|
+
defaultCountry?: string;
|
|
109
|
+
/** Override the IP geolocation endpoint. Must return JSON with `country_code` or `country`. */
|
|
110
|
+
ipEndpoint?: string;
|
|
111
|
+
/** Localized strings for the country picker UI. */
|
|
112
|
+
searchPlaceholder?: string;
|
|
113
|
+
emptyText?: string;
|
|
114
|
+
loadingText?: string;
|
|
115
|
+
/** Error labels keyed by reason. Each gets a sensible English default. */
|
|
116
|
+
errorMessages?: Partial<Record<PhoneValidationReason, string>>;
|
|
117
|
+
/**
|
|
118
|
+
* When true, the country picker is hidden until a leading dial code is detected in the
|
|
119
|
+
* phone input. Every keystroke runs a longest-prefix match against known dial codes; on
|
|
120
|
+
* first match the picker reveals with that country and the matched dial digits are
|
|
121
|
+
* stripped from `phone`. Skips the onMount IP/timezone/locale detection chain.
|
|
122
|
+
*/
|
|
123
|
+
detectFromInput?: boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Debounce window (ms) for `detectFromInput` detection. Each keystroke schedules the
|
|
126
|
+
* libphonenumber parse + lookup; bursts of typing/paste collapse into a single call.
|
|
127
|
+
* Clearing the input is not debounced — the picker hides immediately. Default 150ms.
|
|
128
|
+
*/
|
|
129
|
+
detectDebounceMs?: number;
|
|
130
|
+
/** Override the flag URL builder, forwarded to ACountrySelect. */
|
|
131
|
+
flagUrl?: (iso2: string, width: number) => string;
|
|
132
|
+
/** Custom search predicate, forwarded to ACountrySelect. */
|
|
133
|
+
searcher?: (query: string, country: CountryOption) => boolean;
|
|
134
|
+
/** Provide your own country list, forwarded to ACountrySelect. */
|
|
135
|
+
countries?: CountryOption[];
|
|
136
|
+
/**
|
|
137
|
+
* Fully custom country detection. When provided, this function runs in place of the
|
|
138
|
+
* built-in chain — `detectCountry`-style options are still honored but the function
|
|
139
|
+
* receives them and is free to ignore them.
|
|
140
|
+
*/
|
|
141
|
+
detector?: (options: DetectCountryOptions) => Promise<string | null | undefined>;
|
|
142
|
+
/** Forwarded to ACountrySelect: classes for the popover content surface. */
|
|
143
|
+
contentClass?: string;
|
|
144
|
+
/** Forwarded to ACountrySelect: classes for the desktop popover surface. */
|
|
145
|
+
popoverClass?: string;
|
|
146
|
+
/** Forwarded to ACountrySelect: classes for the mobile drawer surface. */
|
|
147
|
+
drawerClass?: string;
|
|
148
|
+
/** Classes for the inner phone field input element. */
|
|
149
|
+
inputClass?: string;
|
|
150
|
+
/** Classes for the outer wrapper that holds country select + input. */
|
|
151
|
+
fieldClass?: string;
|
|
152
|
+
/** Classes for the helper hint line. */
|
|
153
|
+
hintClass?: string;
|
|
154
|
+
/** Classes for the error message line. */
|
|
155
|
+
errorClass?: string;
|
|
156
|
+
/**
|
|
157
|
+
* How page scroll is blocked while the country popover is open. Defaults to `'events'`
|
|
158
|
+
* (sticky-safe document-level lock). Pass `'body'` for the legacy
|
|
159
|
+
* `body { overflow: hidden }` lock, or `'none'` to leave page scrolling alone.
|
|
160
|
+
*/
|
|
161
|
+
scrollLock?: 'events' | 'body' | 'none';
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Props for {@link ACountryFlag} — the standalone flag image component. Renders a
|
|
166
|
+
* `flagcdn` image for an ISO2 code with an automatic text-badge fallback when the
|
|
167
|
+
* image fails to load. Surface separately so it can be used outside `ATelInput`
|
|
168
|
+
* (e.g., in a custom country picker).
|
|
169
|
+
*/
|
|
170
|
+
export interface ACountryFlagProps {
|
|
171
|
+
/** ISO 3166-1 alpha-2 country code, case-insensitive. */
|
|
172
|
+
iso2: string;
|
|
173
|
+
/** Pixel width served by flagcdn. 40 is crisp at retina up to ~24px wide. */
|
|
174
|
+
width?: number;
|
|
175
|
+
/** Optional explicit URL override. When set, `iso2` / `width` / `flagUrl` are ignored. */
|
|
176
|
+
src?: string | null;
|
|
177
|
+
/** Function `(iso2, width) => string` — fully replace the URL builder. */
|
|
178
|
+
flagUrl?: FlagUrlBuilder;
|
|
179
|
+
alt?: string;
|
|
180
|
+
class?: HTMLAttributes['class'];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Slot prop shape for {@link ACountryFlag}. The `empty` slot is rendered when no
|
|
185
|
+
* flag URL is available and no ISO2 fallback can be derived.
|
|
186
|
+
*/
|
|
187
|
+
export interface ACountryFlagSlots {
|
|
188
|
+
/** Rendered when the flag URL is unavailable and no ISO2 text fallback can be derived. */
|
|
189
|
+
empty?: () => unknown;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export const DEFAULT_ERROR_MESSAGES: Record<PhoneValidationReason, string> = {
|
|
193
|
+
missing_country: 'Please select a country.',
|
|
194
|
+
country_not_supported: 'This country is not supported.',
|
|
195
|
+
phone_has_non_digits: 'Phone number can only contain digits.',
|
|
196
|
+
too_short: 'Phone number is too short.',
|
|
197
|
+
too_long: 'Phone number is too long.',
|
|
198
|
+
invalid_phone: 'Phone number is invalid.',
|
|
199
|
+
parse_failed: 'Could not parse phone number.',
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/** English defaults for every {@link TelInputMessages} key. */
|
|
203
|
+
export const DEFAULT_MESSAGES: TelInputMessages = {
|
|
204
|
+
searchPlaceholder: 'Search country or +code…',
|
|
205
|
+
emptyText: 'No countries found.',
|
|
206
|
+
loadingText: 'Loading countries…',
|
|
207
|
+
suggestedLabel: 'Suggested',
|
|
208
|
+
allCountriesLabel: 'All countries',
|
|
209
|
+
errorMessages: DEFAULT_ERROR_MESSAGES,
|
|
210
|
+
countryLabel: 'Country',
|
|
211
|
+
selectCountryLabel: 'Select country',
|
|
212
|
+
phoneInputLabel: 'Phone number',
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Merge a partial `messages` override onto the English defaults. Used internally by
|
|
217
|
+
* `ATelInput` to resolve a complete {@link TelInputMessages} object.
|
|
218
|
+
*/
|
|
219
|
+
export function resolveMessages(input?: TelInputMessagesInput): TelInputMessages {
|
|
220
|
+
if (!input) return DEFAULT_MESSAGES;
|
|
221
|
+
return {
|
|
222
|
+
...DEFAULT_MESSAGES,
|
|
223
|
+
...input,
|
|
224
|
+
errorMessages: { ...DEFAULT_ERROR_MESSAGES, ...input.errorMessages },
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Slot prop shape for {@link ATelInput}. Use to get full slot-prop type inference
|
|
230
|
+
* when overriding slots in a consumer template:
|
|
231
|
+
*
|
|
232
|
+
* <ATelInput #suffix="{ validationState }">…</ATelInput>
|
|
233
|
+
* ↑ inferred as `'idle' | 'valid' | 'error'`
|
|
234
|
+
*
|
|
235
|
+
* Or in TypeScript code:
|
|
236
|
+
* type SuffixProps = Parameters<NonNullable<ATelInputSlots['suffix']>>[0];
|
|
237
|
+
*/
|
|
238
|
+
export interface ATelInputSlots {
|
|
239
|
+
/** Content before the country select trigger (e.g. an icon). */
|
|
240
|
+
prefix?: () => unknown;
|
|
241
|
+
/** Content between the input and the validation icons. */
|
|
242
|
+
suffix?: (props: {
|
|
243
|
+
validationState: 'idle' | 'valid' | 'error';
|
|
244
|
+
validation: PhoneValidationResult;
|
|
245
|
+
}) => unknown;
|
|
246
|
+
/** Replace the green check shown when the number validates. */
|
|
247
|
+
'valid-icon'?: () => unknown;
|
|
248
|
+
/** Replace the warning icon shown when the number fails validation. */
|
|
249
|
+
'error-icon'?: (props: { reason: string }) => unknown;
|
|
250
|
+
/** Replace the dim helper line shown below the input when empty. */
|
|
251
|
+
hint?: (props: { country: string; formatHint: string; example: string | null }) => unknown;
|
|
252
|
+
/** Replace the error message rendered when invalid. */
|
|
253
|
+
error?: (props: {
|
|
254
|
+
message: string;
|
|
255
|
+
reason: string;
|
|
256
|
+
validation: PhoneValidationResult;
|
|
257
|
+
}) => unknown;
|
|
258
|
+
/** Forwarded to ACountrySelect — replace the trigger button. */
|
|
259
|
+
trigger?: (props: {
|
|
260
|
+
selectedCountry: CountryOption | null;
|
|
261
|
+
open: boolean;
|
|
262
|
+
sizeClasses: string;
|
|
263
|
+
}) => unknown;
|
|
264
|
+
/** Forwarded to ACountrySelect — replace the chevron. */
|
|
265
|
+
chevron?: (props: { open: boolean }) => unknown;
|
|
266
|
+
/** Forwarded — replace any flag rendering. */
|
|
267
|
+
flag?: (props: { country: CountryOption; context: 'trigger' | 'item' }) => unknown;
|
|
268
|
+
/** Forwarded — replace each country list row. */
|
|
269
|
+
item?: (props: {
|
|
270
|
+
country: CountryOption;
|
|
271
|
+
selected: boolean;
|
|
272
|
+
disabled: boolean;
|
|
273
|
+
select: () => void;
|
|
274
|
+
}) => unknown;
|
|
275
|
+
/** Forwarded — section header. */
|
|
276
|
+
'group-header'?: (props: { label: string; group: 'suggested' | 'all' }) => unknown;
|
|
277
|
+
/** Forwarded — search bar. */
|
|
278
|
+
search?: (props: {
|
|
279
|
+
value: string;
|
|
280
|
+
setValue: (v: string) => void;
|
|
281
|
+
isSearching: boolean;
|
|
282
|
+
}) => unknown;
|
|
283
|
+
loading?: () => unknown;
|
|
284
|
+
empty?: (props: { query: string }) => unknown;
|
|
285
|
+
/** Replace the spinner shown in the picker slot during the debounce window. */
|
|
286
|
+
detecting?: () => unknown;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Emit map for {@link ATelInput}. `update:phone` carries the digits-only string,
|
|
291
|
+
* `update:country` carries the dial-number (not ISO2). Surface for consumers who
|
|
292
|
+
* wire the events manually instead of via `v-model:phone` / `v-model:country`.
|
|
293
|
+
*/
|
|
294
|
+
export type ATelInputEmits = {
|
|
295
|
+
'update:phone': [value: string];
|
|
296
|
+
'update:country': [value: number | null];
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Props for {@link ACountrySelect} — the standalone country picker. Surface
|
|
301
|
+
* separately so it can be used outside `ATelInput` with full type support.
|
|
302
|
+
*/
|
|
303
|
+
export interface ACountrySelectProps {
|
|
304
|
+
class?: HTMLAttributes['class'];
|
|
305
|
+
triggerClass?: HTMLAttributes['class'];
|
|
306
|
+
contentClass?: HTMLAttributes['class'];
|
|
307
|
+
popoverClass?: HTMLAttributes['class'];
|
|
308
|
+
drawerClass?: HTMLAttributes['class'];
|
|
309
|
+
searchPlaceholder?: string;
|
|
310
|
+
emptyText?: string;
|
|
311
|
+
loadingText?: string;
|
|
312
|
+
suggestedLabel?: string;
|
|
313
|
+
allCountriesLabel?: string;
|
|
314
|
+
/** ISO2 codes that are selectable. Others are listed but disabled. */
|
|
315
|
+
allowedDialCodes?: string[];
|
|
316
|
+
disabled?: boolean;
|
|
317
|
+
/** Drives the trigger button padding + text size. Matches ATelInput's `size`. */
|
|
318
|
+
size?: ATelInputSize;
|
|
319
|
+
/** Max items rendered under the "Suggested" header (current + recents, deduped). */
|
|
320
|
+
suggestedLimit?: number;
|
|
321
|
+
/** Cap the number of matching countries shown in search results. */
|
|
322
|
+
maxResults?: number;
|
|
323
|
+
/** Override the flag URL builder, e.g. `(iso, w) => `/flags/${iso}.svg``. */
|
|
324
|
+
flagUrl?: (iso2: string, width: number) => string;
|
|
325
|
+
/** Custom search predicate. Default: substring match on the precomputed `search_key`. */
|
|
326
|
+
searcher?: (query: string, country: CountryOption) => boolean;
|
|
327
|
+
/** Provide your own country list (bypasses the REST Countries fetch). */
|
|
328
|
+
countries?: CountryOption[];
|
|
329
|
+
/** Override the right-side kbd hints. Pass `null` to hide. */
|
|
330
|
+
kbdOpen?: string | null;
|
|
331
|
+
kbdClose?: string | null;
|
|
332
|
+
/** BCP-47 locale — country names render localized via `Intl.DisplayNames`. */
|
|
333
|
+
locale?: string;
|
|
334
|
+
/** Prefix of the trigger's `aria-label` when a country is selected, e.g. `"Country"`. */
|
|
335
|
+
countryLabel?: string;
|
|
336
|
+
/** Trigger's `aria-label` when no country is selected. */
|
|
337
|
+
selectCountryLabel?: string;
|
|
338
|
+
/** How page scroll is blocked while the popover is open. Default `'events'`. */
|
|
339
|
+
scrollLock?: 'events' | 'body' | 'none';
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Slot prop shape for {@link ACountrySelect}. Forwarded versions of these slots
|
|
344
|
+
* also appear on {@link ATelInputSlots} (`trigger`, `chevron`, `flag`, `item`,
|
|
345
|
+
* etc.) — keep them in sync when changing one.
|
|
346
|
+
*/
|
|
347
|
+
export interface ACountrySelectSlots {
|
|
348
|
+
/** Replace the entire country picker trigger button. */
|
|
349
|
+
trigger?: (props: {
|
|
350
|
+
selectedCountry: CountryOption | null;
|
|
351
|
+
open: boolean;
|
|
352
|
+
sizeClasses: string;
|
|
353
|
+
}) => unknown;
|
|
354
|
+
/** Replace the chevron icon. */
|
|
355
|
+
chevron?: (props: { open: boolean }) => unknown;
|
|
356
|
+
/** Replace just the flag rendered in the trigger and items. */
|
|
357
|
+
flag?: (props: { country: CountryOption; context: 'trigger' | 'item' }) => unknown;
|
|
358
|
+
/** Replace the entire search bar (input + icon + kbd). */
|
|
359
|
+
search?: (props: {
|
|
360
|
+
value: string;
|
|
361
|
+
setValue: (v: string) => void;
|
|
362
|
+
isSearching: boolean;
|
|
363
|
+
}) => unknown;
|
|
364
|
+
/** Replace the search-bar leading icon. */
|
|
365
|
+
'search-icon'?: () => unknown;
|
|
366
|
+
/** Replace the loading state. */
|
|
367
|
+
loading?: () => unknown;
|
|
368
|
+
/** Replace the empty/no-results state. */
|
|
369
|
+
empty?: (props: { query: string }) => unknown;
|
|
370
|
+
/** Replace a section header. */
|
|
371
|
+
'group-header'?: (props: { label: string; group: 'suggested' | 'all' }) => unknown;
|
|
372
|
+
/** Replace each country list row. */
|
|
373
|
+
item?: (props: {
|
|
374
|
+
country: CountryOption;
|
|
375
|
+
selected: boolean;
|
|
376
|
+
disabled: boolean;
|
|
377
|
+
select: () => void;
|
|
378
|
+
}) => unknown;
|
|
379
|
+
/** Replace just the right-side check icon for the selected row. */
|
|
380
|
+
'item-check'?: (props: { country: CountryOption }) => unknown;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Emit map for {@link ACountrySelect}. The `selected` is `v-model:selected`,
|
|
385
|
+
* carrying the ISO2 code of the picked country.
|
|
386
|
+
*/
|
|
387
|
+
export type ACountrySelectEmits = {
|
|
388
|
+
'update:selected': [value: string];
|
|
389
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alternative-numeral support. Phone numbers are routinely entered with the digits of the
|
|
3
|
+
* user's own script — Arabic-Indic, Persian/Urdu, Devanagari, Bengali. `libphonenumber-js`
|
|
4
|
+
* and our own `\d` cleanup only understand ASCII `0-9`, so anything else silently becomes
|
|
5
|
+
* an empty number. `normalizeDigits` folds those scripts down to ASCII before validation.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Base code points of contiguous decimal-digit blocks. Each block runs `base`‥`base+9`
|
|
10
|
+
* for digit `0`‥`9`, so the ASCII digit is `codePoint - base`. Add a script by appending
|
|
11
|
+
* one entry here.
|
|
12
|
+
*/
|
|
13
|
+
export const LOCALE_DIGIT_RANGES: { name: string; base: number }[] = [
|
|
14
|
+
{ name: 'arabic-indic', base: 0x0660 }, // ٠١٢٣٤٥٦٧٨٩
|
|
15
|
+
{ name: 'extended-arabic', base: 0x06f0 }, // ۰۱۲۳۴۵۶۷۸۹ — Persian / Urdu
|
|
16
|
+
{ name: 'devanagari', base: 0x0966 }, // ०१२३४५६७८९
|
|
17
|
+
{ name: 'bengali', base: 0x09e6 }, // ০১২৩৪৫৬৭৮৯
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/** Lookup of every non-ASCII digit code point → its ASCII character. */
|
|
21
|
+
const DIGIT_MAP: Map<number, string> = (() => {
|
|
22
|
+
const map = new Map<number, string>();
|
|
23
|
+
for (const { base } of LOCALE_DIGIT_RANGES) {
|
|
24
|
+
for (let d = 0; d <= 9; d++) map.set(base + d, String(d));
|
|
25
|
+
}
|
|
26
|
+
return map;
|
|
27
|
+
})();
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Replace any supported non-ASCII decimal digit with its ASCII equivalent. Every other
|
|
31
|
+
* character (spaces, `+`, separators, letters) is left untouched — callers still run their
|
|
32
|
+
* own `\D` cleanup afterwards.
|
|
33
|
+
*/
|
|
34
|
+
export function normalizeDigits(input: string): string {
|
|
35
|
+
const str = String(input ?? '');
|
|
36
|
+
let out = '';
|
|
37
|
+
for (const ch of str) {
|
|
38
|
+
const cp = ch.codePointAt(0);
|
|
39
|
+
out += (cp != null && DIGIT_MAP.get(cp)) || ch;
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default flag URL builder — flagcdn.com hosts PNG flags at multiple widths and is
|
|
3
|
+
* generous with caching + no API key required. Swap via the `flagUrl` prop on
|
|
4
|
+
* ATelInput / ACountrySelect / ACountryFlag to use any other source.
|
|
5
|
+
*/
|
|
6
|
+
export function defaultFlagUrl(iso2: string, width = 40): string {
|
|
7
|
+
return `https://flagcdn.com/w${width}/${iso2.toLowerCase()}.png`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type FlagUrlBuilder = (iso2: string, width: number) => string;
|