Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autocomplete): allow number zero as value #5364

Merged
merged 2 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: fallback -> fall back.

this._element.nativeElement.value = toDisplay != null ? toDisplay : '';
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('MdAutocomplete', () => {
AutocompleteWithoutForms,
NgIfAutocomplete,
AutocompleteWithNgModel,
AutocompleteWithNumbers,
AutocompleteWithOnPushDelay,
AutocompleteWithNativeInput,
AutocompleteWithoutPanel
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -1404,6 +1418,23 @@ class AutocompleteWithNgModel {

}

@Component({
template: `
<md-input-container>
<input mdInput placeholder="Number" [mdAutocomplete]="auto" [(ngModel)]="selectedNumber">
</md-input-container>

<md-autocomplete #auto="mdAutocomplete">
<md-option *ngFor="let number of numbers" [value]="number">
<span>{{ number }}</span>
</md-option>
</md-autocomplete>
`
})
class AutocompleteWithNumbers {
selectedNumber: number;
numbers = [0, 1, 2];
}

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
Expand Down