1
1
import {
2
2
booleanAttribute ,
3
3
ChangeDetectionStrategy ,
4
+ ChangeDetectorRef ,
4
5
Component ,
5
6
ElementRef ,
6
7
forwardRef ,
8
+ inject ,
7
9
Input ,
8
10
OnDestroy ,
9
11
OnInit ,
@@ -30,21 +32,42 @@ export const SEARCH_CONTROL_VALUE_ACCESSOR = {
30
32
providers : [ SEARCH_CONTROL_VALUE_ACCESSOR ] ,
31
33
} )
32
34
export class SearchComponent implements OnInit , OnDestroy , ControlValueAccessor {
33
- @Input ( ) value : string = '' ;
34
35
@Input ( ) placeholder : string = 'Search...' ;
35
36
@Input ( ) focusKey : string = '/' ;
36
37
@Input ( { transform : booleanAttribute } ) focusKeyEnabled : boolean = true ;
37
- @Input ( { transform : booleanAttribute } ) disabled : boolean = false ;
38
38
@Input ( { transform : booleanAttribute } ) slim : boolean = false ;
39
39
@ViewChild ( 'input' ) _inputElement ! : ElementRef < HTMLInputElement > ;
40
40
41
41
protected _onChange : ( value : string ) => void = noop ;
42
42
protected _onTouched : ( ) => void = noop ;
43
43
44
+ private _value : string = '' ;
45
+ private _disabled : boolean = false ;
46
+
47
+ @Input ( )
48
+ get value ( ) : string {
49
+ return this . _value ;
50
+ }
51
+ set value ( value : string ) {
52
+ this . _value = value ;
53
+ this . _changeDetectorRef . markForCheck ( ) ;
54
+ }
55
+
56
+ @Input ( { transform : booleanAttribute } )
57
+ get disabled ( ) : boolean {
58
+ return this . _disabled ;
59
+ }
60
+ set disabled ( disabled : boolean ) {
61
+ this . _disabled = disabled ;
62
+ this . _changeDetectorRef . markForCheck ( ) ;
63
+ }
64
+
65
+ private _changeDetectorRef = inject ( ChangeDetectorRef ) ;
44
66
private searchSubject : Subject < string > = new Subject ( ) ;
45
67
46
68
ngOnInit ( ) : void {
47
69
this . searchSubject . pipe ( debounceTime ( 200 ) ) . subscribe ( ( value ) => {
70
+ this . value = value ;
48
71
this . _onChange ( value ) ;
49
72
} ) ;
50
73
@@ -78,12 +101,18 @@ export class SearchComponent implements OnInit, OnDestroy, ControlValueAccessor
78
101
}
79
102
80
103
protected _onBlur ( ) : void {
81
- Promise . resolve ( ) . then ( ( ) => this . _onTouched ( ) ) ;
104
+ Promise . resolve ( ) . then ( ( ) => {
105
+ this . _onTouched ( ) ;
106
+ this . _changeDetectorRef . markForCheck ( ) ;
107
+ } ) ;
108
+ }
109
+
110
+ protected _onInputEvent ( ) : void {
111
+ this . searchSubject . next ( this . _inputElement . nativeElement . value ) ;
82
112
}
83
113
84
114
protected _onInteractionEvent ( event : Event ) : void {
85
115
event . stopPropagation ( ) ;
86
- this . searchSubject . next ( this . _inputElement . nativeElement . value ) ;
87
116
}
88
117
89
118
protected _onKeyEvent ( event : KeyboardEvent ) : void {
0 commit comments