Skip to content

Commit 36dea28

Browse files
committed
Properly mark component for check
1 parent 0365c7a commit 36dea28

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/lib/slide-toggle/slide-toggle.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,31 @@ describe('MdSlideToggle with forms', () => {
606606
expect(slideToggle.checked).toBe(true);
607607
expect(slideToggleElement.classList).toContain('mat-checked');
608608
}));
609+
610+
it('should update checked state on click if control is checked initially', fakeAsync(() => {
611+
fixture = TestBed.createComponent(SlideToggleWithModel);
612+
slideToggle = fixture.debugElement.query(By.directive(MdSlideToggle)).componentInstance;
613+
labelElement = fixture.debugElement.query(By.css('label')).nativeElement;
614+
615+
fixture.componentInstance.modelValue = true;
616+
fixture.detectChanges();
617+
618+
// Flush the microtasks because the forms module updates the model state asynchronously.
619+
flushMicrotasks();
620+
621+
// Now the new checked variable has been updated in the slide-toggle and the slide-toggle
622+
// is marked for check because it still needs to update the underlying input.
623+
fixture.detectChanges();
624+
625+
expect(slideToggle.checked)
626+
.toBe(true, 'Expected slide-toggle to be checked initially');
627+
628+
labelElement.click();
629+
fixture.detectChanges();
630+
631+
expect(slideToggle.checked)
632+
.toBe(false, 'Expected slide-toggle to be no longer checked after label click.');
633+
}));
609634
});
610635

611636
describe('with a FormControl', () => {

src/lib/slide-toggle/slide-toggle.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase implements OnDestroy,
8686
private _uniqueId: string = `md-slide-toggle-${++nextUniqueId}`;
8787
private _slideRenderer: SlideToggleRenderer;
8888
private _required: boolean = false;
89+
private _checked: boolean = false;
8990

9091
/** Reference to the focus state ripple. */
9192
private _focusRipple: RippleRef | null;
@@ -103,7 +104,6 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase implements OnDestroy,
103104
@Input() labelPosition: 'before' | 'after' = 'after';
104105

105106
/** Whether the slide-toggle element is checked or not */
106-
@Input() checked: boolean = false;
107107

108108
/** Used to set the aria-label attribute on the underlying input element. */
109109
@Input('aria-label') ariaLabel: string | null = null;
@@ -116,6 +116,13 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase implements OnDestroy,
116116
get required(): boolean { return this._required; }
117117
set required(value) { this._required = coerceBooleanProperty(value); }
118118

119+
/** Whether the slide-toggle element is checked or not */
120+
@Input()
121+
get checked(): boolean { return this._checked; }
122+
set checked(value) {
123+
this._checked = !!value;
124+
this._changeDetectorRef.markForCheck();
125+
}
119126
/** An event will be dispatched each time the slide-toggle changes its value. */
120127
@Output() change: EventEmitter<MdSlideToggleChange> = new EventEmitter<MdSlideToggleChange>();
121128

0 commit comments

Comments
 (0)