svelte-tel-input 0.6.3 → 0.7.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.
Files changed (55) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +1 -1
  3. package/assets/allCountry.d.ts +2 -3
  4. package/assets/allCountry.js +2 -3
  5. package/assets/countries_deprecated.d.ts +1 -0
  6. package/assets/{countries.js → countries_deprecated.js} +1 -2
  7. package/assets/index.d.ts +3 -0
  8. package/assets/index.js +3 -0
  9. package/assets/regions.d.ts +1 -2
  10. package/assets/regions.js +1 -2
  11. package/assets/telTypes.d.ts +5 -0
  12. package/assets/telTypes.js +16 -0
  13. package/components/Input/TelInput.svelte +17 -8
  14. package/components/Input/TelInput.svelte.d.ts +9 -1
  15. package/components/Select/Select.svelte +8 -3
  16. package/components/Select/Select.svelte.d.ts +2 -1
  17. package/components/Select/TelCountrySelect/TelCountrySelect.svelte.d.ts +1 -1
  18. package/components/Select/TelRegionSelect/TelRegionSelectOption.svelte.d.ts +2 -2
  19. package/components/Select/TelTypeSelect/TelTypeSelect.svelte +1 -1
  20. package/components/Select/TelTypeSelect/TelTypeSelect.svelte.d.ts +2 -2
  21. package/components/Select/TelTypeSelect/TelTypeSelectOption.svelte.d.ts +2 -2
  22. package/package.json +28 -22
  23. package/stores/index.d.ts +1 -1
  24. package/{models/types → types}/DynamicSvelteComponent.type.d.ts +0 -0
  25. package/{models/types → types}/DynamicSvelteComponent.type.js +0 -0
  26. package/types/Select.type.d.ts +2 -0
  27. package/{models/types → types}/Select.type.js +0 -0
  28. package/types/enums/PhoneNumberParseError.enum.d.ts +6 -0
  29. package/types/enums/PhoneNumberParseError.enum.js +7 -0
  30. package/{models → types}/enums/PhoneType.enum.d.ts +0 -0
  31. package/{models → types}/enums/PhoneType.enum.js +0 -0
  32. package/types/enums/index.d.ts +2 -0
  33. package/types/enums/index.js +2 -0
  34. package/types/index.d.ts +4 -0
  35. package/types/index.js +2 -0
  36. package/{models → types}/interfaces/Country.interface.d.ts +2 -1
  37. package/{models → types}/interfaces/Country.interface.js +0 -0
  38. package/types/interfaces/Phone.interface.d.ts +5 -0
  39. package/{models/interfaces/Select.interface.js → types/interfaces/Phone.interface.js} +0 -0
  40. package/types/interfaces/Select.interface.d.ts +5 -0
  41. package/types/interfaces/Select.interface.js +1 -0
  42. package/types/interfaces/index.d.ts +3 -0
  43. package/types/interfaces/index.js +1 -0
  44. package/utils/helpers.d.ts +4 -0
  45. package/utils/helpers.js +25 -4
  46. package/utils/typeCheck.d.ts +1 -1
  47. package/views/Usage.svelte +55 -0
  48. package/views/Usage.svelte.d.ts +17 -0
  49. package/assets/countries.d.ts +0 -2
  50. package/models/enums/index.d.ts +0 -1
  51. package/models/enums/index.js +0 -1
  52. package/models/index.d.ts +0 -1
  53. package/models/index.js +0 -1
  54. package/models/interfaces/Select.interface.d.ts +0 -5
  55. package/models/types/Select.type.d.ts +0 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # svelte-tel-input
2
2
 
