-import { Subject } from 'rxjs'
-import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
-import { ActivatedRoute, Router } from '@angular/router'
@Component({
selector: 'my-simple-search-input',
value = ''
inputShown: boolean
- private searchSubject = new Subject<string>()
-
- constructor (
- private router: Router,
- private route: ActivatedRoute
- ) {}
+ private hasAlreadySentSearch = false
ngOnInit () {
- this.searchSubject
- .pipe(
- debounceTime(400),
- distinctUntilChanged()
- )
- .subscribe(value => this.searchChanged.emit(value))
-
- this.searchSubject.next(this.value)
-
if (this.isInputShown()) this.showInput(false)
}
return
}
- this.searchChange()
+ this.sendSearch()
}
showInput (focus = true) {
this.hideInput()
}
- searchChange () {
- this.router.navigate([ './search' ], { relativeTo: this.route })
+ sendSearch () {
+ this.hasAlreadySentSearch = true
+ this.searchChanged.emit(this.value)
+ }
+
+ onResetFilter () {
+ this.value = ''
- this.searchSubject.next(this.value)
+ if (this.hasAlreadySentSearch) this.sendSearch()
}
}