@intl-tel-input/svelte 28.0.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/LICENSE +21 -0
- package/README.md +14 -0
- package/package.json +56 -0
- package/src/IntlTelInput.svelte +233 -0
- package/src/IntlTelInput.svelte.d.ts +36 -0
- package/src/IntlTelInputWithUtils.svelte +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-2016 Jack O'Connor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# IntlTelInput Svelte Component
|
|
2
|
+
|
|
3
|
+
A Svelte 5 component for the [intl-tel-input](https://github.com/jackocnr/intl-tel-input) JavaScript plugin. View the [source code](https://github.com/jackocnr/intl-tel-input/blob/master/packages/svelte/src/IntlTelInput.svelte).
|
|
4
|
+
|
|
5
|
+
[Explore docs »](https://intl-tel-input.com/docs/svelte-component)
|
|
6
|
+
|
|
7
|
+
## Running the demos locally
|
|
8
|
+
|
|
9
|
+
1. Initialise the submodules: `git submodule update --init --recursive`
|
|
10
|
+
2. Install dependencies: `npm install`
|
|
11
|
+
3. Build: `npm run build`
|
|
12
|
+
4. Run a demo: `npm run svelte:demo` and copy the given URL into your browser.
|
|
13
|
+
|
|
14
|
+
This defaults to the validation demo — to run a different one, set the `DEMO` env var, e.g. `DEMO=simple npm run svelte:demo`. View the full list of [available demos](https://github.com/jackocnr/intl-tel-input/tree/master/packages/svelte/demo).
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@intl-tel-input/svelte",
|
|
3
|
+
"version": "28.0.0",
|
|
4
|
+
"description": "A Svelte component for the intl-tel-input JavaScript plugin",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Jack O'Connor (http://jackocnr.com)",
|
|
8
|
+
"types": "./src/IntlTelInput.svelte.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./src/IntlTelInput.svelte.d.ts",
|
|
12
|
+
"svelte": "./src/IntlTelInput.svelte",
|
|
13
|
+
"import": "./src/IntlTelInput.svelte"
|
|
14
|
+
},
|
|
15
|
+
"./with-utils": {
|
|
16
|
+
"types": "./src/IntlTelInput.svelte.d.ts",
|
|
17
|
+
"svelte": "./src/IntlTelInputWithUtils.svelte",
|
|
18
|
+
"import": "./src/IntlTelInputWithUtils.svelte"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src/*",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"package.json",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"intl-tel-input": "^28.0.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"svelte": "^5.0.0"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/jackocnr/intl-tel-input.git",
|
|
39
|
+
"directory": "packages/svelte"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://intl-tel-input.com/docs/svelte-component",
|
|
42
|
+
"bugs": "https://github.com/jackocnr/intl-tel-input/issues",
|
|
43
|
+
"keywords": [
|
|
44
|
+
"intl-tel-input",
|
|
45
|
+
"svelte",
|
|
46
|
+
"component",
|
|
47
|
+
"international",
|
|
48
|
+
"telephone",
|
|
49
|
+
"phone",
|
|
50
|
+
"input",
|
|
51
|
+
"flag",
|
|
52
|
+
"dropdown",
|
|
53
|
+
"validation",
|
|
54
|
+
"typescript"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
import intlTelInput, { type Iti } from "intl-tel-input";
|
|
3
|
+
export { intlTelInput };
|
|
4
|
+
</script>
|
|
5
|
+
|
|
6
|
+
<script lang="ts">
|
|
7
|
+
// Resolves to IntlTelInput.svelte.d.ts (the type declaration file for this component).
|
|
8
|
+
import type { Props } from "./IntlTelInput.svelte";
|
|
9
|
+
import type { SomeOptions, ValidationError } from "intl-tel-input";
|
|
10
|
+
import { onMount, onDestroy } from "svelte";
|
|
11
|
+
|
|
12
|
+
// Props
|
|
13
|
+
let {
|
|
14
|
+
disabled = false,
|
|
15
|
+
readonly = false,
|
|
16
|
+
inputProps = {},
|
|
17
|
+
usePreciseValidation = false,
|
|
18
|
+
initialValue = "",
|
|
19
|
+
value = undefined,
|
|
20
|
+
onChangeNumber,
|
|
21
|
+
onChangeCountry,
|
|
22
|
+
onChangeValidity,
|
|
23
|
+
onChangeErrorCode,
|
|
24
|
+
onOpenCountryDropdown,
|
|
25
|
+
onCloseCountryDropdown,
|
|
26
|
+
onStrictReject,
|
|
27
|
+
...initOptions
|
|
28
|
+
} = $props() as Props;
|
|
29
|
+
|
|
30
|
+
type StrictRejectDetail = {
|
|
31
|
+
source: "key" | "paste";
|
|
32
|
+
rejectedInput: string;
|
|
33
|
+
reason: "invalid" | "max-length";
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// State
|
|
37
|
+
let inputElement: HTMLInputElement | undefined = $state();
|
|
38
|
+
let instance: Iti | undefined = $state();
|
|
39
|
+
// Classes the plugin adds directly to the input (e.g. iti__tel-input)
|
|
40
|
+
let pluginInputClasses = $state("");
|
|
41
|
+
let lastEmittedNumber: string | undefined = $state();
|
|
42
|
+
let lastEmittedCountry: string | undefined = $state();
|
|
43
|
+
let lastEmittedValidity: boolean | undefined = $state();
|
|
44
|
+
let lastEmittedErrorCode: ValidationError | null | undefined = $state();
|
|
45
|
+
let hasInitialized = $state(false);
|
|
46
|
+
// if an input event fires before utils has loaded, we defer the update until the promise resolves
|
|
47
|
+
let pendingUpdate = false;
|
|
48
|
+
|
|
49
|
+
// Validation helper
|
|
50
|
+
const isValid = (): boolean | null => {
|
|
51
|
+
if (!instance) return null;
|
|
52
|
+
return usePreciseValidation
|
|
53
|
+
? instance.isValidNumberPrecise()
|
|
54
|
+
: instance.isValidNumber();
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// Update handlers
|
|
58
|
+
const updateValidity = () => {
|
|
59
|
+
if (!instance) return;
|
|
60
|
+
// if utils has not loaded yet, isValidNumber/getValidationError will throw. defer until the promise resolves.
|
|
61
|
+
if (!intlTelInput.utils) {
|
|
62
|
+
pendingUpdate = true;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const isCurrentlyValid = isValid();
|
|
66
|
+
if (isCurrentlyValid === null) return;
|
|
67
|
+
|
|
68
|
+
const valid = !!isCurrentlyValid;
|
|
69
|
+
const errorCode = valid ? null : instance.getValidationError();
|
|
70
|
+
|
|
71
|
+
if (valid !== lastEmittedValidity) {
|
|
72
|
+
lastEmittedValidity = valid;
|
|
73
|
+
onChangeValidity?.(valid);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (errorCode !== lastEmittedErrorCode) {
|
|
77
|
+
lastEmittedErrorCode = errorCode;
|
|
78
|
+
onChangeErrorCode?.(errorCode);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const updateValue = () => {
|
|
83
|
+
if (!instance?.isActive()) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
// if utils has not loaded yet, getNumber will throw. defer until the promise resolves.
|
|
87
|
+
if (!intlTelInput.utils) {
|
|
88
|
+
pendingUpdate = true;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const number = instance.getNumber() ?? "";
|
|
92
|
+
if (number !== lastEmittedNumber) {
|
|
93
|
+
lastEmittedNumber = number;
|
|
94
|
+
onChangeNumber?.(number);
|
|
95
|
+
}
|
|
96
|
+
updateValidity();
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const updateCountry = () => {
|
|
100
|
+
if (!instance?.isActive()) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const country = instance.getSelectedCountryData()?.iso2 ?? "";
|
|
104
|
+
if (country !== lastEmittedCountry) {
|
|
105
|
+
lastEmittedCountry = country;
|
|
106
|
+
onChangeCountry?.(country);
|
|
107
|
+
}
|
|
108
|
+
updateValue();
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const handleOpenDropdown = (): void => onOpenCountryDropdown?.();
|
|
112
|
+
const handleCloseDropdown = (): void => onCloseCountryDropdown?.();
|
|
113
|
+
const handleStrictReject = (e: Event): void => {
|
|
114
|
+
const { source, rejectedInput, reason } = (e as CustomEvent<StrictRejectDetail>).detail;
|
|
115
|
+
onStrictReject?.(source, rejectedInput, reason);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// Lifecycle
|
|
119
|
+
onMount(() => {
|
|
120
|
+
if (inputElement) {
|
|
121
|
+
instance = intlTelInput(inputElement, initOptions as SomeOptions);
|
|
122
|
+
pluginInputClasses = inputElement.className;
|
|
123
|
+
if (disabled) instance.setDisabled(disabled);
|
|
124
|
+
if (readonly) instance.setReadonly(readonly);
|
|
125
|
+
|
|
126
|
+
inputElement.addEventListener("open:countrydropdown", handleOpenDropdown);
|
|
127
|
+
inputElement.addEventListener("close:countrydropdown", handleCloseDropdown);
|
|
128
|
+
inputElement.addEventListener("strict:reject", handleStrictReject);
|
|
129
|
+
|
|
130
|
+
lastEmittedCountry = instance.getSelectedCountryData()?.iso2 ?? "";
|
|
131
|
+
hasInitialized = true;
|
|
132
|
+
|
|
133
|
+
// wait for utils to load before calling methods that require it (getNumber, setNumber, isValidNumber, etc.)
|
|
134
|
+
instance.promise.then(() => {
|
|
135
|
+
if (!instance?.isActive()) return;
|
|
136
|
+
if (initialValue) instance.setNumber(initialValue);
|
|
137
|
+
// if an input event fired during the utils-loading gap, replay it now so the skipped emissions fire.
|
|
138
|
+
// otherwise seed silently so we don't fire change callbacks on initial mount.
|
|
139
|
+
if (pendingUpdate) {
|
|
140
|
+
pendingUpdate = false;
|
|
141
|
+
updateCountry();
|
|
142
|
+
} else {
|
|
143
|
+
lastEmittedNumber = instance.getNumber() ?? "";
|
|
144
|
+
const initialValid = isValid();
|
|
145
|
+
if (initialValid !== null) {
|
|
146
|
+
lastEmittedValidity = !!initialValid;
|
|
147
|
+
lastEmittedErrorCode = initialValid ? null : instance.getValidationError();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
onDestroy(() => {
|
|
155
|
+
if (inputElement) {
|
|
156
|
+
inputElement.removeEventListener("open:countrydropdown", handleOpenDropdown);
|
|
157
|
+
inputElement.removeEventListener("close:countrydropdown", handleCloseDropdown);
|
|
158
|
+
inputElement.removeEventListener("strict:reject", handleStrictReject);
|
|
159
|
+
}
|
|
160
|
+
instance?.destroy();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Watch disabled prop changes (only after initialization)
|
|
164
|
+
$effect(() => {
|
|
165
|
+
if (hasInitialized && instance) {
|
|
166
|
+
instance.setDisabled(disabled);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Watch readonly prop changes (only after initialization)
|
|
171
|
+
$effect(() => {
|
|
172
|
+
if (hasInitialized && instance) {
|
|
173
|
+
instance.setReadonly(readonly);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Watch value prop changes (only after initialization).
|
|
178
|
+
// If value is undefined, the component is uncontrolled — do not touch the input
|
|
179
|
+
// (otherwise we would clobber any initialValue with an empty string on mount).
|
|
180
|
+
$effect(() => {
|
|
181
|
+
if (!hasInitialized || !instance || value === undefined) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const next = value ?? "";
|
|
185
|
+
// wait for utils to load before calling methods that require it
|
|
186
|
+
instance.promise.then(() => {
|
|
187
|
+
if (!instance?.isActive()) return;
|
|
188
|
+
const currentCanonical = instance.getNumber() ?? "";
|
|
189
|
+
const isFocused = document.activeElement === inputElement;
|
|
190
|
+
if (isFocused || currentCanonical === next) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
instance.setNumber(next);
|
|
194
|
+
updateValidity();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Expose instance and input for parent access
|
|
199
|
+
export function getInstance(): Iti | undefined {
|
|
200
|
+
return instance;
|
|
201
|
+
}
|
|
202
|
+
export function getInput(): HTMLInputElement | undefined {
|
|
203
|
+
return inputElement;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const warnInputProp = (prop: string): void => {
|
|
207
|
+
console.warn(`intl-tel-input: ignoring inputProps.${prop} - see docs for more info.`);
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const ignoredInputProps = new Set(["type", "value", "disabled", "readonly", "oninput"]);
|
|
211
|
+
|
|
212
|
+
const sanitizeInputProps = (props: Record<string, unknown>) => {
|
|
213
|
+
const rest: Record<string, unknown> = {};
|
|
214
|
+
for (const [key, val] of Object.entries(props)) {
|
|
215
|
+
if (ignoredInputProps.has(key)) {
|
|
216
|
+
warnInputProp(key);
|
|
217
|
+
} else if (key === "class") {
|
|
218
|
+
rest[key] = `${pluginInputClasses} ${val}`;
|
|
219
|
+
} else {
|
|
220
|
+
rest[key] = val;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return rest;
|
|
224
|
+
};
|
|
225
|
+
</script>
|
|
226
|
+
|
|
227
|
+
<!-- inputProps must come first, so cannot override the other required props -->
|
|
228
|
+
<input
|
|
229
|
+
{...sanitizeInputProps(inputProps)}
|
|
230
|
+
bind:this={inputElement}
|
|
231
|
+
type="tel"
|
|
232
|
+
oninput={updateCountry}
|
|
233
|
+
/>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Hand-written because svelte2tsx can't generate correct prop types for Svelte 5's $props() rune.
|
|
2
|
+
// See: https://github.com/sveltejs/svelte/discussions/13164
|
|
3
|
+
import type { Component } from "svelte";
|
|
4
|
+
import type { Iti, SomeOptions, ValidationError } from "intl-tel-input";
|
|
5
|
+
import intlTelInput from "intl-tel-input";
|
|
6
|
+
|
|
7
|
+
export type StrictRejectSource = "key" | "paste";
|
|
8
|
+
export type StrictRejectReason = "invalid" | "max-length";
|
|
9
|
+
|
|
10
|
+
export type Props = SomeOptions & {
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
readonly?: boolean;
|
|
13
|
+
inputProps?: Record<string, unknown>;
|
|
14
|
+
initialValue?: string;
|
|
15
|
+
value?: string | null;
|
|
16
|
+
usePreciseValidation?: boolean;
|
|
17
|
+
onChangeNumber?: (number: string) => void;
|
|
18
|
+
onChangeCountry?: (iso2: string) => void;
|
|
19
|
+
onChangeValidity?: (isValid: boolean) => void;
|
|
20
|
+
onChangeErrorCode?: (errorCode: ValidationError | null) => void;
|
|
21
|
+
onOpenCountryDropdown?: () => void;
|
|
22
|
+
onCloseCountryDropdown?: () => void;
|
|
23
|
+
onStrictReject?: (source: StrictRejectSource, rejectedInput: string, reason: StrictRejectReason) => void;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { intlTelInput };
|
|
27
|
+
|
|
28
|
+
declare const IntlTelInput: Component<
|
|
29
|
+
Props,
|
|
30
|
+
{
|
|
31
|
+
getInstance: () => Iti | undefined;
|
|
32
|
+
getInput: () => HTMLInputElement | undefined;
|
|
33
|
+
}
|
|
34
|
+
>;
|
|
35
|
+
|
|
36
|
+
export default IntlTelInput;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
import intlTelInput from "intl-tel-input";
|
|
3
|
+
import utils from "intl-tel-input/utils";
|
|
4
|
+
intlTelInput.utils = utils as typeof intlTelInput.utils;
|
|
5
|
+
export { intlTelInput };
|
|
6
|
+
</script>
|
|
7
|
+
|
|
8
|
+
<script lang="ts">
|
|
9
|
+
import IntlTelInput from "./IntlTelInput.svelte";
|
|
10
|
+
import type { ComponentProps } from "svelte";
|
|
11
|
+
import type { Iti } from "intl-tel-input";
|
|
12
|
+
|
|
13
|
+
let { ...props }: ComponentProps<typeof IntlTelInput> = $props();
|
|
14
|
+
let inner: ReturnType<typeof IntlTelInput> | undefined = $state();
|
|
15
|
+
|
|
16
|
+
export function getInstance(): Iti | undefined {
|
|
17
|
+
return inner?.getInstance();
|
|
18
|
+
}
|
|
19
|
+
export function getInput(): HTMLInputElement | undefined {
|
|
20
|
+
return inner?.getInput();
|
|
21
|
+
}
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<IntlTelInput bind:this={inner} {...props} />
|