3
+ ## 0.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - feat: example page payload feature, parsing improvements" ([#68](https://github.com/gyurielf/svelte-tel-input/pull/68))
8
+
9
+ ## 0.6.5
10
+
11
+ ### Patch Changes
12
+
13
+ - fix: add changeset ([#61](https://github.com/gyurielf/svelte-tel-input/pull/61))
14
+
15
+ ## 0.6.4
16
+
17
+ ### Patch Changes
18
+
19
+ - chore: update to vite 3 ([#58](https://github.com/gyurielf/svelte-tel-input/pull/58))
20
+
3
21
  ## 0.6.3
4
22
 
5
23
  ### Patch Changes
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Svelte Tel Input
2
2
 
3
- ## Under heavy development!
3
+ ## Under development!
4
4
 
5
5
  <ins>Do not use it before 1.0, it won't works properly!</ins>
6
6
 
@@ -1,3 +1,2 @@
1
- import type { Country } from '../models/interfaces/Country.interface';
2
- declare const restructuredCountries: Country[];
3
- export default restructuredCountries;
1
+ import type { Country } from '../types/interfaces/Country.interface';
2
+ export declare const normalizedCountries: Country[];
@@ -294,13 +294,12 @@ const allCountries = [
294
294
  ['Zimbabwe', 'zw', '263'],
295
295
  ['Åland Islands', 'ax', '358', 1, ['18']]
296
296
  ];
297
- const restructuredCountries = allCountries.map((country) => {
297
+ export const normalizedCountries = allCountries.map((country) => {
298
298
  return {
299
299
  name: country[0],
300
- iso2: country[1],
300
+ iso2: country[1].toUpperCase(),
301
301
  dialCode: country[2],
302
302
  priority: country[3] || 0,
303
303
  areaCodes: country[4] || null
304
304
  };
305
305
  });
306
- export default restructuredCountries;
@@ -0,0 +1 @@
1
+ export declare const rawCountries: (string | number | string[])[][];
@@ -15,7 +15,7 @@
15
15
  // Sub-regions:
16
16
  // ['north-america', 'south-america', 'central-america', 'carribean',
17
17
  // 'eu-union', 'ex-ussr', 'ex-yugos', 'baltic', 'middle-east', 'north-africa']
18
- const rawCountries = [
18
+ export const rawCountries = [
19
19
  ['Afghanistan', ['asia'], 'af', '93'],
20
20
  ['Albania', ['europe'], 'al', '355'],
21
21
  ['Algeria', ['africa', 'north-africa'], 'dz', '213'],
@@ -600,4 +600,3 @@ const rawCountries = [
600
600
  ['Zambia', ['africa'], 'zm', '260'],
601
601
  ['Zimbabwe', ['africa'], 'zw', '263']
602
602
  ];
603
- export default rawCountries;
@@ -0,0 +1,3 @@
1
+ export { normalizedCountries } from './allCountry';
2
+ export { telTypes } from './telTypes';
3
+ export { rawRegions } from './regions';
@@ -0,0 +1,3 @@
1
+ export { normalizedCountries } from './allCountry';
2
+ export { telTypes } from './telTypes';
3
+ export { rawRegions } from './regions';
@@ -1,2 +1 @@
1
- declare const rawTerritories: (string | number | string[])[][];
2
- export default rawTerritories;
1
+ export declare const rawRegions: (string | number | string[])[][];
package/assets/regions.js CHANGED
@@ -15,7 +15,7 @@
15
15
  // Sub-regions:
16
16
  // ['north-america', 'south-america', 'central-america', 'carribean',
17
17
  // 'eu-union', 'ex-ussr', 'ex-yugos', 'baltic', 'middle-east', 'north-africa']
18
- const rawTerritories = [
18
+ export const rawRegions = [
19
19
  ['American Samoa', ['oceania'], 'as', '1684'],
20
20
  ['Anguilla', ['america', 'carribean'], 'ai', '1264'],
21
21
  ['Bermuda', ['america', 'north-america'], 'bm', '1441'],
@@ -41,4 +41,3 @@ const rawTerritories = [
41
41
  ['U.S. Virgin Islands', ['america', 'carribean'], 'vi', '1340'],
42
42
  ['Wallis and Futuna', ['oceania'], 'wf', '681']
43
43
  ];
44
- export default rawTerritories;
@@ -0,0 +1,5 @@
1
+ export declare const telTypes: {
2
+ id: string;
3
+ value: string;
4
+ label: string;
5
+ }[];
@@ -0,0 +1,16 @@
1
+ import { capitalize } from '../utils/helpers';
2
+ export const telTypes = [
3
+ 'PREMIUM_RATE',
4
+ 'TOLL_FREE',
5
+ 'SHARED_COST',
6
+ 'VOIP',
7
+ 'PERSONAL_NUMBER',
8
+ 'PAGER',
9
+ 'UAN',
10
+ 'VOICEMAIL',
11
+ 'FIXED_LINE_OR_MOBILE',
12
+ 'FIXED_LINE',
13
+ 'MOBILE'
14
+ ].map((el) => {
15
+ return { id: el, value: el, label: capitalize(el.split('_').join(' ')) };
16
+ });
@@ -1,23 +1,32 @@
1
1
  <script>import { enteredTelInputStore } from '../../stores';
2
+ import { PhoneNumberParseError } from '../../types';
2
3
  import { parsePhoneNumberWithError, ParseError } from 'libphonenumber-js';
3
- export let enteredTelInput;
4
+ export let defaultCountry = null;
5
+ export let phoneInput = null;
6
+ export let parsedPhoneInput = null;
7
+ export let error = null;
8
+ export let disabled = false;
9
+ export let id = null;
10
+ export let name = null;
4
11
  const handleInput = (event) => {
5
12
  const inputVal = event.target.value;
6
- enteredTelInput = inputVal;
13
+ phoneInput = inputVal;
7
14
  $enteredTelInputStore = inputVal;
8
15
  try {
9
- const phoneNumber = parsePhoneNumberWithError(inputVal);
16
+ parsedPhoneInput = parsePhoneNumberWithError(inputVal, defaultCountry || undefined);
17
+ error = null;
10
18
  }
11
- catch (error) {
12
- if (error instanceof ParseError) {
19
+ catch (err) {
20
+ if (err instanceof ParseError) {
13
21
  // Not a phone number, non-existent country, etc.
14
- console.log(error.message);
22
+ parsedPhoneInput = null;
23
+ error = PhoneNumberParseError[err.message];
15
24
  }
16
25
  else {
17
- throw error;
26
+ throw err;
18
27
  }
19
28
  }
20
29
  };
21
30
  </script>
22
31
 
23
- <input class={$$props.class} type="tel" on:input={handleInput} />
32
+ <input {id} {name} class={$$props.class} {disabled} type="tel" on:input={handleInput} />
@@ -1,8 +1,16 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
+ import { PhoneNumberParseError } from '../../types';
3
+ import { type CountryCode, type PhoneNumber } from 'libphonenumber-js';
2
4
  declare const __propDef: {
3
5
  props: {
4
6
  [x: string]: any;
5
- enteredTelInput: string;
7
+ defaultCountry?: CountryCode | null | undefined;
8
+ phoneInput?: string | null | undefined;
9
+ parsedPhoneInput?: PhoneNumber | null | undefined;
10
+ error?: PhoneNumberParseError | null | undefined;
11
+ disabled?: boolean | undefined;
12
+ id?: string | null | undefined;
13
+ name?: string | null | undefined;
6
14
  };
7
15
  events: {
8
16
  [evt: string]: CustomEvent<any>;
@@ -4,6 +4,7 @@ export let selectOptions = {
4
4
  flags: true
5
5
  };
6
6
  export let items;
7
+ export let clickOutside = true;
7
8
  let isOpen = false;
8
9
  let enteredSearch;
9
10
  const toggleSelect = (e) => {
@@ -14,9 +15,15 @@ const closeSelect = (e) => {
14
15
  e?.preventDefault();
15
16
  isOpen = false;
16
17
  };
18
+ const handleClickOutside = (e) => {
19
+ e?.preventDefault();
20
+ if (clickOutside) {
21
+ closeSelect(e);
22
+ }
23
+ };
17
24
  </script>
18
25
 
19
- <div class="select cursor-pointer" use:clickOutsideAction={closeSelect}>
26
+ <div class="select cursor-pointer" use:clickOutsideAction={handleClickOutside}>
20
27
  <div on:click={() => toggleSelect()}>CHOOSE</div>
21
28
  {#if isOpen}
22
29
  <ul class="border border-gray-900 max-h-40 w-fit overflow-y-scroll">
@@ -37,5 +44,3 @@ const closeSelect = (e) => {
37
44
  </ul>
38
45
  {/if}
39
46
  </div>
40
-
41
- <style></style>
@@ -1,5 +1,5 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
- import type { Country } from '../../models/interfaces/Country.interface';
2
+ import type { Country } from '../../types/interfaces/Country.interface';
3
3
  declare const __propDef: {
4
4
  props: {
5
5
  selectOptions?: {
@@ -7,6 +7,7 @@ declare const __propDef: {
7
7
  flags: boolean;
8
8
  } | undefined;
9
9
  items: Country[];
10
+ clickOutside?: boolean | undefined;
10
11
  };
11
12
  events: {
12
13
  [evt: string]: CustomEvent<any>;
@@ -2,7 +2,7 @@ import { SvelteComponentTyped } from "svelte";
2
2
  declare const __propDef: {
3
3
  props: {
4
4
  [x: string]: any;
5
- selectedCountry: string;
5
+ selectedCountry: string | null;
6
6
  };
7
7
  events: {
8
8
  [evt: string]: CustomEvent<any>;
@@ -1,9 +1,9 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
- import type { SelectObject } from '../../../models/interfaces/Select.interface';
2
+ import type { TelSelectObject } from '../../../types/interfaces/Select.interface';
3
3
  declare const __propDef: {
4
4
  props: {
5
5
  [x: string]: any;
6
- regionOption: SelectObject;
6
+ regionOption: TelSelectObject;
7
7
  };
8
8
  events: {
9
9
  [evt: string]: CustomEvent<any>;
@@ -1,5 +1,5 @@
1
1
  <script>import { selectedTelTypeStore } from '../../../stores';
2
- export let selectedTelType;
2
+ export let selectedTelType = null;
3
3
  const setSelectedCountry = (value) => {
4
4
  selectedTelType = value;
5
5
  $selectedTelTypeStore = value;
@@ -1,9 +1,9 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
- import type { SelectType } from '../../../models/types/Select.type';
2
+ import type { SelectType } from '../../../types/Select.type';
3
3
  declare const __propDef: {
4
4
  props: {
5
5
  [x: string]: any;
6
- selectedTelType: SelectType;
6
+ selectedTelType?: SelectType | null | undefined;
7
7
  };
8
8
  events: {
9
9
  [evt: string]: CustomEvent<any>;
@@ -1,9 +1,9 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
- import type { SelectObject } from '../../../models/interfaces/Select.interface';
2
+ import type { TelSelectObject } from '../../../types/interfaces/Select.interface';
3
3
  declare const __propDef: {
4
4
  props: {
5
5
  [x: string]: any;
6
- typeOption: SelectObject;
6
+ typeOption: TelSelectObject;
7
7
  };
8
8
  events: {
9
9
  [evt: string]: CustomEvent<any>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "svelte-tel-input",
3
3
  "description": "svelte-tel-input",
4
- "version": "0.6.3",
4
+ "version": "0.7.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/gyurielf/svelte-tel-input.git"
@@ -21,27 +21,27 @@
21
21
  "node": ">= 16"
22
22
  },
23
23
  "dependencies": {
24
- "libphonenumber-js": "^1.10.7"
24
+ "libphonenumber-js": "^1.10.9"
25
25
  },
26
26
  "devDependencies": {
27
- "@babel/core": "^7.18.6",
28
- "@babel/preset-env": "^7.18.6",
29
- "@changesets/cli": "^2.23.1",
27
+ "@babel/core": "^7.18.9",
28
+ "@babel/preset-env": "^7.18.9",
29
+ "@changesets/cli": "^2.24.0",
30
30
  "@changesets/get-github-info": "^0.5.1",
31
- "@changesets/types": "^5.0.0",
32
- "@sveltejs/adapter-static": "^1.0.0-next.35",
33
- "@sveltejs/kit": "^1.0.0-next.367",
31
+ "@changesets/types": "^5.1.0",
32
+ "@sveltejs/adapter-static": "^1.0.0-next.38",
33
+ "@sveltejs/kit": "^1.0.0-next.388",
34
34
  "@testing-library/jest-dom": "^5.16.4",
35
35
  "@testing-library/svelte": "^3.1.3",
36
36
  "@types/jest": "^28.1.4",
37
- "@typescript-eslint/eslint-plugin": "^5.30.5",
38
- "@typescript-eslint/parser": "^5.30.5",
37
+ "@typescript-eslint/eslint-plugin": "^5.30.7",
38
+ "@typescript-eslint/parser": "^5.30.7",
39
39
  "autoprefixer": "^10.4.7",
40
40
  "babel-jest": "^28.1.2",
41
41
  "babel-loader": "^8.2.5",
42
42
  "cssnano": "^5.1.12",
43
43
  "dotenv": "^16.0.1",
44
- "eslint": "^8.19.0",
44
+ "eslint": "^8.20.0",
45
45
  "eslint-config-prettier": "^8.5.0",
46
46
  "eslint-plugin-jest": "^26.5.3",
47
47
  "eslint-plugin-svelte3": "^4.0.0",
@@ -59,8 +59,8 @@
59
59
  "svelte-jester": "^2.3.2",
60
60
  "svelte-loader": "^3.1.3",
61
61
  "svelte-preprocess": "^4.10.7",
62
- "svelte2tsx": "^0.5.11",
63
- "tailwindcss": "^3.1.5",
62
+ "svelte2tsx": "^0.5.12",
63
+ "tailwindcss": "^3.1.6",
64
64
  "ts-jest": "^28.0.5",
65
65
  "tsconfig-paths-webpack-plugin": "^3.5.2",
66
66
  "tslib": "^2.4.0",
@@ -139,8 +139,10 @@
139
139
  "exports": {
140
140
  "./package.json": "./package.json",
141
141
  "./assets/allCountry": "./assets/allCountry.js",
142
- "./assets/countries": "./assets/countries.js",
142
+ "./assets/countries_deprecated": "./assets/countries_deprecated.js",
143
+ "./assets": "./assets/index.js",
143
144
  "./assets/regions": "./assets/regions.js",
145
+ "./assets/telTypes": "./assets/telTypes.js",
144
146
  "./components/Input/TelInput.svelte": "./components/Input/TelInput.svelte",
145
147
  "./components/Select/Select.svelte": "./components/Select/Select.svelte",
146
148
  "./components/Select/SelectListItem.svelte": "./components/Select/SelectListItem.svelte",
@@ -150,18 +152,22 @@
150
152
  "./components/Select/TelTypeSelect/TelTypeSelect.svelte": "./components/Select/TelTypeSelect/TelTypeSelect.svelte",
151
153
  "./components/Select/TelTypeSelect/TelTypeSelectOption.svelte": "./components/Select/TelTypeSelect/TelTypeSelectOption.svelte",
152
154
  ".": "./index.js",
153
- "./models/enums/PhoneType.enum": "./models/enums/PhoneType.enum.js",
154
- "./models/enums": "./models/enums/index.js",
155
- "./models": "./models/index.js",
156
- "./models/interfaces/Country.interface": "./models/interfaces/Country.interface.js",
157
- "./models/interfaces/Select.interface": "./models/interfaces/Select.interface.js",
158
- "./models/types/DynamicSvelteComponent.type": "./models/types/DynamicSvelteComponent.type.js",
159
- "./models/types/Select.type": "./models/types/Select.type.js",
160
155
  "./stores": "./stores/index.js",
156
+ "./types/DynamicSvelteComponent.type": "./types/DynamicSvelteComponent.type.js",
157
+ "./types/Select.type": "./types/Select.type.js",
158
+ "./types/enums/PhoneNumberParseError.enum": "./types/enums/PhoneNumberParseError.enum.js",
159
+ "./types/enums/PhoneType.enum": "./types/enums/PhoneType.enum.js",
160
+ "./types/enums": "./types/enums/index.js",
161
+ "./types": "./types/index.js",
162
+ "./types/interfaces/Country.interface": "./types/interfaces/Country.interface.js",
163
+ "./types/interfaces/Phone.interface": "./types/interfaces/Phone.interface.js",
164
+ "./types/interfaces/Select.interface": "./types/interfaces/Select.interface.js",
165
+ "./types/interfaces": "./types/interfaces/index.js",
161
166
  "./utils/api": "./utils/api.js",
162
167
  "./utils/directives/clickOutsideAction": "./utils/directives/clickOutsideAction.js",
163
168
  "./utils/helpers": "./utils/helpers.js",
164
169
  "./utils/simulator": "./utils/simulator.js",
165
- "./utils/typeCheck": "./utils/typeCheck.js"
170
+ "./utils/typeCheck": "./utils/typeCheck.js",
171
+ "./views/Usage.svelte": "./views/Usage.svelte"
166
172
  }
167
173
  }
package/stores/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Writable } from 'svelte/store';
2
- import type { SelectType } from '../models/types/Select.type';
2
+ import type { SelectType } from '../types';
3
3
  export declare const booleanStore: (initial: boolean) => {
4
4
  isOpen: Writable<boolean>;
5
5
  open: () => void;
@@ -0,0 +1,2 @@
1
+ import type { TelSelectObject } from './interfaces/Select.interface';
2
+ export declare type SelectType = TelSelectObject | (string | number);
File without changes
@@ -0,0 +1,6 @@
1
+ export declare enum PhoneNumberParseError {
2
+ NOT_A_NUMBER = "NOT_A_NUMBER",
3
+ INVALID_COUNTRY = "INVALID_COUNTRY",
4
+ TOO_SHORT = "TOO_SHORT",
5
+ TOO_LONG = "TOO_LONG"
6
+ }
@@ -0,0 +1,7 @@
1
+ export var PhoneNumberParseError;
2
+ (function (PhoneNumberParseError) {
3
+ PhoneNumberParseError["NOT_A_NUMBER"] = "NOT_A_NUMBER";
4
+ PhoneNumberParseError["INVALID_COUNTRY"] = "INVALID_COUNTRY";
5
+ PhoneNumberParseError["TOO_SHORT"] = "TOO_SHORT";
6
+ PhoneNumberParseError["TOO_LONG"] = "TOO_LONG";
7
+ })(PhoneNumberParseError || (PhoneNumberParseError = {}));
File without changes
File without changes
@@ -0,0 +1,2 @@
1
+ export { PhoneType } from './PhoneType.enum';
2
+ export { PhoneNumberParseError } from './PhoneNumberParseError.enum';
@@ -0,0 +1,2 @@
1
+ export { PhoneType } from './PhoneType.enum';
2
+ export { PhoneNumberParseError } from './PhoneNumberParseError.enum';
@@ -0,0 +1,4 @@
1
+ export * from './enums';
2
+ export * from './interfaces';
3
+ export type { DynamicSvelteComponent } from './DynamicSvelteComponent.type';
4
+ export type { SelectType } from './Select.type';
package/types/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './enums';
2
+ export * from './interfaces';
@@ -1,6 +1,7 @@
1
+ import type { CountryCode } from 'libphonenumber-js';
1
2
  export interface Country {
2
3
  name: string;
3
- iso2: string | number | string[];
4
+ iso2: CountryCode;
4
5
  dialCode: string | number | string[];
5
6
  priority: string | number | string[];
6
7
  areaCodes: string | number | string[] | null;
@@ -0,0 +1,5 @@
1
+ import type { PhoneNumberParseError } from '../enums';
2
+ export interface PhoneNumberError {
3
+ isValid: false;
4
+ error: PhoneNumberParseError;
5
+ }
@@ -0,0 +1,5 @@
1
+ export interface TelSelectObject {
2
+ id: string;
3
+ label: string;
4
+ value: string | number;
5
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export type { Country } from './Country.interface';
2
+ export type { TelSelectObject } from './Select.interface';
3
+ export type { PhoneNumberError } from './Phone.interface';
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,6 @@
1
+ import type { PhoneNumber } from 'libphonenumber-js';
1
2
  export declare const capitalize: (str: string) => string;
2
3
  export declare const getCurrentCountry: () => Promise<string>;
4
+ export declare const normalizePhoneInput: (input: PhoneNumber | null) => {
5
+ [k: string]: string | boolean | import("libphonenumber-js").E164Number | import("libphonenumber-js").CountryCallingCode | import("libphonenumber-js").NationalNumber | null | undefined;
6
+ };
package/utils/helpers.js CHANGED
@@ -2,10 +2,31 @@ export const capitalize = (str) => {
2
2
  return (str && str[0].toUpperCase() + str.slice(1).toLowerCase()) || '';
3
3
  };
4
4
  export const getCurrentCountry = async () => {
5
- const response = await (await fetch('https://ip2c.org/s')).text();
6
- const result = (response || '').toString();
7
- if (!result || result[0] !== '1') {
5
+ try {
6
+ const response = await (await fetch('https://ip2c.org/s')).text();
7
+ const result = (response || '').toString();
8
+ if (!result || result[0] !== '1') {
9
+ throw new Error('Unable to fetch the country');
10
+ }
11
+ return result.substring(2, 4);
12
+ }
13
+ catch (error) {
8
14
  throw new Error('Unable to fetch the country');
9
15
  }
10
- return result.substring(2, 4);
16
+ };
17
+ export const normalizePhoneInput = (input) => {
18
+ const resultObject = {
19
+ countryCode: input ? input.country : null,
20
+ isValid: input ? input.isValid() : false,
21
+ phoneNumber: input ? input.number : null,
22
+ countryCallingCode: input ? input.countryCallingCode : null,
23
+ formattedNumber: input ? input.formatInternational() : null,
24
+ nationalNumber: input ? input.nationalNumber : null,
25
+ formatInternational: input ? input.formatInternational() : null,
26
+ formatNational: input ? input.formatNational() : null,
27
+ uri: input ? input.getURI() : null,
28
+ e164: input ? input.number : null
29
+ };
30
+ const filteredResult = Object.fromEntries(Object.entries(resultObject).filter(([key, value]) => value !== null));
31
+ return filteredResult;
11
32
  };
@@ -1,4 +1,4 @@
1
- import type { DynamicSvelteComponent } from '../models/types/DynamicSvelteComponent.type';
1
+ import type { DynamicSvelteComponent } from '../types/DynamicSvelteComponent.type';
2
2
  export declare const isDynamicComponent: (item: unknown) => item is DynamicSvelteComponent;
3
3
  export declare const isStringArray: (items: unknown) => items is string[];
4
4
  export declare const isDynamicComponentArray: (items: unknown) => items is DynamicSvelteComponent[];
@@ -0,0 +1,55 @@
1
+ <script>import { onMount } from 'svelte';
2
+ import CountrySelect from '../components/Select/TelCountrySelect/TelCountrySelect.svelte';
3
+ import TelTypeSelect from '../components/Select/TelTypeSelect/TelTypeSelect.svelte';
4
+ import TelTypeSelectOption from '../components/Select/TelTypeSelect/TelTypeSelectOption.svelte';
5
+ import TelInput from '../components/Input/TelInput.svelte';
6
+ import { getCountries } from 'libphonenumber-js';
7
+ import { getCurrentCountry } from '../utils/helpers';
8
+ // Assets
9
+ import { telTypes } from '../assets/telTypes';
10
+ // Tel input
11
+ let phoneInput;
12
+ // Countries
13
+ const countries = getCountries();
14
+ let selectedCountry = null;
15
+ // Validity of inputs
16
+ let dataIsValid = {
17
+ enteredTelInput: true
18
+ };
19
+ // TODO >> sort and order option for telTypes.
20
+ let selectedTelType = null;
21
+ export let data;
22
+ onMount(async () => {
23
+ // Get current country on initialization
24
+ const currentCountry = await getCurrentCountry();
25
+ if (currentCountry.length === 2)
26
+ selectedCountry = currentCountry;
27
+ });
28
+ </script>
29
+
30
+ <!-- Whis way you can build your own country select component. You can both style and add your own logic. -->
31
+ <CountrySelect bind:selectedCountry class="text-gray-800 rounded-sm py-2">
32
+ <svelte:fragment slot="options">
33
+ <option value="" class="my-2" selected={true} hidden={true}>Choose here</option>
34
+ {#each countries as country (country)}
35
+ <option class="text-red-500" value={country}>{country}</option>
36
+ {/each}
37
+ </svelte:fragment>
38
+ </CountrySelect>
39
+
40
+ <TelTypeSelect bind:selectedTelType class="text-gray-800 rounded-sm">
41
+ <svelte:fragment slot="options">
42
+ <option value="" selected={true} hidden={true}>Choose here</option>
43
+ {#each telTypes as telType (telType.id)}
44
+ <TelTypeSelectOption class="text-red-500" typeOption={telType} />
45
+ {/each}
46
+ </svelte:fragment>
47
+ </TelTypeSelect>
48
+
49
+ <TelInput
50
+ bind:parsedPhoneInput={data}
51
+ bind:phoneInput
52
+ class="px-4 py-1 text-gray-900 focus:outline-none rounded-sm {dataIsValid.enteredTelInput
53
+ ? ' border-none border-0'
54
+ : 'border-2 border-red-600'}"
55
+ />
@@ -0,0 +1,17 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ import { type PhoneNumber } from 'libphonenumber-js';
3
+ declare const __propDef: {
4
+ props: {
5
+ data: PhoneNumber;
6
+ };
7
+ events: {
8
+ [evt: string]: CustomEvent<any>;
9
+ };
10
+ slots: {};
11
+ };
12
+ export declare type UsageProps = typeof __propDef.props;
13
+ export declare type UsageEvents = typeof __propDef.events;
14
+ export declare type UsageSlots = typeof __propDef.slots;
15
+ export default class Usage extends SvelteComponentTyped<UsageProps, UsageEvents, UsageSlots> {
16
+ }
17
+ export {};
@@ -1,2 +0,0 @@
1
- declare const rawCountries: (string | number | string[])[][];
2
- export default rawCountries;
@@ -1 +0,0 @@
1
- export * from './PhoneType.enum';
@@ -1 +0,0 @@
1
- export * from './PhoneType.enum';
package/models/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from './enums';
package/models/index.js DELETED
@@ -1 +0,0 @@
1
- export * from './enums';
@@ -1,5 +0,0 @@
1
- export interface SelectObject {
2
- id?: string | number;
3
- label: string;
4
- value: string | number;
5
- }
@@ -1,2 +0,0 @@
1
- import type { SelectObject } from '../interfaces/Select.interface';
2
- export declare type SelectType = SelectObject | (string | number);