Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add preferred countries and focus input after change country #3

Merged
merged 1 commit into from
May 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/ngx-intl-tel-input.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
<div class="iti-arrow"></div>
</div>
<ul class="country-list dropdown-menu" *dropdownMenu>
<li class="country" *ngFor="let country of allCountries" (click)="onCountrySelect(country)">
<li class="country" *ngFor="let country of preferredCountriesInDropDown" (click)="onCountrySelect(country, focusable)">
<div class="flag-box">
<div class="iti-flag" [ngClass]="country.flagClass"></div>
</div>
<span class="country-name">{{country.name}}</span>
<span class="dial-code">+{{country.dialCode}}</span>
</li>
<li class="divider"></li>
<li class="country" *ngFor="let country of allCountries" (click)="onCountrySelect(country, focusable)">
<div class="flag-box">
<div class="iti-flag" [ngClass]="country.flagClass"></div>
</div>
Expand All @@ -18,5 +26,5 @@
(keypress)="onInputKeyPress($event)"
[(ngModel)]="phone_number"
(ngModelChange)="onPhoneNumberChange()"
[placeholder]="selectedCountry.placeHolder">
</div>
[placeholder]="selectedCountry.placeHolder" #focusable>
</div>
20 changes: 17 additions & 3 deletions src/ngx-intl-tel-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import * as _ from 'google-libphonenumber';
})
export class NgxIntlTelInputComponent implements OnInit {
@Input() value = '';
@Input() preferredCountries: Array<string> = [];
@Output() valueChange: EventEmitter<string> = new EventEmitter<string>();

phone_number = '';
allCountries: Array<Country> = [];
preferredCountriesInDropDown: Array<Country> = [];
selectedCountry: Country = new Country();
constructor(
private countryCodeData: CountryCode
Expand All @@ -23,20 +25,33 @@ export class NgxIntlTelInputComponent implements OnInit {
}

ngOnInit() {
this.selectedCountry = this.allCountries[0];
if (this.preferredCountries.length) {
this.preferredCountries.forEach(iso2 => {
let preferredCountry = this.allCountries.filter((c) => {
return c.iso2 === iso2;
});
this.preferredCountriesInDropDown.push(preferredCountry[0]);
});
}
if (this.preferredCountriesInDropDown.length) {
this.selectedCountry = this.preferredCountriesInDropDown[0];
} else {
this.selectedCountry = this.allCountries[0];
}
}

public onPhoneNumberChange(): void {
this.value = this.selectedCountry.dialCode + this.phone_number;
this.valueChange.emit(this.value);
}

public onCountrySelect(country: Country): void {
public onCountrySelect(country: Country, el): void {
this.selectedCountry = country;
if (this.phone_number.length > 0) {
this.value = this.selectedCountry.dialCode + this.phone_number;
this.valueChange.emit(this.value);
}
el.focus();
}

public onInputKeyPress(event): void {
Expand Down Expand Up @@ -72,5 +87,4 @@ export class NgxIntlTelInputComponent implements OnInit {
return e;
}
}

}
2 changes: 2 additions & 0 deletions src/resource/intl-tel-input.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
.intl-tel-input .country-list .divider {
padding-bottom: 5px;
margin-bottom: 5px;
background-color: transparent;
margin-top: 0;
border-bottom: 1px solid #CCC; }
.intl-tel-input .country-list .country {
padding: 5px 10px; }
Expand Down