Skip to content

Commit 5af91f1

Browse files
sluFicodesSHENGXING LU
andauthored
autofind phone number (#239)
Co-authored-by: SHENGXING LU <slu@SHENGXINGs-MacBook-Air.local>
1 parent 0d4399d commit 5af91f1

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/app/shared/billing-account-form/billing-account-form.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@
137137
</button>
138138
@if(prefixCheck==true){
139139
<div class="absolute bottom-12 right-0 z-20 max-h-48 w-full bg-white divide-y divide-gray-100 dark:bg-secondary-300 dark:border-secondary-200 dark:text-white rounded-lg shadow overflow-y-auto">
140-
<ul class="py-2 text-sm text-gray-700" aria-labelledby="dropdown-phone-button">
140+
<ul #prefixList class="py-2 text-sm text-gray-700" aria-labelledby="dropdown-phone-button">
141141
<li>
142142
@for(pref of prefixes;track pref.code){
143-
<button type="button" (click)="selectPrefix(pref)" class="inline-flex w-full px-4 py-2 text-sm text-gray-700 dark:text-white hover:bg-gray-100 dark:hover:bg-secondary-200" role="menuitem">
143+
<button data-cy="phoneCountries" [attr.data-country]="pref.country" type="button" (click)="selectPrefix(pref)" class="inline-flex w-full px-4 py-2 text-sm text-gray-700 dark:text-white hover:bg-gray-100 dark:hover:bg-secondary-200" role="menuitem">
144144
<div class="inline-flex items-center">
145145
{{pref.text}}
146146
</div>

src/app/shared/billing-account-form/billing-account-form.component.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ export class BillingAccountFormComponent implements OnInit, OnDestroy {
5151
telephoneNumber: new FormControl('', [Validators.required, Validators.min(0)]),
5252
telephoneType: new FormControl('Mobile')
5353
});
54-
prefixes: any[] = phoneNumbers;
54+
prefixes: any[] = [...phoneNumbers].sort((a, b) => this.getCountryName(a.text).localeCompare(this.getCountryName(b.text)));
5555
countries: any[] = countries;
5656
phonePrefix: any = phoneNumbers[0];
57+
@ViewChild('prefixList') prefixListRef!: ElementRef;
5758
prefixCheck: boolean = false;
5859
toastVisibility: boolean = false;
5960

@@ -404,6 +405,22 @@ export class BillingAccountFormComponent implements OnInit, OnDestroy {
404405
}
405406
}
406407

408+
getCountryName(text: string): string {
409+
const match = text.match(/\+\d+\s+(.+)$/);
410+
return match ? match[1] : text;
411+
}
412+
413+
@HostListener('keydown', ['$event'])
414+
onKeydown(event: KeyboardEvent) {
415+
if (!this.prefixCheck) return;
416+
const letter = event.key.toLowerCase();
417+
if (!/^[a-z]$/.test(letter)) return;
418+
const match = this.prefixes.find(p => this.getCountryName(p.text).toLowerCase().startsWith(letter));
419+
if (!match || !this.prefixListRef) return;
420+
const btn = this.prefixListRef.nativeElement.querySelector(`[data-country="${match.country}"]`);
421+
if (btn) btn.scrollIntoView({ block: 'nearest' });
422+
}
423+
407424
selectPrefix(pref:any) {
408425
console.log(pref)
409426
this.prefixCheck = false;

0 commit comments

Comments
 (0)