Skip to content

Commit c456d86

Browse files
committed
Properly mark component for check
1 parent aad1e1a commit c456d86

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,40 @@ describe('MdSlideToggle', () => {
407407
});
408408

409409
describe('custom template', () => {
410+
410411
it('should not trigger the change event on initialization', async(() => {
411-
let fixture = TestBed.createComponent(SlideToggleTestApp);
412+
const fixture = TestBed.createComponent(SlideToggleTestApp);
412413
fixture.componentInstance.slideModel = true;
413414
fixture.componentInstance.slideChecked = true;
414415
fixture.detectChanges();
415416

416417
expect(fixture.componentInstance.lastEvent).toBeFalsy();
417418
}));
419+
420+
it('should update checked state on click if control is checked initially', async(() => {
421+
const fixture = TestBed.createComponent(SlideToggleTestApp);
422+
const slideToggleInstance = fixture.debugElement
423+
.query(By.directive(MdSlideToggle)).componentInstance;
424+
const labelElement = fixture.debugElement.query(By.css('label')).nativeElement;
425+
426+
fixture.componentInstance.slideModel = true;
427+
fixture.detectChanges();
428+
429+
fixture.whenStable().then(() => {
430+
// Now the new checked variable has been updated in the slide-toggle and the slide-toggle
431+
// is marked for check because it still needs to update the underlying input.
432+
fixture.detectChanges();
433+
434+
expect(slideToggleInstance.checked)
435+
.toBe(true, 'Expected slide-toggle to be checked initially');
436+
437+
labelElement.click();
438+
fixture.detectChanges();
439+
440+
expect(slideToggleInstance.checked)
441+
.toBe(false, 'Expected slide-toggle to be no longer checked after label click.');
442+
});
443+
}));
418444
});
419445

420446
describe('with forms', () => {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
8585
private _uniqueId = `md-slide-toggle-${++nextId}`;
8686
private _slideRenderer: SlideToggleRenderer;
8787
private _required: boolean = false;
88+
private _checked: boolean = false;
8889
private _disableRipple: boolean = false;
8990

9091
/** Reference to the focus state ripple. */
@@ -103,7 +104,6 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
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;
@@ -121,6 +121,14 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
121121
get disableRipple(): boolean { return this._disableRipple; }
122122
set disableRipple(value) { this._disableRipple = coerceBooleanProperty(value); }
123123

124+
/** Whether the slide-toggle element is checked or not */
125+
@Input()
126+
get checked(): boolean { return this._checked; }
127+
set checked(value) {
128+
this._checked = !!value;
129+
this._changeDetectorRef.markForCheck();
130+
}
131+
124132
/** An event will be dispatched each time the slide-toggle changes its value. */
125133
@Output() change: EventEmitter<MdSlideToggleChange> = new EventEmitter<MdSlideToggleChange>();
126134

0 commit comments

Comments
 (0)