Skip to content

Commit 5346353

Browse files
devversionmmalerba
authored andcommitted
fix(slide-toggle): invalid change events with no new value (#3555)
Currently the slide-toggle will always emit a change event on dragend. This is not always correct, because the `checked` value only changes if 50% of the distance have been dragged. Fixes #3526
1 parent 0609e29 commit 5346353

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,22 @@ describe('MdSlideToggle', () => {
563563
expect(testComponent.lastEvent.checked).toBe(true);
564564
}));
565565

566+
it('should not emit a change event when the value did not change', fakeAsync(() => {
567+
expect(slideToggle.checked).toBe(false);
568+
569+
gestureConfig.emitEventForElement('slidestart', slideThumbContainer);
570+
gestureConfig.emitEventForElement('slide', slideThumbContainer, { deltaX: 0 });
571+
gestureConfig.emitEventForElement('slideend', slideThumbContainer);
572+
573+
// Flush the timeout for the slide ending.
574+
tick();
575+
576+
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
577+
expect(slideToggle.checked).toBe(false);
578+
expect(testComponent.lastEvent)
579+
.toBeFalsy('Expected the slide-toggle to not emit a change event.');
580+
}));
581+
566582
it('should update the checked property of the input', fakeAsync(() => {
567583
expect(inputElement.checked).toBe(false);
568584

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,12 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor {
258258

259259
_onDragEnd() {
260260
if (this._slideRenderer.dragging) {
261+
let _previousChecked = this.checked;
261262
this.checked = this._slideRenderer.dragPercentage > 50;
262-
this._emitChangeEvent();
263+
264+
if (_previousChecked !== this.checked) {
265+
this._emitChangeEvent();
266+
}
263267

264268
// The drag should be stopped outside of the current event handler, because otherwise the
265269
// click event will be fired before and will revert the drag change.

0 commit comments

Comments
 (0)