Skip to content

Commit

Permalink
[bugfix] fix search value not clear (#2688)
Browse files Browse the repository at this point in the history
Co-authored-by: shown <[email protected]>
Co-authored-by: Calvin <[email protected]>
  • Loading branch information
3 people authored Sep 9, 2024
1 parent dab66d4 commit 4d9900b
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions web-app/src/app/layout/basic/widgets/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
OnDestroy,
Output
} from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { debounceTime, distinctUntilChanged, tap } from 'rxjs/operators';
import { debounceTime, distinctUntilChanged, tap, filter } from 'rxjs/operators';

import { Monitor } from '../../../pojo/Monitor';
import { MonitorService } from '../../../service/monitor.service';
Expand All @@ -38,12 +39,7 @@ import { MonitorService } from '../../../service/monitor.service';
/>
</nz-input-group>
<nz-autocomplete nzBackfill="false" nzDefaultActiveFirstOption #auto>
<nz-auto-option
*ngFor="let option of options"
[nzValue]="option.id"
[nzLabel]="option.name"
[routerLink]="['/monitors/' + option.id]"
>
<nz-auto-option *ngFor="let option of options" [nzValue]="option.id" [nzLabel]="option.name" (click)="onOptionSelect(option)">
<a>
{{ 'monitor.name' | i18n }} : {{ option.name }}
<span style="left:50% ; position: absolute;">{{ 'monitor.host' | i18n }} : {{ option.host }}</span>
Expand Down Expand Up @@ -79,10 +75,23 @@ export class HeaderSearchComponent implements AfterViewInit, OnDestroy {
}
@Output() readonly toggleChangeChange = new EventEmitter<boolean>();

constructor(private el: ElementRef<HTMLElement>, private cdr: ChangeDetectorRef, private monitorSvc: MonitorService) {}
constructor(
private router: Router,
private el: ElementRef<HTMLElement>,
private cdr: ChangeDetectorRef,
private monitorSvc: MonitorService
) {
this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe(() => {
this.resetSearch();
});
}

ngAfterViewInit(): void {
this.qIpt = this.el.nativeElement.querySelector('.ant-input') as HTMLInputElement;
this.initOptions();
}

initOptions() {
this.search$
.pipe(
debounceTime(500),
Expand Down Expand Up @@ -135,6 +144,22 @@ export class HeaderSearchComponent implements AfterViewInit, OnDestroy {
this.search$.next((ev.target as HTMLInputElement).value);
}

onOptionSelect(option: any) {
this.router.navigate([`/monitors/${option.id}`]);
this.resetSearch();
if (this.qIpt) {
this.qIpt.blur();
}
this.qBlur();
}

resetSearch() {
if (this.qIpt) {
this.qIpt!.value = '';
}
this.initOptions();
}

ngOnDestroy(): void {
this.search$.complete();
this.search$.unsubscribe();
Expand Down

0 comments on commit 4d9900b

Please sign in to comment.