Skip to content

Commit

Permalink
fix(material-experimental/checkbox): model value not updated through …
Browse files Browse the repository at this point in the history
…toggle method (#17229)

Along the same lines as #11902. When the value of an experimental checkbox is changed through the `toggle` method it wasn't being propagated to the `ControlValueAccessor`.

(cherry picked from commit f178f20)
  • Loading branch information
crisbeto authored and mmalerba committed Oct 8, 2019
1 parent 2a92162 commit 73b086d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/material-experimental/mdc-checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,17 @@ describe('MatCheckbox', () => {
expect(checkboxInstance.checked).toBe(false);
expect(ngModel.valid).toBe(false);
}));

it('should update the ngModel value when using the `toggle` method', fakeAsync(() => {
const checkbox = fixture.debugElement.query(By.directive(MatCheckbox)).componentInstance;

expect(fixture.componentInstance.isGood).toBe(false);

checkbox.toggle();
fixture.detectChanges();

expect(fixture.componentInstance.isGood).toBe(true);
}));
});

describe('with name attribute', () => {
Expand Down
1 change: 1 addition & 0 deletions src/material-experimental/mdc-checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export class MatCheckbox implements AfterViewInit, OnDestroy, ControlValueAccess
/** Toggles the `checked` state of the checkbox. */
toggle() {
this.checked = !this.checked;
this._cvaOnChange(this.checked);
}

/** Handles blur events on the native input. */
Expand Down

0 comments on commit 73b086d

Please sign in to comment.