From 4d9900b56b87b2f1f8917c5b243bd5a9cd2455eb Mon Sep 17 00:00:00 2001 From: aias00 Date: Mon, 9 Sep 2024 23:00:49 +0800 Subject: [PATCH] [bugfix] fix search value not clear (#2688) Co-authored-by: shown Co-authored-by: Calvin --- .../layout/basic/widgets/search.component.ts | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/web-app/src/app/layout/basic/widgets/search.component.ts b/web-app/src/app/layout/basic/widgets/search.component.ts index a1b7c2a9f2f..05d9a972873 100644 --- a/web-app/src/app/layout/basic/widgets/search.component.ts +++ b/web-app/src/app/layout/basic/widgets/search.component.ts @@ -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'; @@ -38,12 +39,7 @@ import { MonitorService } from '../../../service/monitor.service'; /> - + {{ 'monitor.name' | i18n }} : {{ option.name }} {{ 'monitor.host' | i18n }} : {{ option.host }} @@ -79,10 +75,23 @@ export class HeaderSearchComponent implements AfterViewInit, OnDestroy { } @Output() readonly toggleChangeChange = new EventEmitter(); - constructor(private el: ElementRef, private cdr: ChangeDetectorRef, private monitorSvc: MonitorService) {} + constructor( + private router: Router, + private el: ElementRef, + 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), @@ -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();