Skip to content

Commit

Permalink
Fix search result click event
Browse files Browse the repository at this point in the history
  • Loading branch information
lsfgrd committed Oct 26, 2020
1 parent 89c1071 commit 1cf88eb
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
import { Component, OnInit, ViewChild } from '@angular/core';
import { Component, ViewChild } from '@angular/core';
import { NavigationEnd, Router, RouterEvent } from '@angular/router';
import { filter, take, tap } from 'rxjs/operators';
import { MenuComponent } from './components/menu/menu.component';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
export class AppComponent {
@ViewChild(MenuComponent, {static: false}) theMenu;
searchString = '';
savedSearchString = '';

ngOnInit() {
}

openSidebar() {
this.theMenu.openSidebar();
}

constructor(private router: Router) {}

onSearchChanged(searchString: string) {
this.searchString = searchString;
}

saveSearchString() {
// if a search result is clicked, give time to change route
setTimeout(() => {
this.savedSearchString = this.searchString;
this.searchString = null;
}, 100);
// this method needs to wait the route to change before setting the searchString to null

this.router.events
.pipe(
filter((event: RouterEvent) => event instanceof NavigationEnd), // waits for NavigationEnd event
take(1), // takes the first NavigationEnd event
tap((event: RouterEvent) => {
this.savedSearchString = this.searchString;
this.searchString = null;
})
)
.subscribe();
}

restoreSearchString() {
Expand Down

0 comments on commit 1cf88eb

Please sign in to comment.