From a64a1c26cc58e75153f51f8bf317675e88779647 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Mon, 26 Jun 2017 19:01:20 +0200 Subject: [PATCH 1/2] 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. --- src/lib/autocomplete/autocomplete-trigger.ts | 4 ++- src/lib/autocomplete/autocomplete.spec.ts | 31 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/lib/autocomplete/autocomplete-trigger.ts b/src/lib/autocomplete/autocomplete-trigger.ts index 47816e0ba66b..11099f2fba68 100644 --- a/src/lib/autocomplete/autocomplete-trigger.ts +++ b/src/lib/autocomplete/autocomplete-trigger.ts @@ -358,7 +358,9 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy { private _setTriggerValue(value: any): void { const toDisplay = this.autocomplete.displayWith ? this.autocomplete.displayWith(value) : value; - this._element.nativeElement.value = toDisplay || ''; + // Simply falling back to an empty string if the display value is falsy does not work properly. + // The display value can also be the number zero and shouldn't fallback to an empty string. + this._element.nativeElement.value = toDisplay != null ? toDisplay : ''; } /** diff --git a/src/lib/autocomplete/autocomplete.spec.ts b/src/lib/autocomplete/autocomplete.spec.ts index 699b1f6bd568..336968f2f780 100644 --- a/src/lib/autocomplete/autocomplete.spec.ts +++ b/src/lib/autocomplete/autocomplete.spec.ts @@ -53,6 +53,7 @@ describe('MdAutocomplete', () => { AutocompleteWithoutForms, NgIfAutocomplete, AutocompleteWithNgModel, + AutocompleteWithNumbers, AutocompleteWithOnPushDelay, AutocompleteWithNativeInput, AutocompleteWithoutPanel @@ -1136,6 +1137,19 @@ describe('MdAutocomplete', () => { }); })); + it('should display the number when the selected option is the number zero', async(() => { + const fixture = TestBed.createComponent(AutocompleteWithNumbers); + + fixture.componentInstance.selectedNumber = 0; + fixture.detectChanges(); + + fixture.whenStable().then(() => { + const input = fixture.debugElement.query(By.css('input')).nativeElement; + + expect(input.value).toBe('0'); + }); + })); + it('should work when input is wrapped in ngIf', async(() => { const fixture = TestBed.createComponent(NgIfAutocomplete); fixture.detectChanges(); @@ -1404,6 +1418,23 @@ class AutocompleteWithNgModel { } +@Component({ + template: ` + + + + + + + {{ number }} + + + ` +}) +class AutocompleteWithNumbers { + selectedNumber: number; + numbers = [0, 1, 2]; +} @Component({ changeDetection: ChangeDetectionStrategy.OnPush, From a98a8c1caece1e33d3f92477a66ffe0d761f7bd3 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Mon, 26 Jun 2017 19:14:19 +0200 Subject: [PATCH 2/2] Fix typo --- src/lib/autocomplete/autocomplete-trigger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/autocomplete/autocomplete-trigger.ts b/src/lib/autocomplete/autocomplete-trigger.ts index 11099f2fba68..3d5fa4d7a577 100644 --- a/src/lib/autocomplete/autocomplete-trigger.ts +++ b/src/lib/autocomplete/autocomplete-trigger.ts @@ -359,7 +359,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy { private _setTriggerValue(value: any): void { const toDisplay = this.autocomplete.displayWith ? this.autocomplete.displayWith(value) : value; // Simply falling back to an empty string if the display value is falsy does not work properly. - // The display value can also be the number zero and shouldn't fallback to an empty string. + // The display value can also be the number zero and shouldn't fall back to an empty string. this._element.nativeElement.value = toDisplay != null ? toDisplay : ''; }