Skip to content

Commit d734c60

Browse files
committed
fix(select): unable to programmatically select falsy values
Fixes not being able to set falsy values progammatically in `md-select`. Fixes #4854.
1 parent 12d6e96 commit d734c60

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/lib/select/select.spec.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ describe('MdSelect', () => {
5656
BasicSelectInitiallyHidden,
5757
BasicSelectNoPlaceholder,
5858
BasicSelectWithTheming,
59-
ResetValuesSelect
59+
ResetValuesSelect,
60+
FalsyValueSelect
6061
],
6162
providers: [
6263
{provide: OverlayContainer, useFactory: () => {
@@ -621,6 +622,22 @@ describe('MdSelect', () => {
621622
.toContain('*', `Expected placeholder to have an asterisk, as control was required.`);
622623
});
623624

625+
it('should be able to programmatically select a falsy option', () => {
626+
fixture.destroy();
627+
628+
const falsyFixture = TestBed.createComponent(FalsyValueSelect);
629+
630+
falsyFixture.detectChanges();
631+
falsyFixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click();
632+
falsyFixture.componentInstance.control.setValue(0);
633+
falsyFixture.detectChanges();
634+
635+
expect(falsyFixture.componentInstance.options.first.selected)
636+
.toBe(true, 'Expected first option to be selected');
637+
expect(overlayContainerElement.querySelectorAll('md-option')[0].classList)
638+
.toContain('mat-selected', 'Expected first option to be selected');
639+
});
640+
624641
});
625642

626643
describe('disabled behavior', () => {
@@ -2504,3 +2521,20 @@ class ResetValuesSelect {
25042521

25052522
@ViewChild(MdSelect) select: MdSelect;
25062523
}
2524+
2525+
2526+
@Component({
2527+
template: `
2528+
<md-select [formControl]="control">
2529+
<md-option *ngFor="let food of foods" [value]="food.value">{{ food.viewValue }}</md-option>
2530+
</md-select>
2531+
`
2532+
})
2533+
class FalsyValueSelect {
2534+
foods: any[] = [
2535+
{ value: 0, viewValue: 'Steak' },
2536+
{ value: 1, viewValue: 'Pizza' },
2537+
];
2538+
control = new FormControl();
2539+
@ViewChildren(MdOption) options: QueryList<MdOption>;
2540+
}

src/lib/select/select.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,9 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
579579
*/
580580
private _selectValue(value: any): MdOption {
581581
let optionsArray = this.options.toArray();
582-
let correspondingOption = optionsArray.find(option => option.value && option.value === value);
582+
let correspondingOption = optionsArray.find(option => {
583+
return option.value != null && option.value === value;
584+
});
583585

584586
if (correspondingOption) {
585587
correspondingOption.select();

0 commit comments

Comments
 (0)