ngx-dial-input 2.0.0 → 2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ngx-dial-input",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Allows users to select a country and automatically formats the phone number input based on the selected country.",
5
5
  "author": "CroemInc",
6
6
  "license": "MIT",
@@ -1,12 +1,9 @@
1
1
  {
2
- "name": "phone-input",
3
- "version": "0.0.1",
2
+ "name": "ngx-dial-input",
3
+ "version": "2.0.2",
4
4
  "peerDependencies": {
5
- "@angular/common": "15.1.1",
6
- "@angular/core": "15.1.1"
7
- },
8
- "dependencies": {
9
- "tslib": "^2.3.0"
10
- },
11
- "sideEffects": false
5
+ "@angular/common": "15.2.10",
6
+ "@angular/core": "15.2.10",
7
+ "@angular/forms": "^15.1.1"
8
+ }
12
9
  }
@@ -55,14 +55,14 @@ import {
55
55
  <!-- Phone Input (80%) -->
56
56
  <input
57
57
  type="tel"
58
- pattern="[0-9]*"
58
+ pattern="[0-9\\s\\-]*"
59
59
  inputmode="numeric"
60
60
  class="phone-input"
61
61
  [placeholder]="placeholder"
62
62
  [(ngModel)]="phoneNumber"
63
63
  [ngClass]="{ 'error-border': isInvalid }"
64
64
  (input)="onPhoneInput()"
65
- (keypress)="allowOnlyNumbers($event)"
65
+ (keypress)="allowPhoneCharacters($event)"
66
66
  />
67
67
  </div>
68
68
 
@@ -257,13 +257,15 @@ export class PhoneInputComponent implements ControlValueAccessor {
257
257
  }
258
258
  }
259
259
 
260
- onSearchChange() {
260
+ onSearchChange() {
261
261
  const term = this.searchTerm.toLowerCase();
262
262
  this.countriesList = countries.filter(c =>
263
- c.name.toLowerCase().includes(term)
263
+ c.name.toLowerCase().includes(term) ||
264
+ (c.native?.toLowerCase().includes(term))
264
265
  );
265
266
  }
266
267
 
268
+
267
269
  selectCountry(country: any) {
268
270
  this.selectedCountry = country;
269
271
  this.searchTerm = '';
@@ -277,23 +279,28 @@ export class PhoneInputComponent implements ControlValueAccessor {
277
279
  }
278
280
 
279
281
  private emitPhoneChange() {
280
- const digitsOnly = this.phoneNumber.replace(/\D/g, '');
281
- const length = digitsOnly.length;
282
+ const raw = this.phoneNumber || '';
283
+ const digitsOnly = raw.replace(/\D/g, '');
284
+ const hyphenCount = (raw.match(/-/g) || []).length;
285
+ const spaceCount = (raw.match(/ /g) || []).length;
282
286
 
283
- const expectedLength = this.selectedCountry?.phoneLength;
287
+ const totalChars = this.phoneNumber.length;
288
+ const maxAllowedLength = this.selectedCountry?.phoneLength + hyphenCount + spaceCount;
284
289
 
285
- if (expectedLength) {
286
- this.isInvalid = length !== expectedLength;
287
- } else {
288
- // fallback validation
289
- this.isInvalid = length < 7 || length > 12;
290
+ if (digitsOnly.length !== this.selectedCountry?.phoneLength) {
291
+ this.isInvalid = true;
290
292
  }
291
-
292
- const fullPhone = this.selectedCountry.dialCode + this.phoneNumber;
293
+ else if (totalChars > maxAllowedLength) {
294
+ this.isInvalid = true;
295
+ }
296
+ else {
297
+ this.isInvalid = false;
298
+ }
299
+ const fullPhone = this.selectedCountry.dialCode + raw;
293
300
 
294
301
  const structuredValue = {
295
302
  e164Number: fullPhone,
296
- nationalNumber: this.phoneNumber,
303
+ nationalNumber: raw,
297
304
  dialCode: this.selectedCountry.dialCode,
298
305
  countryCode: this.selectedCountry.iso2,
299
306
  isValid: !this.isInvalid,
@@ -326,13 +333,16 @@ export class PhoneInputComponent implements ControlValueAccessor {
326
333
  registerOnTouched(fn: any): void {
327
334
  this.onTouched = fn;
328
335
  }
329
- allowOnlyNumbers(event: KeyboardEvent) {
330
- const charCode = event.charCode;
336
+ allowPhoneCharacters(event: KeyboardEvent) {
337
+ const allowedChars = [' ', '-'];
338
+ const charCode = event.charCode;
339
+
340
+ // Allow digits 0–9
341
+ if (charCode >= 48 && charCode <= 57) return;
342
+
343
+ // Allow hyphen and space
344
+ if (allowedChars.includes(event.key)) return;
331
345
 
332
- // Allow only digits (0–9)
333
- if (charCode < 48 || charCode > 57) {
334
346
  event.preventDefault();
335
347
  }
336
348
  }
337
-
338
- }
Binary file