1
- import { Component , EventEmitter , Input , OnChanges , OnInit , Output } from '@angular/core' ;
1
+ import { Component , EventEmitter , Input , OnInit , Output } from '@angular/core' ;
2
2
import { IdAndName , Service } from "../../../abstract/service" ;
3
3
import { catchError , debounceTime , distinctUntilChanged , map , merge , Observable , of , OperatorFunction , Subject , switchMap , tap } from "rxjs" ;
4
4
import { fromPromise } from "rxjs/internal/observable/innerFrom" ;
@@ -12,10 +12,11 @@ export class FormAutocompleteComponent implements OnInit {
12
12
13
13
@Input ( ) name ! : string ;
14
14
@Input ( ) disabled : boolean = false ;
15
- @Input ( ) data ?: string = undefined ;
15
+ @Input ( ) data ?: string | number = undefined ;
16
16
@Input ( ) service ! : Service < any > ;
17
- @Input ( ) useName : boolean = false ;
17
+ @Input ( ) useTilde : boolean = false ;
18
18
@Output ( ) dataChange = new EventEmitter < string > ( ) ;
19
+ @Output ( ) dataChangeId = new EventEmitter < number > ( ) ;
19
20
@Output ( ) enter = new EventEmitter < void > ( ) ;
20
21
21
22
focus$ = new Subject < string > ( ) ;
@@ -53,7 +54,7 @@ export class FormAutocompleteComponent implements OnInit {
53
54
54
55
async ngOnInit ( ) : Promise < void > {
55
56
if ( this . data ) {
56
- this . selected = await this . service . getWithIdAndName ( ( this . useName ? '~' : '' ) + this . data ) ;
57
+ this . selected = await this . service . getWithIdAndName ( ( this . useTilde ? '~' : '' ) + this . data ) ;
57
58
}
58
59
}
59
60
@@ -62,12 +63,15 @@ export class FormAutocompleteComponent implements OnInit {
62
63
return ;
63
64
}
64
65
65
- const value = this . useName ? this . selected . name : this . selected . id ;
66
- if ( ! value ) {
67
- return ;
66
+ if ( this . dataChange . observed ) {
67
+ if ( this . selected . name ) {
68
+ this . dataChange . emit ( this . selected . name ) ;
69
+ }
70
+ } else if ( this . dataChangeId . observed ) {
71
+ if ( this . selected . id ) {
72
+ this . dataChangeId . emit ( parseInt ( this . selected . id ) ) ;
73
+ }
68
74
}
69
-
70
- this . dataChange . emit ( value ) ;
71
75
}
72
76
73
77
}
0 commit comments