@@ -17,11 +17,13 @@ import {
17
17
Host ,
18
18
Input ,
19
19
NgZone ,
20
+ OnChanges ,
20
21
OnDestroy ,
21
22
OnInit ,
22
23
Optional ,
23
24
Output ,
24
25
QueryList ,
26
+ SimpleChanges ,
25
27
TemplateRef ,
26
28
ViewChild ,
27
29
ViewChildren ,
@@ -44,6 +46,18 @@ export interface AutocompleteDataSourceItem {
44
46
45
47
export type AutocompleteDataSource = Array < AutocompleteDataSourceItem | string | number > ;
46
48
49
+ function normalizeDataSource ( value : AutocompleteDataSource ) : AutocompleteDataSourceItem [ ] {
50
+ return value ?. map ( item => {
51
+ if ( typeof item === 'number' || typeof item === 'string' ) {
52
+ return {
53
+ label : item . toString ( ) ,
54
+ value : item . toString ( )
55
+ } ;
56
+ }
57
+ return item ;
58
+ } ) ;
59
+ }
60
+
47
61
@Component ( {
48
62
selector : 'nz-autocomplete' ,
49
63
exportAs : 'nzAutocomplete' ,
@@ -74,19 +88,15 @@ export type AutocompleteDataSource = Array<AutocompleteDataSourceItem | string |
74
88
<ng-content></ng-content>
75
89
</ng-template>
76
90
<ng-template #optionsTemplate>
77
- <nz-auto-option
78
- *ngFor="let option of nzDataSource!"
79
- [nzValue]="option"
80
- [nzLabel]="option && $any(option).label ? $any(option).label : $any(option)"
81
- >
82
- {{ option && $any(option).label ? $any(option).label : $any(option) }}
91
+ <nz-auto-option *ngFor="let option of normalizedDataSource" [nzValue]="option.value" [nzLabel]="option.label">
92
+ {{ option.label }}
83
93
</nz-auto-option>
84
94
</ng-template>
85
95
</ng-template>
86
96
` ,
87
97
animations : [ slideMotion ]
88
98
} )
89
- export class NzAutocompleteComponent implements AfterContentInit , AfterViewInit , OnDestroy , OnInit {
99
+ export class NzAutocompleteComponent implements AfterContentInit , AfterViewInit , OnDestroy , OnInit , OnChanges {
90
100
static ngAcceptInputType_nzDefaultActiveFirstOption : BooleanInput ;
91
101
static ngAcceptInputType_nzBackfill : BooleanInput ;
92
102
@@ -104,6 +114,7 @@ export class NzAutocompleteComponent implements AfterContentInit, AfterViewInit,
104
114
isOpen : boolean = false ;
105
115
activeItem : NzAutocompleteOptionComponent | null = null ;
106
116
dir : Direction = 'ltr' ;
117
+ normalizedDataSource : AutocompleteDataSourceItem [ ] = [ ] ;
107
118
private destroy$ = new Subject < void > ( ) ;
108
119
animationStateChange = new EventEmitter < AnimationEvent > ( ) ;
109
120
@@ -160,6 +171,7 @@ export class NzAutocompleteComponent implements AfterContentInit, AfterViewInit,
160
171
@Optional ( ) private directionality : Directionality ,
161
172
@Host ( ) @Optional ( ) public noAnimation ?: NzNoAnimationDirective
162
173
) { }
174
+
163
175
ngOnInit ( ) : void {
164
176
this . directionality . change ?. pipe ( takeUntil ( this . destroy$ ) ) . subscribe ( ( direction : Direction ) => {
165
177
this . dir = direction ;
@@ -169,6 +181,13 @@ export class NzAutocompleteComponent implements AfterContentInit, AfterViewInit,
169
181
this . dir = this . directionality . value ;
170
182
}
171
183
184
+ ngOnChanges ( changes : SimpleChanges ) : void {
185
+ const { nzDataSource } = changes ;
186
+ if ( nzDataSource ) {
187
+ this . normalizedDataSource = normalizeDataSource ( nzDataSource . currentValue ) ;
188
+ }
189
+ }
190
+
172
191
onAnimationEvent ( event : AnimationEvent ) : void {
173
192
this . animationStateChange . emit ( event ) ;
174
193
}
0 commit comments