Skip to content

Commit 30ee204

Browse files
authored
Merge pull request #228 from ieedan/more-ergonomic-country-binding
fix: Allow `undefined` as an option for country for cases when it is not provided
2 parents 4ed1584 + 2eaa5e0 commit 30ee204

File tree

3 files changed

+10
-24
lines changed

3 files changed

+10
-24
lines changed

.changeset/two-pigs-sing.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-tel-input': patch
3+
---
4+
5+
fix: Allow for `undefined` as a value for country making the input easier to consume in reusable components.

packages/svelte-tel-input/src/lib/components/input/TelInput.svelte

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
generatePlaceholder,
88
telInputAction
99
} from '$lib/utils/index.js';
10-
import { watcher } from '$lib/stores/index.js';
1110
import type { DetailedValue, CountryCode, E164Number, TelInputOptions } from '$lib/types';
1211
1312
const dispatch = createEventDispatcher<{
@@ -47,7 +46,7 @@
4746
/** The core value of the input, this is the only one what you can store. It's an E164 phone number.*/
4847
export let value: E164Number | null;
4948
/** It's accept any Country Code Alpha-2 (ISO 3166) */
50-
export let country: CountryCode | null;
49+
export let country: CountryCode | null | undefined = undefined;
5150
/** Detailed parse of the E164 phone number */
5251
export let detailedValue: Partial<DetailedValue> | null = null;
5352
/** Validity of the input based on the config settings.*/
@@ -150,14 +149,14 @@
150149
151150
// Watch user's country change.
152151
let countryWatchInitRun = true;
153-
const countryChangeWatchFunction = () => {
152+
const countryChangeWatchFunction = (current: CountryCode | null | undefined) => {
154153
if (!countryWatchInitRun) {
155-
handleParsePhoneNumber(null, country);
154+
handleParsePhoneNumber(null, current);
156155
}
157156
countryWatchInitRun = false;
158157
};
159-
const countryChangeWatch = watcher(null, countryChangeWatchFunction);
160-
$: $countryChangeWatch = country;
158+
159+
$: countryChangeWatchFunction(country);
161160
162161
// Generate placeholder based on the autoPlaceholder option
163162
$: getPlaceholder =

packages/svelte-tel-input/src/lib/stores/index.ts

-18
This file was deleted.

0 commit comments

Comments
 (0)