ngxsmk-tel-input 1.2.2 → 1.2.4
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/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/ng-package.json +16 -0
- package/package.json +17 -53
- package/src/lib/ngxsmk-tel-input.component.scss +220 -0
- package/src/lib/ngxsmk-tel-input.component.spec.ts +24 -0
- package/src/lib/ngxsmk-tel-input.component.ts +457 -0
- package/src/lib/ngxsmk-tel-input.service.spec.ts +15 -0
- package/src/lib/ngxsmk-tel-input.service.ts +17 -0
- package/src/lib/types.ts +11 -0
- package/src/public-api.ts +3 -0
- package/tsconfig.lib.json +13 -0
- package/tsconfig.lib.prod.json +9 -0
- package/tsconfig.spec.json +13 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Changesets
|
|
2
|
+
|
|
3
|
+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
|
|
4
|
+
with multi-package repos, or single-package repos to help you version and publish your code. You can
|
|
5
|
+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
|
|
6
|
+
|
|
7
|
+
We have a quick list of common questions to get you started engaging with this project in
|
|
8
|
+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://unpkg.com/@changesets/config@3.1.1/schema.json",
|
|
3
|
+
"changelog": "@changesets/cli/changelog",
|
|
4
|
+
"commit": false,
|
|
5
|
+
"fixed": [],
|
|
6
|
+
"linked": [],
|
|
7
|
+
"access": "restricted",
|
|
8
|
+
"baseBranch": "main",
|
|
9
|
+
"updateInternalDependencies": "patch",
|
|
10
|
+
"ignore": []
|
|
11
|
+
}
|
package/ng-package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
|
|
3
|
+
"dest": "../../dist/ngxsmk-tel-input",
|
|
4
|
+
"lib": {
|
|
5
|
+
"entryFile": "src/public-api.ts"
|
|
6
|
+
},
|
|
7
|
+
"allowedNonPeerDependencies": [
|
|
8
|
+
"intl-tel-input",
|
|
9
|
+
"libphonenumber-js"
|
|
10
|
+
],
|
|
11
|
+
"assets": [
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE",
|
|
14
|
+
"docs"
|
|
15
|
+
]
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,94 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ngxsmk-tel-input",
|
|
3
|
-
"version": "1.2.
|
|
4
|
-
"description": "Angular international telephone input
|
|
3
|
+
"version": "1.2.4",
|
|
4
|
+
"description": "Angular international telephone input (intl-tel-input UI + libphonenumber-js validation). ControlValueAccessor. SSR-safe.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Sachin Dilshan",
|
|
8
|
+
"email": "sachindilshan040@gmail.com",
|
|
9
|
+
"url": "https://github.com/toozuuu"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/toozuuu/ngxsmk-tel-input#readme",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/toozuuu/ngxsmk-tel-input.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/toozuuu/ngxsmk-tel-input/issues"
|
|
18
|
+
},
|
|
5
19
|
"keywords": [
|
|
6
20
|
"ngxsmk-tel-input",
|
|
7
|
-
"ngx",
|
|
8
21
|
"angular",
|
|
9
|
-
"angular17",
|
|
10
|
-
"angular18",
|
|
11
|
-
"angular19",
|
|
12
22
|
"angular-forms",
|
|
13
|
-
"reactive-forms",
|
|
14
|
-
"template-driven-forms",
|
|
15
23
|
"controlvalueaccessor",
|
|
16
24
|
"cva",
|
|
17
25
|
"phone-input",
|
|
18
26
|
"telephone-input",
|
|
19
|
-
"phone-number",
|
|
20
27
|
"international-phone",
|
|
21
28
|
"intl-tel-input",
|
|
22
|
-
"libphonenumber",
|
|
23
29
|
"libphonenumber-js",
|
|
24
30
|
"e164",
|
|
25
|
-
"phone-validation",
|
|
26
|
-
"phone-number-validation",
|
|
27
31
|
"country-code",
|
|
28
32
|
"dial-code",
|
|
29
33
|
"separate-dial-code",
|
|
30
34
|
"national-mode",
|
|
31
35
|
"country-flags",
|
|
32
|
-
"dropdown",
|
|
33
36
|
"i18n",
|
|
34
37
|
"localization",
|
|
35
38
|
"rtl",
|
|
36
39
|
"a11y",
|
|
37
|
-
"accessibility",
|
|
38
40
|
"ssr",
|
|
39
|
-
"angular-universal",
|
|
40
41
|
"typescript"
|
|
41
42
|
],
|
|
42
|
-
"homepage": "https://github.com/toozuuu/ngxsmk-tel-input#readme",
|
|
43
|
-
"repository": {
|
|
44
|
-
"type": "git",
|
|
45
|
-
"url": "git+https://github.com/toozuuu/ngxsmk-tel-input.git"
|
|
46
|
-
},
|
|
47
|
-
"bugs": {
|
|
48
|
-
"url": "https://github.com/toozuuu/ngxsmk-tel-input/issues"
|
|
49
|
-
},
|
|
50
|
-
"author": {
|
|
51
|
-
"name": "Sachin Dilshan",
|
|
52
|
-
"email": "sachindilshan040@gmail.com",
|
|
53
|
-
"url": "https://www.linkedin.com/in/sachindilshan"
|
|
54
|
-
},
|
|
55
|
-
"license": "MIT",
|
|
56
|
-
"private": false,
|
|
57
43
|
"sideEffects": false,
|
|
58
44
|
"publishConfig": {
|
|
59
45
|
"access": "public"
|
|
60
46
|
},
|
|
61
|
-
"exports": {
|
|
62
|
-
"./*": null
|
|
63
|
-
},
|
|
64
|
-
"files": [
|
|
65
|
-
"bundles/",
|
|
66
|
-
"fesm*",
|
|
67
|
-
"esm*",
|
|
68
|
-
"schematics/",
|
|
69
|
-
"migrations/",
|
|
70
|
-
"README.md",
|
|
71
|
-
"LICENSE",
|
|
72
|
-
"docs/"
|
|
73
|
-
],
|
|
74
47
|
"peerDependencies": {
|
|
75
48
|
"@angular/common": ">=17 <21",
|
|
76
49
|
"@angular/core": ">=17 <21",
|
|
77
50
|
"@angular/forms": ">=17 <21",
|
|
78
51
|
"rxjs": ">=7.8.0",
|
|
79
52
|
"intl-tel-input": "^25.3.2",
|
|
80
|
-
"libphonenumber-js": "^1.12.10"
|
|
81
|
-
},
|
|
82
|
-
"peerDependenciesMeta": {
|
|
83
|
-
"intl-tel-input": { "optional": false },
|
|
84
|
-
"libphonenumber-js": { "optional": false }
|
|
53
|
+
"libphonenumber-js": "^1.12.10 || ^1.12.11"
|
|
85
54
|
},
|
|
86
55
|
"dependencies": {
|
|
87
56
|
"tslib": "^2.3.0"
|
|
88
|
-
},
|
|
89
|
-
"devDependencies": {
|
|
90
|
-
"intl-tel-input": "^25.3.2",
|
|
91
|
-
"libphonenumber-js": "^1.12.10",
|
|
92
|
-
"@changesets/cli": "^2.29.5"
|
|
93
57
|
}
|
|
94
58
|
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/* ---------- Theme tokens ---------- */
|
|
2
|
+
:host {
|
|
3
|
+
--tel-bg: #fff;
|
|
4
|
+
--tel-fg: #0f172a;
|
|
5
|
+
--tel-border: #c0c0c0;
|
|
6
|
+
--tel-border-hover: #9aa0a6;
|
|
7
|
+
--tel-ring: #2563eb;
|
|
8
|
+
--tel-placeholder: #9ca3af;
|
|
9
|
+
--tel-error: #ef4444;
|
|
10
|
+
--tel-radius: 12px;
|
|
11
|
+
--tel-focus-shadow: 0 0 0 3px rgba(37, 99, 235, .25);
|
|
12
|
+
|
|
13
|
+
--tel-dd-bg: var(--tel-bg);
|
|
14
|
+
--tel-dd-border: var(--tel-border);
|
|
15
|
+
--tel-dd-shadow: 0 24px 60px rgba(0, 0, 0, .18);
|
|
16
|
+
--tel-dd-radius: 12px;
|
|
17
|
+
--tel-dd-item-hover: rgba(37, 99, 235, .08);
|
|
18
|
+
--tel-dd-z: 2000;
|
|
19
|
+
--tel-dd-search-bg: rgba(148, 163, 184, .08);
|
|
20
|
+
|
|
21
|
+
display: block;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
:host-context(.dark) {
|
|
25
|
+
--tel-bg: #0b0f17;
|
|
26
|
+
--tel-fg: #e5e7eb;
|
|
27
|
+
--tel-border: #334155;
|
|
28
|
+
--tel-border-hover: #475569;
|
|
29
|
+
--tel-ring: #60a5fa;
|
|
30
|
+
--tel-placeholder: #94a3b8;
|
|
31
|
+
|
|
32
|
+
--tel-dd-bg: #0f1521;
|
|
33
|
+
--tel-dd-border: #324056;
|
|
34
|
+
--tel-dd-search-bg: rgba(148, 163, 184, .12);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/* ---------- Structure ---------- */
|
|
38
|
+
.ngxsmk-tel {
|
|
39
|
+
width: 100%;
|
|
40
|
+
color: var(--tel-fg);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.ngxsmk-tel.disabled {
|
|
44
|
+
opacity: .7;
|
|
45
|
+
cursor: not-allowed;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.ngxsmk-tel__label {
|
|
49
|
+
display: inline-block;
|
|
50
|
+
margin-bottom: 6px;
|
|
51
|
+
font-size: .875rem;
|
|
52
|
+
font-weight: 500;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.ngxsmk-tel__wrap {
|
|
56
|
+
position: relative;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.ngxsmk-tel-input__wrapper,
|
|
60
|
+
:host ::ng-deep .iti {
|
|
61
|
+
width: 100%;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.ngxsmk-tel-input__control {
|
|
65
|
+
width: 100%;
|
|
66
|
+
height: 40px;
|
|
67
|
+
font: inherit;
|
|
68
|
+
color: var(--tel-fg);
|
|
69
|
+
background: var(--tel-bg);
|
|
70
|
+
border: 1px solid var(--tel-border);
|
|
71
|
+
border-radius: var(--tel-radius);
|
|
72
|
+
padding: 10px 40px 10px 12px;
|
|
73
|
+
outline: none;
|
|
74
|
+
transition: border-color .15s, box-shadow .15s, background .15s;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.ngxsmk-tel-input__control::placeholder {
|
|
78
|
+
color: var(--tel-placeholder);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
.ngxsmk-tel-input__control:hover {
|
|
82
|
+
border-color: var(--tel-border-hover);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.ngxsmk-tel-input__control:focus {
|
|
86
|
+
border-color: var(--tel-ring);
|
|
87
|
+
box-shadow: var(--tel-focus-shadow);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/* Size presets */
|
|
91
|
+
[data-size="sm"] .ngxsmk-tel-input__control {
|
|
92
|
+
height: 34px;
|
|
93
|
+
font-size: 13px;
|
|
94
|
+
padding: 6px 36px 6px 10px;
|
|
95
|
+
border-radius: 10px;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
[data-size="lg"] .ngxsmk-tel-input__control {
|
|
99
|
+
height: 46px;
|
|
100
|
+
font-size: 16px;
|
|
101
|
+
padding: 12px 44px 12px 14px;
|
|
102
|
+
border-radius: 14px;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/* Variants */
|
|
106
|
+
[data-variant="filled"] .ngxsmk-tel-input__control {
|
|
107
|
+
background: rgba(148, 163, 184, .08);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
[data-variant="underline"] .ngxsmk-tel-input__control {
|
|
111
|
+
border: 0;
|
|
112
|
+
border-bottom: 2px solid var(--tel-border);
|
|
113
|
+
border-radius: 0;
|
|
114
|
+
padding-left: 0;
|
|
115
|
+
padding-right: 34px;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
[data-variant="underline"] .ngxsmk-tel-input__control:focus {
|
|
119
|
+
border-bottom-color: var(--tel-ring);
|
|
120
|
+
box-shadow: none;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/* ---------- intl-tel-input dropdown (deep selectors) ---------- */
|
|
124
|
+
:host ::ng-deep .iti__flag-container {
|
|
125
|
+
border-top-left-radius: var(--tel-radius);
|
|
126
|
+
border-bottom-left-radius: var(--tel-radius);
|
|
127
|
+
border: 1px solid var(--tel-border);
|
|
128
|
+
border-right: none;
|
|
129
|
+
background: var(--tel-bg);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
:host ::ng-deep .iti__selected-flag {
|
|
133
|
+
height: 100%;
|
|
134
|
+
padding: 0 10px;
|
|
135
|
+
display: inline-flex;
|
|
136
|
+
align-items: center;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
:host ::ng-deep .iti__country-list {
|
|
140
|
+
background: var(--tel-dd-bg);
|
|
141
|
+
border: 1px solid var(--tel-dd-border);
|
|
142
|
+
border-radius: var(--tel-dd-radius);
|
|
143
|
+
box-shadow: var(--tel-dd-shadow);
|
|
144
|
+
max-height: min(50vh, 360px);
|
|
145
|
+
overflow: auto;
|
|
146
|
+
padding: 6px 0;
|
|
147
|
+
width: max(280px, 100%);
|
|
148
|
+
z-index: var(--tel-dd-z);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
:host ::ng-deep .iti--container .iti__country-list {
|
|
152
|
+
z-index: var(--tel-dd-z);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
:host ::ng-deep .iti__search-input {
|
|
156
|
+
position: sticky;
|
|
157
|
+
top: 0;
|
|
158
|
+
margin: 0;
|
|
159
|
+
padding: 10px 12px;
|
|
160
|
+
width: 100%;
|
|
161
|
+
border: 0;
|
|
162
|
+
border-bottom: 1px solid var(--tel-dd-border);
|
|
163
|
+
outline: none;
|
|
164
|
+
background: var(--tel-dd-search-bg);
|
|
165
|
+
color: var(--tel-fg);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
:host ::ng-deep .iti__country {
|
|
169
|
+
display: grid;
|
|
170
|
+
grid-template-columns: 28px 1fr auto;
|
|
171
|
+
align-items: center;
|
|
172
|
+
column-gap: .5rem;
|
|
173
|
+
padding: 10px 12px;
|
|
174
|
+
cursor: pointer;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
:host ::ng-deep .iti__dial-code {
|
|
178
|
+
color: var(--tel-placeholder);
|
|
179
|
+
font-weight: 600;
|
|
180
|
+
margin-left: 10px;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/* Clear button */
|
|
184
|
+
.ngxsmk-tel__clear {
|
|
185
|
+
position: absolute;
|
|
186
|
+
right: 8px;
|
|
187
|
+
top: 50%;
|
|
188
|
+
transform: translateY(-50%);
|
|
189
|
+
border: 0;
|
|
190
|
+
background: transparent;
|
|
191
|
+
font-size: 18px;
|
|
192
|
+
line-height: 1;
|
|
193
|
+
width: 28px;
|
|
194
|
+
height: 28px;
|
|
195
|
+
border-radius: 50%;
|
|
196
|
+
cursor: pointer;
|
|
197
|
+
color: var(--tel-placeholder);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
.ngxsmk-tel__clear:hover {
|
|
201
|
+
background: rgba(148, 163, 184, .15);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/* Hint & Error */
|
|
205
|
+
.ngxsmk-tel__hint {
|
|
206
|
+
margin-top: 6px;
|
|
207
|
+
font-size: 12px;
|
|
208
|
+
color: var(--tel-placeholder);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
.ngxsmk-tel__error {
|
|
212
|
+
margin-top: 6px;
|
|
213
|
+
font-size: 12px;
|
|
214
|
+
color: var(--tel-error);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
.ngxsmk-tel__wrap.has-error .ngxsmk-tel-input__control {
|
|
218
|
+
border-color: var(--tel-error);
|
|
219
|
+
box-shadow: 0 0 0 3px rgba(239, 68, 68, .15);
|
|
220
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
2
|
+
import { PLATFORM_ID } from '@angular/core';
|
|
3
|
+
import { NgxsmkTelInputComponent } from './ngxsmk-tel-input.component';
|
|
4
|
+
|
|
5
|
+
describe('NgxsmkTelInputComponent', () => {
|
|
6
|
+
let component: NgxsmkTelInputComponent;
|
|
7
|
+
let fixture: ComponentFixture<NgxsmkTelInputComponent>;
|
|
8
|
+
|
|
9
|
+
beforeEach(async () => {
|
|
10
|
+
await TestBed.configureTestingModule({
|
|
11
|
+
imports: [NgxsmkTelInputComponent],
|
|
12
|
+
// Pretend we're on the server so ngAfterViewInit skips intl-tel-input init
|
|
13
|
+
providers: [{ provide: PLATFORM_ID, useValue: 'server' }],
|
|
14
|
+
}).compileComponents();
|
|
15
|
+
|
|
16
|
+
fixture = TestBed.createComponent(NgxsmkTelInputComponent);
|
|
17
|
+
component = fixture.componentInstance;
|
|
18
|
+
fixture.detectChanges();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should create', () => {
|
|
22
|
+
expect(component).toBeTruthy();
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AfterViewInit,
|
|
3
|
+
Component,
|
|
4
|
+
ElementRef,
|
|
5
|
+
EventEmitter,
|
|
6
|
+
forwardRef,
|
|
7
|
+
Inject,
|
|
8
|
+
Input,
|
|
9
|
+
NgZone,
|
|
10
|
+
OnChanges,
|
|
11
|
+
OnDestroy,
|
|
12
|
+
Output,
|
|
13
|
+
PLATFORM_ID,
|
|
14
|
+
SimpleChanges,
|
|
15
|
+
ViewChild
|
|
16
|
+
} from '@angular/core';
|
|
17
|
+
import {isPlatformBrowser} from '@angular/common';
|
|
18
|
+
import {
|
|
19
|
+
AbstractControl,
|
|
20
|
+
ControlValueAccessor,
|
|
21
|
+
NG_VALIDATORS,
|
|
22
|
+
NG_VALUE_ACCESSOR,
|
|
23
|
+
ValidationErrors,
|
|
24
|
+
Validator
|
|
25
|
+
} from '@angular/forms';
|
|
26
|
+
import type {CountryCode} from 'libphonenumber-js';
|
|
27
|
+
import {NgxsmkTelInputService} from './ngxsmk-tel-input.service';
|
|
28
|
+
import {CountryMap, IntlTelI18n} from './types';
|
|
29
|
+
|
|
30
|
+
type IntlTelInstance = any;
|
|
31
|
+
|
|
32
|
+
@Component({
|
|
33
|
+
selector: 'ngxsmk-tel-input',
|
|
34
|
+
standalone: true,
|
|
35
|
+
imports: [],
|
|
36
|
+
template: `
|
|
37
|
+
<div class="ngxsmk-tel"
|
|
38
|
+
[class.disabled]="disabled"
|
|
39
|
+
[attr.data-size]="size"
|
|
40
|
+
[attr.data-variant]="variant"
|
|
41
|
+
[attr.dir]="dir">
|
|
42
|
+
@if (label) {
|
|
43
|
+
<label class="ngxsmk-tel__label" [for]="resolvedId">{{ label }}</label>
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
<div class="ngxsmk-tel__wrap" [class.has-error]="showError">
|
|
47
|
+
<div class="ngxsmk-tel-input__wrapper">
|
|
48
|
+
<input
|
|
49
|
+
#telInput
|
|
50
|
+
type="tel"
|
|
51
|
+
class="ngxsmk-tel-input__control"
|
|
52
|
+
[id]="resolvedId"
|
|
53
|
+
[attr.name]="name || null"
|
|
54
|
+
[attr.placeholder]="placeholder || null"
|
|
55
|
+
[attr.autocomplete]="autocomplete"
|
|
56
|
+
[attr.inputmode]="digitsOnly ? 'numeric' : 'tel'"
|
|
57
|
+
[attr.pattern]="digitsOnly ? (allowLeadingPlus ? '\\\\+?[0-9]*' : '[0-9]*') : null"
|
|
58
|
+
[disabled]="disabled"
|
|
59
|
+
[attr.aria-invalid]="showError ? 'true' : 'false'"
|
|
60
|
+
(blur)="onBlur()"
|
|
61
|
+
(focus)="onFocus()"
|
|
62
|
+
/>
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
@if (showClear && currentRaw()) {
|
|
66
|
+
<button type="button"
|
|
67
|
+
class="ngxsmk-tel__clear"
|
|
68
|
+
(click)="clearInput()"
|
|
69
|
+
[attr.aria-label]="clearAriaLabel">
|
|
70
|
+
×
|
|
71
|
+
</button>
|
|
72
|
+
}
|
|
73
|
+
</div>
|
|
74
|
+
|
|
75
|
+
@if (hint && !showError) {
|
|
76
|
+
<div class="ngxsmk-tel__hint">{{ hint }}</div>
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@if (showError) {
|
|
80
|
+
<div class="ngxsmk-tel__error">{{ errorText || 'Please enter a valid phone number.' }}</div>
|
|
81
|
+
}
|
|
82
|
+
</div>
|
|
83
|
+
`,
|
|
84
|
+
styleUrls: ['./ngxsmk-tel-input.component.scss'],
|
|
85
|
+
providers: [
|
|
86
|
+
{provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NgxsmkTelInputComponent), multi: true},
|
|
87
|
+
{provide: NG_VALIDATORS, useExisting: forwardRef(() => NgxsmkTelInputComponent), multi: true}
|
|
88
|
+
]
|
|
89
|
+
})
|
|
90
|
+
export class NgxsmkTelInputComponent implements AfterViewInit, OnChanges, OnDestroy, ControlValueAccessor, Validator {
|
|
91
|
+
|
|
92
|
+
@ViewChild('telInput', {static: true}) inputRef!: ElementRef<HTMLInputElement>;
|
|
93
|
+
|
|
94
|
+
/* Core config */
|
|
95
|
+
@Input() initialCountry: CountryCode | 'auto' = 'US';
|
|
96
|
+
@Input() preferredCountries: CountryCode[] = ['US', 'GB'];
|
|
97
|
+
@Input() onlyCountries?: CountryCode[];
|
|
98
|
+
@Input() nationalMode: boolean = false;
|
|
99
|
+
@Input() separateDialCode: boolean = false;
|
|
100
|
+
@Input() allowDropdown: boolean = true;
|
|
101
|
+
|
|
102
|
+
/* UX */
|
|
103
|
+
@Input() placeholder?: string;
|
|
104
|
+
@Input() autocomplete = 'tel';
|
|
105
|
+
@Input() name?: string;
|
|
106
|
+
@Input() inputId?: string;
|
|
107
|
+
@Input() disabled: boolean = false;
|
|
108
|
+
|
|
109
|
+
@Input() label?: string;
|
|
110
|
+
@Input() hint?: string;
|
|
111
|
+
@Input() errorText?: string;
|
|
112
|
+
@Input() size: 'sm' | 'md' | 'lg' = 'md';
|
|
113
|
+
@Input() variant: 'outline' | 'filled' | 'underline' = 'outline';
|
|
114
|
+
@Input() showClear: boolean = true;
|
|
115
|
+
@Input() autoFocus: boolean = false;
|
|
116
|
+
@Input() selectOnFocus: boolean = false;
|
|
117
|
+
@Input() formatOnBlur: boolean = true;
|
|
118
|
+
@Input() showErrorWhenTouched: boolean = true;
|
|
119
|
+
|
|
120
|
+
/* Dropdown plumbing */
|
|
121
|
+
@Input() dropdownAttachToBody: boolean = true;
|
|
122
|
+
@Input() dropdownZIndex: number = 2000;
|
|
123
|
+
|
|
124
|
+
/* Localization + RTL */
|
|
125
|
+
@Input('i18n') i18n?: IntlTelI18n;
|
|
126
|
+
|
|
127
|
+
@Input('telI18n') set telI18n(v: IntlTelI18n | undefined) {
|
|
128
|
+
this.i18n = v;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
@Input('localizedCountries') localizedCountries?: CountryMap;
|
|
132
|
+
|
|
133
|
+
@Input('telLocalizedCountries') set telLocalizedCountries(v: CountryMap | undefined) {
|
|
134
|
+
this.localizedCountries = v;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
@Input() clearAriaLabel: string = 'Clear phone number';
|
|
138
|
+
@Input() dir: 'ltr' | 'rtl' = 'ltr';
|
|
139
|
+
|
|
140
|
+
/* Placeholders (intl-tel-input) */
|
|
141
|
+
@Input() autoPlaceholder: 'off' | 'polite' | 'aggressive' = 'off'; // default OFF since no utils fallback
|
|
142
|
+
@Input() utilsScript?: string;
|
|
143
|
+
@Input() customPlaceholder?: (example: string, country: any) => string;
|
|
144
|
+
|
|
145
|
+
@Input() formatWhenValid: 'off' | 'blur' | 'typing' = 'blur';
|
|
146
|
+
|
|
147
|
+
/* Digits-only controls */
|
|
148
|
+
@Input() digitsOnly: boolean = true;
|
|
149
|
+
@Input() allowLeadingPlus: boolean = true;
|
|
150
|
+
|
|
151
|
+
/* Outputs */
|
|
152
|
+
@Output() countryChange = new EventEmitter<{ iso2: CountryCode }>();
|
|
153
|
+
@Output() validityChange = new EventEmitter<boolean>();
|
|
154
|
+
@Output() inputChange = new EventEmitter<{ raw: string; e164: string | null; iso2: CountryCode }>();
|
|
155
|
+
|
|
156
|
+
/* Internal */
|
|
157
|
+
private iti: IntlTelInstance | null = null;
|
|
158
|
+
private onChange: (val: string | null) => void = () => {
|
|
159
|
+
};
|
|
160
|
+
private onTouchedCb: () => void = () => {
|
|
161
|
+
};
|
|
162
|
+
private validatorChange?: () => void;
|
|
163
|
+
private lastEmittedValid = false;
|
|
164
|
+
private pendingWrite: string | null = null;
|
|
165
|
+
private touched: boolean = false;
|
|
166
|
+
|
|
167
|
+
readonly resolvedId: string = this.inputId || ('tel-' + Math.random().toString(36).slice(2));
|
|
168
|
+
|
|
169
|
+
constructor(
|
|
170
|
+
private readonly zone: NgZone,
|
|
171
|
+
private readonly tel: NgxsmkTelInputService,
|
|
172
|
+
@Inject(PLATFORM_ID) private readonly platformId: Object
|
|
173
|
+
) {
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
ngAfterViewInit(): void {
|
|
177
|
+
if (!isPlatformBrowser(this.platformId)) return;
|
|
178
|
+
void this.initAndWire();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private async initAndWire(): Promise<void> {
|
|
182
|
+
await this.initIntlTelInput();
|
|
183
|
+
this.bindDomListeners();
|
|
184
|
+
|
|
185
|
+
if (this.pendingWrite !== null) {
|
|
186
|
+
this.setInputValue(this.pendingWrite);
|
|
187
|
+
this.handleInput();
|
|
188
|
+
this.pendingWrite = null;
|
|
189
|
+
}
|
|
190
|
+
if (this.autoFocus) setTimeout(() => this.focus(), 0);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
ngOnChanges(changes: SimpleChanges): void {
|
|
194
|
+
if (!isPlatformBrowser(this.platformId)) return;
|
|
195
|
+
const configChanged = [
|
|
196
|
+
'initialCountry', 'preferredCountries', 'onlyCountries',
|
|
197
|
+
'separateDialCode', 'allowDropdown', 'nationalMode',
|
|
198
|
+
'i18n', 'localizedCountries', 'dir',
|
|
199
|
+
'autoPlaceholder', 'utilsScript', 'customPlaceholder',
|
|
200
|
+
'digitsOnly', 'allowLeadingPlus'
|
|
201
|
+
].some(k => k in changes && !changes[k]?.firstChange);
|
|
202
|
+
if (configChanged && this.iti) {
|
|
203
|
+
this.reinitPlugin();
|
|
204
|
+
this.validatorChange?.();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
ngOnDestroy(): void {
|
|
209
|
+
this.destroyPlugin();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ----- CVA -----
|
|
213
|
+
writeValue(val: string | null): void {
|
|
214
|
+
if (!this.inputRef) return;
|
|
215
|
+
if (!this.iti) {
|
|
216
|
+
this.pendingWrite = val ?? '';
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
this.setInputValue(val ?? '');
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
registerOnChange(fn: any): void {
|
|
223
|
+
this.onChange = fn;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
registerOnTouched(fn: any): void {
|
|
227
|
+
this.onTouchedCb = fn;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
setDisabledState(isDisabled: boolean): void {
|
|
231
|
+
this.disabled = isDisabled;
|
|
232
|
+
if (this.inputRef) this.inputRef.nativeElement.disabled = isDisabled;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ----- Validator -----
|
|
236
|
+
validate(_: AbstractControl): ValidationErrors | null {
|
|
237
|
+
const raw = this.currentRaw();
|
|
238
|
+
if (!raw) return null;
|
|
239
|
+
const valid = this.tel.isValid(raw, this.currentIso2());
|
|
240
|
+
if (valid !== this.lastEmittedValid) {
|
|
241
|
+
this.lastEmittedValid = valid;
|
|
242
|
+
this.validityChange.emit(valid);
|
|
243
|
+
}
|
|
244
|
+
return valid ? null : {phoneInvalid: true};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
registerOnValidatorChange(fn: () => void): void {
|
|
248
|
+
this.validatorChange = fn;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ----- Public helpers -----
|
|
252
|
+
focus(): void {
|
|
253
|
+
this.inputRef?.nativeElement.focus();
|
|
254
|
+
if (this.selectOnFocus) {
|
|
255
|
+
const el = this.inputRef.nativeElement;
|
|
256
|
+
queueMicrotask(() => el.setSelectionRange(0, el.value.length));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
selectCountry(iso2: CountryCode): void {
|
|
261
|
+
if (this.iti) {
|
|
262
|
+
this.iti.setCountry(iso2.toLowerCase());
|
|
263
|
+
this.handleInput();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
clearInput() {
|
|
268
|
+
this.setInputValue('');
|
|
269
|
+
this.handleInput();
|
|
270
|
+
this.inputRef.nativeElement.focus();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// ----- Plugin wiring -----
|
|
274
|
+
private async initIntlTelInput() {
|
|
275
|
+
const [{default: intlTelInput}] = await Promise.all([import('intl-tel-input')]);
|
|
276
|
+
|
|
277
|
+
const toLowerKeys = (m?: CountryMap) => {
|
|
278
|
+
if (!m) return undefined;
|
|
279
|
+
const out: Record<string, string> = {};
|
|
280
|
+
for (const k in m) {
|
|
281
|
+
if (Object.prototype.hasOwnProperty.call(m, k)) {
|
|
282
|
+
const v = (m as Record<string, string | undefined>)[k];
|
|
283
|
+
if (v != null) out[k.toLowerCase()] = v;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return out;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const config: any = {
|
|
290
|
+
initialCountry: this.initialCountry === 'auto' ? 'auto' : (this.initialCountry?.toLowerCase() || 'us'),
|
|
291
|
+
preferredCountries: (this.preferredCountries ?? []).map(c => c.toLowerCase()),
|
|
292
|
+
onlyCountries: (this.onlyCountries ?? []).map(c => c.toLowerCase()),
|
|
293
|
+
nationalMode: this.nationalMode,
|
|
294
|
+
allowDropdown: this.allowDropdown,
|
|
295
|
+
separateDialCode: this.separateDialCode,
|
|
296
|
+
geoIpLookup: (cb: (iso2: string) => void) => cb('us'),
|
|
297
|
+
|
|
298
|
+
// placeholders
|
|
299
|
+
autoPlaceholder: this.autoPlaceholder,
|
|
300
|
+
utilsScript: this.utilsScript,
|
|
301
|
+
customPlaceholder: this.customPlaceholder,
|
|
302
|
+
|
|
303
|
+
// localization
|
|
304
|
+
i18n: this.i18n,
|
|
305
|
+
localizedCountries: toLowerKeys(this.localizedCountries),
|
|
306
|
+
|
|
307
|
+
// dropdown container
|
|
308
|
+
dropdownContainer: this.dropdownAttachToBody && typeof document !== 'undefined' ? document.body : undefined
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
this.zone.runOutsideAngular(() => {
|
|
312
|
+
this.iti = intlTelInput(this.inputRef.nativeElement, config);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
(this.inputRef.nativeElement as HTMLElement).style.setProperty('--tel-dd-z', String(this.dropdownZIndex));
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private reinitPlugin() {
|
|
319
|
+
const current = this.currentRaw();
|
|
320
|
+
this.destroyPlugin();
|
|
321
|
+
this.initIntlTelInput().then(() => {
|
|
322
|
+
if (current) {
|
|
323
|
+
this.setInputValue(current);
|
|
324
|
+
this.handleInput();
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private destroyPlugin() {
|
|
330
|
+
if (this.iti) {
|
|
331
|
+
this.iti.destroy();
|
|
332
|
+
this.iti = null;
|
|
333
|
+
}
|
|
334
|
+
if (this.inputRef?.nativeElement) {
|
|
335
|
+
const el = this.inputRef.nativeElement;
|
|
336
|
+
const clone = el.cloneNode(true) as HTMLInputElement;
|
|
337
|
+
el.parentNode?.replaceChild(clone, el);
|
|
338
|
+
(this.inputRef as any).nativeElement = clone;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// ----- Input filtering (digits-only) -----
|
|
343
|
+
private sanitizeDigits(value: string): string {
|
|
344
|
+
if (!this.digitsOnly) return value;
|
|
345
|
+
let v = value.replace(/[^\d+]/g, '');
|
|
346
|
+
if (this.allowLeadingPlus) {
|
|
347
|
+
const hasLeadingPlus = v.startsWith('+');
|
|
348
|
+
v = (hasLeadingPlus ? '+' : '') + v.replace(/\+/g, '');
|
|
349
|
+
} else {
|
|
350
|
+
v = v.replace(/\+/g, '');
|
|
351
|
+
}
|
|
352
|
+
return v;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
private bindDomListeners() {
|
|
356
|
+
const el = this.inputRef.nativeElement;
|
|
357
|
+
|
|
358
|
+
this.zone.runOutsideAngular(() => {
|
|
359
|
+
el.addEventListener('beforeinput', (ev: InputEvent) => {
|
|
360
|
+
if (!this.digitsOnly) return;
|
|
361
|
+
const data = (ev as any).data as string | null;
|
|
362
|
+
if (!data || ev.inputType !== 'insertText') return;
|
|
363
|
+
|
|
364
|
+
const pos = el.selectionStart ?? 0;
|
|
365
|
+
const isDigit = data >= '0' && data <= '9';
|
|
366
|
+
const isPlusAtStart = this.allowLeadingPlus && data === '+' && pos === 0 && !el.value.includes('+');
|
|
367
|
+
|
|
368
|
+
if (!isDigit && !isPlusAtStart) ev.preventDefault();
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
el.addEventListener('paste', (e: ClipboardEvent) => {
|
|
372
|
+
if (!this.digitsOnly) return;
|
|
373
|
+
e.preventDefault();
|
|
374
|
+
const text = (e.clipboardData || (window as any).clipboardData).getData('text');
|
|
375
|
+
const sanitized = this.sanitizeDigits(text);
|
|
376
|
+
const start = el.selectionStart ?? el.value.length;
|
|
377
|
+
const end = el.selectionEnd ?? el.value.length;
|
|
378
|
+
el.setRangeText(sanitized, start, end, 'end');
|
|
379
|
+
queueMicrotask(() => this.handleInput());
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
el.addEventListener('input', () => {
|
|
383
|
+
if (this.digitsOnly) {
|
|
384
|
+
const val = el.value;
|
|
385
|
+
const sanitized = this.sanitizeDigits(val);
|
|
386
|
+
if (val !== sanitized) {
|
|
387
|
+
const caret = el.selectionStart ?? sanitized.length;
|
|
388
|
+
el.value = sanitized;
|
|
389
|
+
el.setSelectionRange(caret, caret);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
this.handleInput();
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
el.addEventListener('countrychange', () => {
|
|
396
|
+
const iso2 = this.currentIso2();
|
|
397
|
+
this.zone.run(() => {
|
|
398
|
+
this.countryChange.emit({iso2});
|
|
399
|
+
this.validatorChange?.();
|
|
400
|
+
});
|
|
401
|
+
this.handleInput();
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
el.addEventListener('blur', () => this.onBlur());
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
onBlur() {
|
|
409
|
+
this.touched = true;
|
|
410
|
+
this.zone.run(() => this.onTouchedCb());
|
|
411
|
+
if (!this.formatOnBlur) return;
|
|
412
|
+
const raw = this.currentRaw();
|
|
413
|
+
if (!raw) return;
|
|
414
|
+
const parsed = this.tel.parse(raw, this.currentIso2());
|
|
415
|
+
if (this.nationalMode && parsed.national) {
|
|
416
|
+
this.setInputValue((parsed.national || '').replace(/\s{2,}/g, ' '));
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
onFocus() {
|
|
421
|
+
if (this.selectOnFocus) {
|
|
422
|
+
const el = this.inputRef.nativeElement;
|
|
423
|
+
queueMicrotask(() => el.setSelectionRange(0, el.value.length));
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
private handleInput() {
|
|
428
|
+
const raw = this.currentRaw();
|
|
429
|
+
const iso2 = this.currentIso2();
|
|
430
|
+
const parsed = this.tel.parse(raw, iso2);
|
|
431
|
+
this.zone.run(() => this.onChange(parsed.e164)); // E.164 or null
|
|
432
|
+
this.zone.run(() => this.inputChange.emit({raw, e164: parsed.e164, iso2}));
|
|
433
|
+
if (raw && this.nationalMode && parsed.national) {
|
|
434
|
+
const normalized = parsed.national.replace(/\s{2,}/g, ' ');
|
|
435
|
+
if (normalized !== raw) this.setInputValue(normalized);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
currentRaw(): string {
|
|
440
|
+
return (this.inputRef?.nativeElement.value ?? '').trim();
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private currentIso2(): CountryCode {
|
|
444
|
+
const iso2 = (this.iti?.getSelectedCountryData?.().iso2 ?? this.initialCountry ?? 'US')
|
|
445
|
+
.toString().toUpperCase();
|
|
446
|
+
return iso2 as CountryCode;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
private setInputValue(v: string) {
|
|
450
|
+
this.inputRef.nativeElement.value = v ?? '';
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
get showError(): boolean {
|
|
454
|
+
const invalid = !!this.validate({} as AbstractControl);
|
|
455
|
+
return this.showErrorWhenTouched ? (this.touched && invalid) : invalid;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
import { NgxsmkTelInputService } from './ngxsmk-tel-input.service';
|
|
3
|
+
|
|
4
|
+
describe('NgxsmkTelInputService', () => {
|
|
5
|
+
let service: NgxsmkTelInputService;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
TestBed.configureTestingModule({});
|
|
9
|
+
service = TestBed.inject(NgxsmkTelInputService);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should be created', () => {
|
|
13
|
+
expect(service).toBeTruthy();
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { parsePhoneNumberFromString, type CountryCode } from 'libphonenumber-js';
|
|
3
|
+
|
|
4
|
+
@Injectable({ providedIn: 'root' })
|
|
5
|
+
export class NgxsmkTelInputService {
|
|
6
|
+
parse(input: string, iso2: CountryCode): { e164: string | null; national: string | null; isValid: boolean } {
|
|
7
|
+
const phone = parsePhoneNumberFromString(input || '', iso2);
|
|
8
|
+
if (!phone) return { e164: null, national: null, isValid: false };
|
|
9
|
+
const isValid = phone.isValid();
|
|
10
|
+
return { e164: isValid ? phone.number : null, national: phone.formatNational(), isValid };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
isValid(input: string, iso2: CountryCode): boolean {
|
|
14
|
+
const phone = parsePhoneNumberFromString(input || '', iso2);
|
|
15
|
+
return !!phone && phone.isValid();
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/lib/types.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CountryCode } from 'libphonenumber-js';
|
|
2
|
+
|
|
3
|
+
export type CountryMap = Partial<Record<CountryCode, string>>;
|
|
4
|
+
|
|
5
|
+
export interface IntlTelI18n {
|
|
6
|
+
selectedCountryAriaLabel?: string;
|
|
7
|
+
countryListAriaLabel?: string;
|
|
8
|
+
searchPlaceholder?: string;
|
|
9
|
+
zeroSearchResults?: string;
|
|
10
|
+
noCountrySelected?: string;
|
|
11
|
+
}
|