Skip to content

Commit 9137fd9

Browse files
devversiontinayuangao
authored andcommitted
fix(autocomplete): allow number zero as value (#5364)
* fix(autocomplete): allow number zero as value * Currently the autocomplete does not properly display the selected options with the number zero as value. Fixes #5363. * Fix typo
1 parent d3af57d commit 9137fd9

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
373373

374374
private _setTriggerValue(value: any): void {
375375
const toDisplay = this.autocomplete.displayWith ? this.autocomplete.displayWith(value) : value;
376-
this._element.nativeElement.value = toDisplay || '';
376+
// Simply falling back to an empty string if the display value is falsy does not work properly.
377+
// The display value can also be the number zero and shouldn't fall back to an empty string.
378+
this._element.nativeElement.value = toDisplay != null ? toDisplay : '';
377379
}
378380

379381
/**

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe('MdAutocomplete', () => {
5353
AutocompleteWithoutForms,
5454
NgIfAutocomplete,
5555
AutocompleteWithNgModel,
56+
AutocompleteWithNumbers,
5657
AutocompleteWithOnPushDelay,
5758
AutocompleteWithNativeInput,
5859
AutocompleteWithoutPanel
@@ -1194,6 +1195,19 @@ describe('MdAutocomplete', () => {
11941195
});
11951196
}));
11961197

1198+
it('should display the number when the selected option is the number zero', async(() => {
1199+
const fixture = TestBed.createComponent(AutocompleteWithNumbers);
1200+
1201+
fixture.componentInstance.selectedNumber = 0;
1202+
fixture.detectChanges();
1203+
1204+
fixture.whenStable().then(() => {
1205+
const input = fixture.debugElement.query(By.css('input')).nativeElement;
1206+
1207+
expect(input.value).toBe('0');
1208+
});
1209+
}));
1210+
11971211
it('should work when input is wrapped in ngIf', async(() => {
11981212
const fixture = TestBed.createComponent(NgIfAutocomplete);
11991213
fixture.detectChanges();
@@ -1462,6 +1476,23 @@ class AutocompleteWithNgModel {
14621476

14631477
}
14641478

1479+
@Component({
1480+
template: `
1481+
<md-input-container>
1482+
<input mdInput placeholder="Number" [mdAutocomplete]="auto" [(ngModel)]="selectedNumber">
1483+
</md-input-container>
1484+
1485+
<md-autocomplete #auto="mdAutocomplete">
1486+
<md-option *ngFor="let number of numbers" [value]="number">
1487+
<span>{{ number }}</span>
1488+
</md-option>
1489+
</md-autocomplete>
1490+
`
1491+
})
1492+
class AutocompleteWithNumbers {
1493+
selectedNumber: number;
1494+
numbers = [0, 1, 2];
1495+
}
14651496

14661497
@Component({
14671498
changeDetection: ChangeDetectionStrategy.OnPush,

0 commit comments

Comments
 (0)