1
- import { ChangeDetectionStrategy , Component , EventEmitter , Input , Output } from '@angular/core' ;
1
+ import { ChangeDetectionStrategy , Component , EventEmitter , Input , OnChanges , Output } from '@angular/core' ;
2
2
import { IconType } from '@hypertrace/assets-library' ;
3
+ import { SubscriptionLifecycle , TypedSimpleChanges } from '@hypertrace/common' ;
4
+ import { Subject } from 'rxjs' ;
5
+ import { debounceTime } from 'rxjs/operators' ;
3
6
import { IconSize } from '../icon/icon-size' ;
4
7
5
8
@Component ( {
6
9
selector : 'ht-search-box' ,
7
10
styleUrls : [ './search-box.component.scss' ] ,
8
11
changeDetection : ChangeDetectionStrategy . OnPush ,
12
+ providers : [ SubscriptionLifecycle ] ,
9
13
template : `
10
14
<div class="ht-search-box" [class.focused]="this.isFocused">
11
15
<ht-icon icon="${ IconType . Search } " size="${ IconSize . Medium } " class="icon" (click)="onSubmit()"></ht-icon>
@@ -29,28 +33,45 @@ import { IconSize } from '../icon/icon-size';
29
33
</div>
30
34
`
31
35
} )
32
- export class SearchBoxComponent {
36
+ export class SearchBoxComponent implements OnChanges {
33
37
@Input ( )
34
38
public placeholder : string = 'Search' ;
35
39
36
40
@Input ( )
37
41
public value : string = '' ;
38
42
43
+ @Input ( )
44
+ public debounceTime : number = 0 ;
45
+
39
46
@Output ( )
40
47
public readonly valueChange : EventEmitter < string > = new EventEmitter ( ) ;
41
48
42
49
@Output ( )
43
50
// tslint:disable-next-line:no-output-native
44
51
public readonly submit : EventEmitter < string > = new EventEmitter ( ) ;
45
52
53
+ public constructor ( private readonly subscriptionLifecycle : SubscriptionLifecycle ) { }
54
+
46
55
public isFocused : boolean = false ;
56
+ private readonly debouncedValueSubject : Subject < string > = new Subject ( ) ;
57
+
58
+ public ngOnChanges ( changes : TypedSimpleChanges < this> ) : void {
59
+ if ( changes . debounceTime ) {
60
+ this . subscriptionLifecycle . unsubscribe ( ) ;
61
+ this . subscriptionLifecycle . add (
62
+ this . debouncedValueSubject
63
+ . pipe ( debounceTime ( this . debounceTime ) )
64
+ . subscribe ( value => this . valueChange . emit ( value ) )
65
+ ) ;
66
+ }
67
+ }
47
68
48
69
public onSubmit ( ) : void {
49
70
this . submit . emit ( this . value ) ;
50
71
}
51
72
52
73
public onValueChange ( ) : void {
53
- this . valueChange . emit ( this . value ) ;
74
+ this . debouncedValueSubject . next ( this . value ) ;
54
75
}
55
76
56
77
public clearValue ( ) : void {
0 commit comments