|
|
@ -14,7 +14,18 @@ |
|
|
/// limitations under the License.
|
|
|
/// limitations under the License.
|
|
|
///
|
|
|
///
|
|
|
|
|
|
|
|
|
import { Component, ElementRef, EventEmitter, forwardRef, Input, OnInit, Output, ViewChild } from '@angular/core'; |
|
|
import { |
|
|
|
|
|
Component, |
|
|
|
|
|
ElementRef, |
|
|
|
|
|
EventEmitter, |
|
|
|
|
|
forwardRef, |
|
|
|
|
|
Input, |
|
|
|
|
|
OnChanges, |
|
|
|
|
|
OnInit, |
|
|
|
|
|
Output, |
|
|
|
|
|
SimpleChanges, |
|
|
|
|
|
ViewChild |
|
|
|
|
|
} from '@angular/core'; |
|
|
import { Country, CountryData } from '@shared/models/country.models'; |
|
|
import { Country, CountryData } from '@shared/models/country.models'; |
|
|
import { |
|
|
import { |
|
|
ControlValueAccessor, |
|
|
ControlValueAccessor, |
|
|
@ -23,14 +34,16 @@ import { |
|
|
NG_VALIDATORS, |
|
|
NG_VALIDATORS, |
|
|
NG_VALUE_ACCESSOR, |
|
|
NG_VALUE_ACCESSOR, |
|
|
ValidationErrors, |
|
|
ValidationErrors, |
|
|
Validator |
|
|
Validator, |
|
|
|
|
|
Validators |
|
|
} from '@angular/forms'; |
|
|
} from '@angular/forms'; |
|
|
import { isNotEmptyStr } from '@core/utils'; |
|
|
import { isNotEmptyStr, objectRequired } from '@core/utils'; |
|
|
import { Observable, of } from 'rxjs'; |
|
|
import { Observable, of } from 'rxjs'; |
|
|
import { debounceTime, distinctUntilChanged, map, share, switchMap, tap } from 'rxjs/operators'; |
|
|
import { debounceTime, distinctUntilChanged, map, share, switchMap, tap } from 'rxjs/operators'; |
|
|
import { SubscriptSizing, MatFormFieldAppearance } from '@angular/material/form-field'; |
|
|
import { MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field'; |
|
|
import { coerceBoolean } from '@shared/decorators/coercion'; |
|
|
import { coerceBoolean } from '@shared/decorators/coercion'; |
|
|
import { TranslateService } from '@ngx-translate/core'; |
|
|
import { TranslateService } from '@ngx-translate/core'; |
|
|
|
|
|
import { MatAutocompleteTrigger } from '@angular/material/autocomplete'; |
|
|
|
|
|
|
|
|
interface CountrySearchData extends Country { |
|
|
interface CountrySearchData extends Country { |
|
|
searchText?: string; |
|
|
searchText?: string; |
|
|
@ -39,6 +52,7 @@ interface CountrySearchData extends Country { |
|
|
@Component({ |
|
|
@Component({ |
|
|
selector: 'tb-country-autocomplete', |
|
|
selector: 'tb-country-autocomplete', |
|
|
templateUrl: 'country-autocomplete.component.html', |
|
|
templateUrl: 'country-autocomplete.component.html', |
|
|
|
|
|
styleUrls: ['./country-autocomplete.component.scss'], |
|
|
providers: [ |
|
|
providers: [ |
|
|
CountryData, |
|
|
CountryData, |
|
|
{ |
|
|
{ |
|
|
@ -53,7 +67,7 @@ interface CountrySearchData extends Country { |
|
|
} |
|
|
} |
|
|
] |
|
|
] |
|
|
}) |
|
|
}) |
|
|
export class CountryAutocompleteComponent implements OnInit, ControlValueAccessor, Validator { |
|
|
export class CountryAutocompleteComponent implements OnInit, OnChanges, ControlValueAccessor, Validator { |
|
|
|
|
|
|
|
|
@Input() |
|
|
@Input() |
|
|
labelText = this.translate.instant('contact.country'); |
|
|
labelText = this.translate.instant('contact.country'); |
|
|
@ -61,6 +75,9 @@ export class CountryAutocompleteComponent implements OnInit, ControlValueAccesso |
|
|
@Input() |
|
|
@Input() |
|
|
requiredText = this.translate.instant('contact.country-required'); |
|
|
requiredText = this.translate.instant('contact.country-required'); |
|
|
|
|
|
|
|
|
|
|
|
@Input() |
|
|
|
|
|
objectRequiredText = this.translate.instant('contact.country-object-required'); |
|
|
|
|
|
|
|
|
@Input() |
|
|
@Input() |
|
|
autocompleteHint: string; |
|
|
autocompleteHint: string; |
|
|
|
|
|
|
|
|
@ -79,6 +96,8 @@ export class CountryAutocompleteComponent implements OnInit, ControlValueAccesso |
|
|
|
|
|
|
|
|
@ViewChild('countryInput', {static: true}) countryInput: ElementRef; |
|
|
@ViewChild('countryInput', {static: true}) countryInput: ElementRef; |
|
|
|
|
|
|
|
|
|
|
|
@ViewChild('autocompleteTrigger') autocompleteTrigger: MatAutocompleteTrigger; |
|
|
|
|
|
|
|
|
@Output() |
|
|
@Output() |
|
|
selectCountryCode = new EventEmitter<string>(); |
|
|
selectCountryCode = new EventEmitter<string>(); |
|
|
|
|
|
|
|
|
@ -103,10 +122,24 @@ export class CountryAutocompleteComponent implements OnInit, ControlValueAccesso |
|
|
private countryData: CountryData, |
|
|
private countryData: CountryData, |
|
|
private translate: TranslateService) { |
|
|
private translate: TranslateService) { |
|
|
this.countryFormGroup = this.fb.group({ |
|
|
this.countryFormGroup = this.fb.group({ |
|
|
country: [''] |
|
|
country: ['', objectRequired()] |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) { |
|
|
|
|
|
if (changes.required) { |
|
|
|
|
|
const requiredChanges = changes.required; |
|
|
|
|
|
if (requiredChanges.currentValue !== requiredChanges.previousValue) { |
|
|
|
|
|
if (requiredChanges.currentValue) { |
|
|
|
|
|
this.countryFormGroup.get('country').addValidators(Validators.required); |
|
|
|
|
|
} else { |
|
|
|
|
|
this.countryFormGroup.get('country').removeValidators(Validators.required); |
|
|
|
|
|
} |
|
|
|
|
|
this.countryFormGroup.get('country').updateValueAndValidity(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
ngOnInit(): void { |
|
|
ngOnInit(): void { |
|
|
this.filteredCountries = this.countryFormGroup.get('country').valueChanges.pipe( |
|
|
this.filteredCountries = this.countryFormGroup.get('country').valueChanges.pipe( |
|
|
debounceTime(150), |
|
|
debounceTime(150), |
|
|
@ -163,7 +196,7 @@ export class CountryAutocompleteComponent implements OnInit, ControlValueAccesso |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
displayCountryFn(country?: Country): string | undefined { |
|
|
displayCountryFn(country?: Country): string | undefined { |
|
|
return country ? `${country.flag} ${country.name}` : undefined; |
|
|
return country ? country.name : undefined; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
onFocus() { |
|
|
onFocus() { |
|
|
@ -173,6 +206,21 @@ export class CountryAutocompleteComponent implements OnInit, ControlValueAccesso |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
checkInputAndAutoSelect() { |
|
|
|
|
|
const control = this.countryFormGroup.get('country'); |
|
|
|
|
|
const value = control.value; |
|
|
|
|
|
|
|
|
|
|
|
if (value && typeof value === 'string') { |
|
|
|
|
|
const foundCountry = this.fetchCountries(value); |
|
|
|
|
|
if (foundCountry.length === 1) { |
|
|
|
|
|
control.setValue(foundCountry[0]); |
|
|
|
|
|
this.autocompleteTrigger?.closePanel(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.onTouched(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
textIsNotEmpty(text: string): boolean { |
|
|
textIsNotEmpty(text: string): boolean { |
|
|
return (text && text.length > 0); |
|
|
return (text && text.length > 0); |
|
|
} |
|
|
} |
|
|
|