Skip to content

fix(select): unable to programmatically select falsy values #4868

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ describe('MdSelect', () => {
BasicSelectInitiallyHidden,
BasicSelectNoPlaceholder,
BasicSelectWithTheming,
ResetValuesSelect
ResetValuesSelect,
FalsyValueSelect
],
providers: [
{provide: OverlayContainer, useFactory: () => {
Expand Down Expand Up @@ -621,6 +622,22 @@ describe('MdSelect', () => {
.toContain('*', `Expected placeholder to have an asterisk, as control was required.`);
});

it('should be able to programmatically select a falsy option', () => {
fixture.destroy();

const falsyFixture = TestBed.createComponent(FalsyValueSelect);

falsyFixture.detectChanges();
falsyFixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click();
falsyFixture.componentInstance.control.setValue(0);
falsyFixture.detectChanges();

expect(falsyFixture.componentInstance.options.first.selected)
.toBe(true, 'Expected first option to be selected');
expect(overlayContainerElement.querySelectorAll('md-option')[0].classList)
.toContain('mat-selected', 'Expected first option to be selected');
});

});

describe('disabled behavior', () => {
Expand Down Expand Up @@ -2504,3 +2521,20 @@ class ResetValuesSelect {

@ViewChild(MdSelect) select: MdSelect;
}


@Component({
template: `
<md-select [formControl]="control">
<md-option *ngFor="let food of foods" [value]="food.value">{{ food.viewValue }}</md-option>
</md-select>
`
})
class FalsyValueSelect {
foods: any[] = [
{ value: 0, viewValue: 'Steak' },
{ value: 1, viewValue: 'Pizza' },
];
control = new FormControl();
@ViewChildren(MdOption) options: QueryList<MdOption>;
}
4 changes: 3 additions & 1 deletion src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,9 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
*/
private _selectValue(value: any): MdOption {
let optionsArray = this.options.toArray();
let correspondingOption = optionsArray.find(option => option.value && option.value === value);
let correspondingOption = optionsArray.find(option => {
return option.value != null && option.value === value;
});

if (correspondingOption) {
correspondingOption.select();
Expand Down