1- import { ChangeDetectionStrategy , Component , EventEmitter , Input , OnChanges , Output } from '@angular/core' ;
1+ import { ChangeDetectionStrategy , Component , EventEmitter , Input , OnChanges , OnInit , Output } from '@angular/core' ;
22import { IconType } from '@hypertrace/assets-library' ;
33import { SubscriptionLifecycle , TypedSimpleChanges } from '@hypertrace/common' ;
44import { Subject } from 'rxjs' ;
@@ -33,15 +33,15 @@ import { IconSize } from '../icon/icon-size';
3333 </div>
3434 `
3535} )
36- export class SearchBoxComponent implements OnChanges {
36+ export class SearchBoxComponent implements OnInit , OnChanges {
3737 @Input ( )
3838 public placeholder : string = 'Search' ;
3939
4040 @Input ( )
4141 public value : string = '' ;
4242
4343 @Input ( )
44- public debounceTime : number = 0 ;
44+ public debounceTime ? : number ;
4545
4646 @Output ( )
4747 public readonly valueChange : EventEmitter < string > = new EventEmitter ( ) ;
@@ -55,14 +55,13 @@ export class SearchBoxComponent implements OnChanges {
5555 public isFocused : boolean = false ;
5656 private readonly debouncedValueSubject : Subject < string > = new Subject ( ) ;
5757
58+ public ngOnInit ( ) : void {
59+ this . setDebouncedSubscription ( ) ;
60+ }
61+
5862 public ngOnChanges ( changes : TypedSimpleChanges < this> ) : void {
5963 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- ) ;
64+ this . setDebouncedSubscription ( ) ;
6665 }
6766 }
6867
@@ -82,4 +81,13 @@ export class SearchBoxComponent implements OnChanges {
8281 this . value = '' ;
8382 this . onValueChange ( ) ;
8483 }
84+
85+ private setDebouncedSubscription ( ) : void {
86+ this . subscriptionLifecycle . unsubscribe ( ) ;
87+ this . subscriptionLifecycle . add (
88+ this . debouncedValueSubject
89+ . pipe ( debounceTime ( this . debounceTime ?? 0 ) )
90+ . subscribe ( value => this . valueChange . emit ( value ) )
91+ ) ;
92+ }
8593}
0 commit comments