mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(select): unable to programmatically select falsy values (#4868)
Fixes not being able to set falsy values progammatically in `md-select`. Fixes #4854.
This commit is contained in:
committed by
Andrew Seguin
parent
ce9d253331
commit
2e3910c86c
@@ -56,7 +56,8 @@ describe('MdSelect', () => {
|
||||
BasicSelectInitiallyHidden,
|
||||
BasicSelectNoPlaceholder,
|
||||
BasicSelectWithTheming,
|
||||
ResetValuesSelect
|
||||
ResetValuesSelect,
|
||||
FalsyValueSelect
|
||||
],
|
||||
providers: [
|
||||
{provide: OverlayContainer, useFactory: () => {
|
||||
@@ -631,6 +632,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', () => {
|
||||
@@ -2516,3 +2533,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>;
|
||||
}
|
||||
|
||||
@@ -582,7 +582,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();
|
||||
|
||||
Reference in New Issue
Block a user