Skip to content

Commit

Permalink
fix(button-toggle): fix button toggle with 0 value not checked (#11292)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao authored May 15, 2018
1 parent 04b076d commit 9a8cd4d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/lib/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ describe('MatButtonToggle without forms', () => {
declarations: [
ButtonTogglesInsideButtonToggleGroup,
ButtonTogglesInsideButtonToggleGroupMultiple,
FalsyButtonTogglesInsideButtonToggleGroupMultiple,
ButtonToggleGroupWithInitialValue,
StandaloneButtonToggle,
ButtonToggleWithAriaLabel,
Expand Down Expand Up @@ -694,6 +695,22 @@ describe('MatButtonToggle without forms', () => {
expect(fixture.componentInstance.toggleGroup.value).toBe('Seven');
expect(fixture.componentInstance.toggles.toArray()[2].checked).toBe(true);
});

it('should select falsy button toggle value in multiple selection', () => {
const fixture = TestBed.createComponent(FalsyButtonTogglesInsideButtonToggleGroupMultiple);
fixture.detectChanges();

expect(fixture.componentInstance.toggles.toArray()[0].checked).toBe(true);
expect(fixture.componentInstance.toggles.toArray()[1].checked).toBe(false);
expect(fixture.componentInstance.toggles.toArray()[2].checked).toBe(false);

fixture.componentInstance.value = [0, false];
fixture.detectChanges();

expect(fixture.componentInstance.toggles.toArray()[0].checked).toBe(true);
expect(fixture.componentInstance.toggles.toArray()[1].checked).toBe(false);
expect(fixture.componentInstance.toggles.toArray()[2].checked).toBe(true);
});
});

@Component({
Expand Down Expand Up @@ -748,6 +765,21 @@ class ButtonTogglesInsideButtonToggleGroupMultiple {
isVertical: boolean = false;
}

@Component({
template: `
<mat-button-toggle-group multiple [value]="value">
<mat-button-toggle [value]="0">Eggs</mat-button-toggle>
<mat-button-toggle [value]="null">Flour</mat-button-toggle>
<mat-button-toggle [value]="false">Sugar</mat-button-toggle>
<mat-button-toggle>Sugar</mat-button-toggle>
</mat-button-toggle-group>
`
})
class FalsyButtonTogglesInsideButtonToggleGroupMultiple {
value: ('' | number | null | undefined | boolean)[] = [0];
@ViewChildren(MatButtonToggle) toggles: QueryList<MatButtonToggle>;
}

@Component({
template: `
<mat-button-toggle>Yes</mat-button-toggle>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase impleme
}

if (this.multiple && Array.isArray(this._rawValue)) {
return !!this._rawValue.find(value => toggle.value != null && value === toggle.value);
return this._rawValue.some(value => toggle.value != null && value === toggle.value);
}

return toggle.value === this._rawValue;
Expand Down

0 comments on commit 9a8cd4d

Please sign in to comment.