frutjam 0.0.1 → 0.0.2

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.
@@ -0,0 +1,117 @@
1
+ import allCountries from "./data";
2
+ import { CountryInfo } from "./types";
3
+ import { TelephoneInputSettings } from "./interfaces";
4
+
5
+ function countryCodeToEmoji(countryCode: string): string {
6
+ const codePoints = countryCode
7
+ .toUpperCase()
8
+ .split('')
9
+ .map((char) => 127397 + char.charCodeAt(0));
10
+ return String.fromCodePoint(...codePoints);
11
+ }
12
+
13
+ export default class TelephoneInput {
14
+ private inputElement: HTMLInputElement | null = null;
15
+ private selectElement: HTMLSelectElement;
16
+ private countries: Record<string, CountryInfo> = {};
17
+ private showFlag: boolean;
18
+
19
+ constructor(elementId: string, settings: TelephoneInputSettings) {
20
+ this.inputElement = document.getElementById(elementId) as HTMLInputElement | null;
21
+ this.selectElement = document.createElement('select');
22
+ this.showFlag = settings.showFlag ?? false; // Default to false if not provided
23
+ this.init();
24
+
25
+ if (this.inputElement) {
26
+ const countryCodes = Array.isArray(settings.countries)
27
+ ? settings.countries.map((country) => (typeof country === 'string' ? country : country.code))
28
+ : Object.keys(allCountries);
29
+
30
+ this.convertCountries(countryCodes).then((convertedCountries) => {
31
+ this.countries = convertedCountries;
32
+ this.createCountryDropdown(); // Call createCountryDropdown after countries are loaded
33
+ });
34
+ } else {
35
+ console.error(`Element with ID '${elementId}' not found.`);
36
+ }
37
+ }
38
+
39
+ // Other methods remain unchanged
40
+
41
+ private async convertCountries(countries: string[]): Promise<Record<string, CountryInfo>> {
42
+ const countryObjects = await Promise.all(
43
+ countries.map(async (code) => {
44
+ return allCountries[code] ? {
45
+ code,
46
+ name: allCountries[code].name,
47
+ flag: this.showFlag ? countryCodeToEmoji(allCountries[code].code) : '', // Conditionally include emoji flag
48
+ exampleNumber: allCountries[code].exampleNumber
49
+ } : null;
50
+ })
51
+ );
52
+ return countryObjects.filter((country) => country !== null).reduce((acc, country) => {
53
+ if (country) acc[country.code] = country;
54
+ return acc;
55
+ }, {} as Record<string, CountryInfo>);
56
+ }
57
+ // Other methods like init, createCountryDropdown, updatePlaceholder remain unchanged
58
+
59
+ private init() {
60
+ if (this.inputElement) {
61
+ // Check if the input element is already inside a div with data-fj="telephoneInput"
62
+ if (!this.inputElement.closest('[data-fj="telephoneInput"]')) {
63
+ // Create a div with data-fj="telephoneInput" attribute
64
+ const telephoneInputDiv = document.createElement('div');
65
+ telephoneInputDiv.setAttribute('data-fj', 'telephoneInput');
66
+
67
+ // Append the input element to the div
68
+ const parent = this.inputElement.parentNode;
69
+ if (parent) {
70
+ parent.insertBefore(telephoneInputDiv, this.inputElement);
71
+ telephoneInputDiv.appendChild(this.inputElement);
72
+ } else {
73
+ console.error('Input element has no parent node.');
74
+ }
75
+ } else {
76
+ console.error('Input element is already inside a div with data-fj="telephoneInput".');
77
+ }
78
+ } else {
79
+ console.error('Input element is not initialized.');
80
+ }
81
+ }
82
+
83
+
84
+ private createCountryDropdown() {
85
+ // Clear the existing content of selectElement
86
+ this.selectElement.innerHTML = '';
87
+
88
+ // Populate selectElement with options
89
+ Object.values(this.countries).forEach((country) => {
90
+ const option = document.createElement('option');
91
+ option.value = country.code;
92
+ const emojiFlag = countryCodeToEmoji(country.code);
93
+ option.text = this.showFlag ? `${emojiFlag} ${country.name} (${country.code})` : `${country.name} (${country.code})`;
94
+
95
+ // Append the option to the selectElement
96
+ this.selectElement.appendChild(option);
97
+ });
98
+
99
+ // Check if selectElement is not already part of the DOM
100
+ if (!this.selectElement.parentNode && this.inputElement && this.inputElement.parentNode) {
101
+ // Prepend selectElement before the input element
102
+ this.inputElement.parentNode.insertBefore(this.selectElement, this.inputElement);
103
+ }
104
+ }
105
+
106
+
107
+
108
+ private updatePlaceholder() {
109
+ const selectedCountryCode = this.selectElement.value;
110
+ const selectedCountry = this.countries[selectedCountryCode];
111
+ if (selectedCountry) {
112
+ this.inputElement?.setAttribute('placeholder', `E.g., ${selectedCountry.exampleNumber}`);
113
+ } else {
114
+ this.inputElement?.removeAttribute('placeholder');
115
+ }
116
+ }
117
+ }
@@ -0,0 +1,6 @@
1
+ import { CountryInfo } from "./types";
2
+ // Updated interface for TelephoneInputSettings to accept CountryInfo[]
3
+ export interface TelephoneInputSettings {
4
+ countries?: CountryInfo[] | string[];
5
+ showFlag?:boolean;
6
+ }
@@ -0,0 +1,7 @@
1
+ type CountryInfo = {
2
+ code: string;
3
+ name: string;
4
+ exampleNumber: string; // Example phone number
5
+ };
6
+
7
+ export type { CountryInfo }
package/README.md DELETED
@@ -1 +0,0 @@
1
- # Welcome to Frutjam