From e13114fb37bf5a398f084b425fc5387209e95c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Du=C5=9B?= Date: Fri, 26 Oct 2018 09:19:15 +0200 Subject: [PATCH] * added missing countries: argentina, austraila, cocos island * added input for specifying which country flag should be display in same dial code situatuion (when we manually enter the phone number) --- README.md | 4 ++ .../int-phone-prefix.component.ts | 40 +++++++++++++++---- src/lib/src/interface/country.interface.ts | 6 +-- src/lib/src/service/country.service.ts | 19 +++++++-- 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a8f9622..93e64ca 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,10 @@ To install this component to an external project, follow the procedure: ### @Input() onlyNumbers Allows only numeric values (default: true) + ### @Input() firstlySelectedCountries + An ISO 639-1 country codes array can be provided to define which country flag should + be displayed in same dial code situation. Array is empty by default + ## License * License: MIT diff --git a/src/lib/src/component/int-phone-prefix/int-phone-prefix.component.ts b/src/lib/src/component/int-phone-prefix/int-phone-prefix.component.ts index b1e3a8e..75117b6 100644 --- a/src/lib/src/component/int-phone-prefix/int-phone-prefix.component.ts +++ b/src/lib/src/component/int-phone-prefix/int-phone-prefix.component.ts @@ -35,6 +35,9 @@ export class IntPhonePrefixComponent implements OnInit, ControlValueAccessor { @Input() defaultCountry: string; + @Input() + firstlySelectedCountries: string[] = []; + @Input() maxLength = 15; @@ -49,6 +52,7 @@ export class IntPhonePrefixComponent implements OnInit, ControlValueAccessor { onModelChange: Function; countries: Country[]; + prioritizedCountries: Country[]; locales: CountryCode; selectedCountry: Country; countryFilter: string; @@ -72,6 +76,7 @@ export class IntPhonePrefixComponent implements OnInit, ControlValueAccessor { ngOnInit(): void { this.countries = this.service.getCountries(); + this.prioritizedCountries = this.getPrioritizedCountries(); this.locales = this.localeService.getLocales(this.locale); this.translateCountryNames(); } @@ -156,9 +161,9 @@ export class IntPhonePrefixComponent implements OnInit, ControlValueAccessor { } private findPrefix(prefix: string) { - let foundPrefixes: Country[] = this.countries.filter((country: Country) => prefix.startsWith(country.dialCode)); + let foundPrefixes: Country[] = _.filter(this.countries, (country: Country) => prefix.startsWith(country.dialCode)); this.selectedCountry = !_.isEmpty(foundPrefixes) - ? IntPhonePrefixComponent.reducePrefixes(foundPrefixes) + ? IntPhonePrefixComponent.reducePrefixes(foundPrefixes, this.prioritizedCountries) : null; } @@ -168,16 +173,35 @@ export class IntPhonePrefixComponent implements OnInit, ControlValueAccessor { this.onTouch(); } + private getPrioritizedCountries() { + const selected: Country[] = []; + _.map(this.firstlySelectedCountries, (countryCode: string) => { + selected.push(_.find(this.countries, (country: Country) => { + return country.countryCode === countryCode; + })); + }); + return _.compact(selected); + } + private static startsWithPlus(text: string): boolean { return text.startsWith(PLUS); } - private static reducePrefixes(foundPrefixes: Country[]) { + private static reducePrefixes(foundPrefixes: Country[], prioritizedCountries: Country[]) { return foundPrefixes.reduce( - (first: Country, second: Country) => - first.dialCode.length > second.dialCode.length - ? first - : second - ); + (first: Country, second: Country) => this.checkPriorities(first, second, prioritizedCountries)); + } + + private static checkPriorities(first: Country, second: Country, prioritizedCountries: Country[]) { + const checkFirst = _.find(prioritizedCountries, (item: Country) => item.countryCode === first.countryCode); + const checkSecond = _.find(prioritizedCountries, (item: Country) => item.countryCode === second.countryCode); + + if (first.dialCode.length === second.dialCode.length) { + return checkFirst || checkSecond || second; + } + + return first.dialCode.length > second.dialCode.length + ? first + : second; } } diff --git a/src/lib/src/interface/country.interface.ts b/src/lib/src/interface/country.interface.ts index 4a94c4b..e1a8323 100644 --- a/src/lib/src/interface/country.interface.ts +++ b/src/lib/src/interface/country.interface.ts @@ -1,5 +1,5 @@ export interface Country { - name: string, - dialCode: string - countryCode: string, + name: string; + dialCode: string; + countryCode: string; } diff --git a/src/lib/src/service/country.service.ts b/src/lib/src/service/country.service.ts index 8e02cac..6d474ae 100644 --- a/src/lib/src/service/country.service.ts +++ b/src/lib/src/service/country.service.ts @@ -1,5 +1,5 @@ import {Injectable} from '@angular/core'; -import {Country} from "../interface/country.interface"; +import {Country} from '../..'; @Injectable() export class CountryService { @@ -13,6 +13,16 @@ export class CountryService { dialCode: '994', countryCode: 'az' }, + { + name: '', + dialCode: '54', + countryCode: 'ar' + }, + { + name: '', + dialCode: '61', + countryCode: 'au' + }, { name: '', dialCode: '1242', @@ -168,6 +178,11 @@ export class CountryService { dialCode: '61', countryCode: 'cx' }, + { + name: '', + dialCode: '61', + countryCode: 'cc' + }, { name: '', dialCode: '57', @@ -1154,8 +1169,6 @@ export class CountryService { getCountries(): Country[] { return this.countries; } - - }