Skip to content

Commit 0a312e3

Browse files
fix(module:autocomplete): fix the wrong value of internal nz-auto-option (#7907)
1 parent 4f63198 commit 0a312e3

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

components/auto-complete/autocomplete.component.ts

+26-7
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import {
1717
Host,
1818
Input,
1919
NgZone,
20+
OnChanges,
2021
OnDestroy,
2122
OnInit,
2223
Optional,
2324
Output,
2425
QueryList,
26+
SimpleChanges,
2527
TemplateRef,
2628
ViewChild,
2729
ViewChildren,
@@ -44,6 +46,18 @@ export interface AutocompleteDataSourceItem {
4446

4547
export type AutocompleteDataSource = Array<AutocompleteDataSourceItem | string | number>;
4648

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+
4761
@Component({
4862
selector: 'nz-autocomplete',
4963
exportAs: 'nzAutocomplete',
@@ -74,19 +88,15 @@ export type AutocompleteDataSource = Array<AutocompleteDataSourceItem | string |
7488
<ng-content></ng-content>
7589
</ng-template>
7690
<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 }}
8393
</nz-auto-option>
8494
</ng-template>
8595
</ng-template>
8696
`,
8797
animations: [slideMotion]
8898
})
89-
export class NzAutocompleteComponent implements AfterContentInit, AfterViewInit, OnDestroy, OnInit {
99+
export class NzAutocompleteComponent implements AfterContentInit, AfterViewInit, OnDestroy, OnInit, OnChanges {
90100
static ngAcceptInputType_nzDefaultActiveFirstOption: BooleanInput;
91101
static ngAcceptInputType_nzBackfill: BooleanInput;
92102

@@ -104,6 +114,7 @@ export class NzAutocompleteComponent implements AfterContentInit, AfterViewInit,
104114
isOpen: boolean = false;
105115
activeItem: NzAutocompleteOptionComponent | null = null;
106116
dir: Direction = 'ltr';
117+
normalizedDataSource: AutocompleteDataSourceItem[] = [];
107118
private destroy$ = new Subject<void>();
108119
animationStateChange = new EventEmitter<AnimationEvent>();
109120

@@ -160,6 +171,7 @@ export class NzAutocompleteComponent implements AfterContentInit, AfterViewInit,
160171
@Optional() private directionality: Directionality,
161172
@Host() @Optional() public noAnimation?: NzNoAnimationDirective
162173
) {}
174+
163175
ngOnInit(): void {
164176
this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
165177
this.dir = direction;
@@ -169,6 +181,13 @@ export class NzAutocompleteComponent implements AfterContentInit, AfterViewInit,
169181
this.dir = this.directionality.value;
170182
}
171183

184+
ngOnChanges(changes: SimpleChanges): void {
185+
const { nzDataSource } = changes;
186+
if (nzDataSource) {
187+
this.normalizedDataSource = normalizeDataSource(nzDataSource.currentValue);
188+
}
189+
}
190+
172191
onAnimationEvent(event: AnimationEvent): void {
173192
this.animationStateChange.emit(event);
174193
}

0 commit comments

Comments
 (0)