1
- import { Component , ElementRef , EventEmitter , Input , Output , ViewChild , ViewEncapsulation } from '@angular/core' ;
1
+ import { ChangeDetectorRef , Component , ElementRef , EventEmitter , Input , Output , ViewChild } from '@angular/core' ;
2
2
import { Injector } from '@angular/core' ;
3
3
import { SafeHtml } from '@angular/platform-browser' ;
4
4
import { TooltipDirective } from 'ngx-bootstrap/tooltip' ;
5
+ import { TypeaheadDirective } from 'ngx-bootstrap/typeahead' ;
5
6
import { debounceTime } from 'rxjs/operators' ;
6
7
import { Subject } from 'rxjs/Subject' ;
8
+ import { setTimeout } from 'timers' ;
7
9
8
10
import { BaseComponent } from './../../base/base-component/base-component.component' ;
9
11
import { SelectInputConfig } from './select-input.config' ;
@@ -17,13 +19,17 @@ export class SelectInputComponent extends BaseComponent {
17
19
18
20
@ViewChild ( 'tooltip' )
19
21
tooltip : TooltipDirective ;
22
+ @ViewChild ( 'typeahead' )
23
+ typeahead : TypeaheadDirective ;
20
24
@ViewChild ( 'inputElement' )
21
25
inputElement : ElementRef ;
22
26
23
27
@Input ( )
24
28
debounceTime ?: number ;
25
29
@Output ( )
26
30
onChangeInputValue : EventEmitter < string > = new EventEmitter < string > ( ) ;
31
+ @Output ( )
32
+ onInputFocus : EventEmitter < string > = new EventEmitter < string > ( ) ;
27
33
@Input ( )
28
34
labelClass ?= 'control-label' ;
29
35
@Input ( )
@@ -55,10 +61,16 @@ export class SelectInputComponent extends BaseComponent {
55
61
@Input ( )
56
62
width : string = null ;
57
63
@Input ( )
58
- set items ( items : any [ ] ) {
64
+ dataSource : Subject < any [ ] > ;
65
+ @Input ( )
66
+ set items ( items : any ) {
59
67
this . _items = items ;
68
+ this . resizeList ( ) ;
60
69
}
61
- get items ( ) {
70
+ get items ( ) : any {
71
+ if ( this . dataSource ) {
72
+ return this . dataSource ;
73
+ }
62
74
return this . _items ;
63
75
}
64
76
@Input ( )
@@ -67,6 +79,7 @@ export class SelectInputComponent extends BaseComponent {
67
79
if ( this . _textValue === '' ) {
68
80
this . value = null ;
69
81
}
82
+ this . debouncer$ . next ( this . textValue ) ;
70
83
}
71
84
get textValue ( ) {
72
85
return this . _textValue ;
@@ -80,7 +93,8 @@ export class SelectInputComponent extends BaseComponent {
80
93
private _showMe = false ;
81
94
82
95
constructor (
83
- public injector : Injector
96
+ public injector : Injector ,
97
+ public changeDetectorRef : ChangeDetectorRef
84
98
) {
85
99
super ( injector ) ;
86
100
this . debouncer$ = new Subject < string > ( ) ;
@@ -105,6 +119,9 @@ export class SelectInputComponent extends BaseComponent {
105
119
this . debouncer$ . pipe ( debounceTime ( this . debounceTime ) )
106
120
. subscribe ( ( value : string ) => this . onChangeInputValue . emit ( value ) ) ;
107
121
}
122
+ inputFocus ( value : string ) {
123
+ this . onInputFocus . emit ( value ) ;
124
+ }
108
125
get inputReadonly ( ) {
109
126
return this . onChangeInputValue . observers && this . onChangeInputValue . observers . length === 0 ;
110
127
}
@@ -123,7 +140,6 @@ export class SelectInputComponent extends BaseComponent {
123
140
// this.textValue = '';
124
141
this . model = null ;
125
142
}
126
- this . debouncer$ . next ( this . textValue ) ;
127
143
this . modelChange . emit ( this . model ) ;
128
144
}
129
145
onTypeaheadNoResults ( typeaheadNoResults : boolean ) {
@@ -135,8 +151,10 @@ export class SelectInputComponent extends BaseComponent {
135
151
if ( this . hardValue ) {
136
152
this . value = this . hardValue ;
137
153
}
138
- this . value = this . value ;
139
154
super . init ( ) ;
155
+ if ( ! this . textValue ) {
156
+ this . value = this . value ;
157
+ }
140
158
}
141
159
getTitle ( item : any ) : SafeHtml | string {
142
160
if ( item && item [ this . titleField ] ) {
@@ -160,4 +178,26 @@ export class SelectInputComponent extends BaseComponent {
160
178
}
161
179
return '' ;
162
180
}
181
+ loading ( status : boolean ) {
182
+ if ( ! status ) {
183
+ this . resizeList ( ) ;
184
+ }
185
+ }
186
+ resizeList ( ) {
187
+ setTimeout ( ( ) => {
188
+ if ( this . changeDetectorRef ) {
189
+ this . changeDetectorRef . detectChanges ( ) ;
190
+ if ( this . typeahead && this . typeahead . _container ) {
191
+ if ( this . typeahead . _container . element . nativeElement . children [ 0 ] ) {
192
+ const list : any = this . typeahead . _container . element . nativeElement . children [ 0 ] ;
193
+ if ( this . width === null ) {
194
+ list . style . width = this . inputElement . nativeElement . offsetWidth + 'px' ;
195
+ } else {
196
+ list . style . width = this . width ;
197
+ }
198
+ }
199
+ }
200
+ }
201
+ } , 1 ) ;
202
+ }
163
203
}
0 commit comments