Skip to content

fix(button-toggle): support two-way binding of value #7911

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
Nov 2, 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
11 changes: 10 additions & 1 deletion src/lib/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,15 @@ describe('MatButtonToggle without forms', () => {
expect(groupInstance.selected).toBe(buttonToggleInstances[0]);
});

it('should propagate the value change back up via a two-way binding', () => {
expect(groupInstance.value).toBeFalsy();
buttonToggleLabelElements[0].click();
fixture.detectChanges();

expect(groupInstance.value).toBe('test1');
expect(testComponent.groupValue).toBe('test1');
});

it('should update the group and toggles when one of the button toggles is clicked', () => {
expect(groupInstance.value).toBeFalsy();
buttonToggleLabelElements[0].click();
Expand Down Expand Up @@ -618,7 +627,7 @@ describe('MatButtonToggle without forms', () => {
template: `
<mat-button-toggle-group [disabled]="isGroupDisabled"
[vertical]="isVertical"
[value]="groupValue">
[(value)]="groupValue">
<mat-button-toggle value="test1">Test1</mat-button-toggle>
<mat-button-toggle value="test2">Test2</mat-button-toggle>
<mat-button-toggle value="test3">Test3</mat-button-toggle>
Expand Down
9 changes: 8 additions & 1 deletion src/lib/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,18 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase
set value(newValue: any) {
if (this._value != newValue) {
this._value = newValue;

this.valueChange.emit(newValue);
this._updateSelectedButtonToggleFromValue();
}
}

/**
* Event that emits whenever the value of the group changes.
* Used to facilitate two-way data binding.
* @docs-private
*/
@Output() valueChange = new EventEmitter<any>();

/** Whether the toggle group is selected. */
@Input()
get selected() {
Expand Down