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 (No countries found) option #411

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Or this:
| selectedCountryISO | `<CountryISO>` | `None` | Set specific country on load. |
| separateDialCode | `boolean` | `false` | Visually separate dialcode into the drop down element. |
| countryChange | `<Country>` | `None` | Emits country value when the user selects a country from the dropdown. |
| noCountriesText | `string` | `'No Countries Found'` | Custom string to be displayed when searching for not found country |

## Supported Formats

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[isDisabled]="disabled"
>
<div class="iti__selected-flag dropdown-toggle" dropdownToggle>
<div class="iti__flag" [ngClass]="selectedCountry.flagClass || ''"></div>
<div class="iti__flag" [ngClass]="selectedCountry?.flagClass"></div>
<div *ngIf="separateDialCode" class="selected-dial-code">+{{ selectedCountry.dialCode }}</div>
<div class="iti__arrow"></div>
</div>
Expand Down Expand Up @@ -40,13 +40,17 @@
*ngFor="let country of allCountries"
(click)="onCountrySelect(country, focusable)"
[id]="country.htmlId"
[hidden]="noCountriesFound"
>
<div class="iti__flag-box">
<div class="iti__flag" [ngClass]="country.flagClass"></div>
</div>
<span class="iti__country-name">{{ country.name }}</span>
<span class="iti__dial-code">+{{ country.dialCode }}</span>
</li>
<li class="iti__country" [hidden]="!noCountriesFound">
{{ noCountriesText }}
</li>
</ul>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export class NgxIntlTelInputComponent implements OnInit, OnChanges {
@Input() separateDialCode = false;
separateDialCodeClass: string;

// take string input to be customized with project language
@Input() noCountriesText = 'No Countries Found';

@Output() readonly countryChange = new EventEmitter<Country>();

selectedCountry: Country = {
Expand All @@ -85,6 +88,10 @@ export class NgxIntlTelInputComponent implements OnInit, OnChanges {
disabled = false;
errors: Array<any> = ['Phone number is required.'];
countrySearchText = '';
// flag if no countries found
noCountriesFound: boolean = false;
// flag to do the settimeout if the view not ended yet
comingFromFalseFlag: boolean = false;

@ViewChild('countryList') countryList: ElementRef;

Expand Down Expand Up @@ -191,14 +198,31 @@ export class NgxIntlTelInputComponent implements OnInit, OnChanges {
});

if (country.length > 0) {
this.noCountriesFound = false;
const el = this.countryList.nativeElement.querySelector('#' + country[0].htmlId);
if (el) {
el.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'nearest',
});
if (!this.comingFromFalseFlag) {
el.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'nearest',
});
} else {
// added setTimeout to make scroll happen because the view is
// not done yet before setTimeout if this.noCountriesFound changed state from true to false
setTimeout(() => {
el.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'nearest',
});
}, 0);
this.comingFromFalseFlag = false;
}
}
} else {
this.noCountriesFound = true;
this.comingFromFalseFlag = true;
}

this.checkSeparateDialCodeStyle();
Expand Down
1 change: 1 addition & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ <h1>Test International Telephone Input Form</h1>
[numberFormat]="PhoneNumberFormat.National"
name="phone"
formControlName="phone"
[noCountriesText]="noFoundCountries"
>
</ngx-intl-tel-input>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class AppComponent {
phoneForm = new FormGroup({
phone: new FormControl(undefined, [Validators.required]),
});
noFoundCountries: string = 'No Countries Found'

changePreferredCountries() {
this.preferredCountries = [CountryISO.India, CountryISO.Canada];
Expand Down