Skip to content

Commit 317952a

Browse files
devversionkara
authored andcommitted
fix(slide-toggle): invalid model change event (#4140)
Fixes #4124.
1 parent fb1fabc commit 317952a

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

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

+14-11
Original file line numberDiff line numberDiff line change
@@ -295,18 +295,23 @@ describe('MdSlideToggle', () => {
295295
expect(slideToggleControl.pristine).toBe(true);
296296
expect(slideToggleControl.touched).toBe(false);
297297

298-
// After changing the value programmatically, the control should
299-
// become dirty (not pristine), but remain untouched.
298+
// After changing the value from the view, the control should
299+
// become dirty (not pristine), but remain untouched if focus is still there.
300300
slideToggle.checked = true;
301+
302+
// Dispatch a change event on the input element to fake a user interaction that triggered
303+
// the state change.
304+
dispatchFakeEvent(inputElement, 'change');
305+
301306
fixture.detectChanges();
302307

303308
expect(slideToggleControl.valid).toBe(true);
304309
expect(slideToggleControl.pristine).toBe(false);
305310
expect(slideToggleControl.touched).toBe(false);
306311

307-
// After a user interaction occurs (such as a click), the control should remain dirty and
308-
// now also be touched.
309-
labelElement.click();
312+
// Once the input element looses focus, the control should remain dirty but should
313+
// also turn touched.
314+
dispatchFakeEvent(inputElement, 'blur');
310315
fixture.detectChanges();
311316

312317
expect(slideToggleControl.valid).toBe(true);
@@ -324,13 +329,13 @@ describe('MdSlideToggle', () => {
324329
expect(slideToggleControl.touched).toBe(false);
325330
expect(slideToggleElement.classList).toContain('mat-checked');
326331

327-
// After a user interaction occurs (such as a click), the control should remain dirty and
328-
// now also be touched.
329-
inputElement.click();
332+
// Once the input element looses focus, the control should remain dirty but should
333+
// also turn touched.
334+
dispatchFakeEvent(inputElement, 'blur');
330335
fixture.detectChanges();
331336

332337
expect(slideToggleControl.touched).toBe(true);
333-
expect(slideToggleElement.classList).not.toContain('mat-checked');
338+
expect(slideToggleElement.classList).toContain('mat-checked');
334339
});
335340

336341
// TODO(kara): update when core/testing adds fix
@@ -434,15 +439,13 @@ describe('MdSlideToggle', () => {
434439
}));
435440

436441
it('should prevent the form from submit when being required', () => {
437-
438442
if ('reportValidity' in inputElement === false) {
439443
// If the browser does not report the validity then the tests will break.
440444
// e.g Safari 8 on Mobile.
441445
return;
442446
}
443447

444448
testComponent.isRequired = true;
445-
446449
fixture.detectChanges();
447450

448451
buttonElement.click();

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

+20-24
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueA
6565

6666
// A unique id for the slide-toggle. By default the id is auto-generated.
6767
private _uniqueId = `md-slide-toggle-${++nextId}`;
68-
private _checked: boolean = false;
6968
private _color: string;
7069
private _isMousedown: boolean = false;
7170
private _slideRenderer: SlideToggleRenderer = null;
@@ -88,6 +87,9 @@ export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueA
8887
/** Whether the label should appear after or before the slide-toggle. Defaults to 'after' */
8988
@Input() labelPosition: 'before' | 'after' = 'after';
9089

90+
/** Whether the slide-toggle element is checked or not */
91+
@Input() checked: boolean = false;
92+
9193
/** Used to set the aria-label attribute on the underlying input element. */
9294
@Input('aria-label') ariaLabel: string = null;
9395

@@ -138,29 +140,30 @@ export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueA
138140
}
139141

140142
/**
141-
* The onChangeEvent method will be also called on click.
142-
* This is because everything for the slide-toggle is wrapped inside of a label,
143-
* which triggers a onChange event on click.
143+
* This function will called if the underlying input changed its value through user interaction.
144144
*/
145145
_onChangeEvent(event: Event) {
146146
// We always have to stop propagation on the change event.
147147
// Otherwise the change event, from the input element, will bubble up and
148148
// emit its event object to the component's `change` output.
149149
event.stopPropagation();
150150

151-
// Once a drag is currently in progress, we do not want to toggle the slide-toggle on a click.
152-
if (!this.disabled && !this._slideRenderer.dragging) {
153-
this.toggle();
151+
// Sync the value from the underlying input element with the slide-toggle component.
152+
this.checked = this._inputElement.nativeElement.checked;
154153

155-
// Emit our custom change event if the native input emitted one.
156-
// It is important to only emit it, if the native input triggered one, because
157-
// we don't want to trigger a change event, when the `checked` variable changes for example.
158-
this._emitChangeEvent();
159-
}
154+
// Emit our custom change event if the native input emitted one.
155+
// It is important to only emit it, if the native input triggered one, because we don't want
156+
// to trigger a change event, when the `checked` variable changes programmatically.
157+
this._emitChangeEvent();
160158
}
161159

162160
_onInputClick(event: Event) {
163-
this.onTouched();
161+
// In some situations the user will release the mouse on the label element. The label element
162+
// redirects the click to the underlying input element and will result in a value change.
163+
// Prevent the default behavior if dragging, because the value will be set after drag.
164+
if (this._slideRenderer.dragging) {
165+
event.preventDefault();
166+
}
164167

165168
// We have to stop propagation for click events on the visual hidden input element.
166169
// By default, when a user clicks on a label element, a generated click event will be
@@ -206,16 +209,6 @@ export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueA
206209
this._focusOriginMonitor.focusVia(this._inputElement.nativeElement, this._renderer, 'keyboard');
207210
}
208211

209-
/** Whether the slide-toggle is checked. */
210-
@Input()
211-
get checked() { return !!this._checked; }
212-
set checked(value) {
213-
if (this.checked !== !!value) {
214-
this._checked = value;
215-
this.onChange(this._checked);
216-
}
217-
}
218-
219212
/** The color of the slide-toggle. Can be primary, accent, or warn. */
220213
@Input()
221214
get color(): string { return this._color; }
@@ -256,12 +249,15 @@ export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueA
256249
}
257250
}
258251

259-
/** Emits the change event to the `change` output EventEmitter */
252+
/**
253+
* Emits a change event on the `change` output. Also notifies the FormControl about the change.
254+
*/
260255
private _emitChangeEvent() {
261256
let event = new MdSlideToggleChange();
262257
event.source = this;
263258
event.checked = this.checked;
264259
this.change.emit(event);
260+
this.onChange(this.checked);
265261
}
266262

267263

0 commit comments

Comments
 (0)