Skip to content

Commit 8aa9857

Browse files
tinayuangaokara
authored andcommitted
fix(checkbox): switch checkbox behaviors for click and change events (#3146)
1 parent eba7641 commit 8aa9857

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/lib/checkbox/checkbox.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ describe('MdCheckbox', () => {
157157
expect(checkboxInstance.checked).toBe(false);
158158
expect(checkboxInstance.indeterminate).toBe(true);
159159

160-
checkboxInstance._onInteractionEvent(<Event>{stopPropagation: () => {}});
160+
checkboxInstance._onInputClick(<Event>{stopPropagation: () => {}});
161161

162162
expect(checkboxInstance.checked).toBe(true);
163163
expect(checkboxInstance.indeterminate).toBe(false);
164164

165-
checkboxInstance._onInteractionEvent(<Event>{stopPropagation: () => {}});
165+
checkboxInstance._onInputClick(<Event>{stopPropagation: () => {}});
166166
fixture.detectChanges();
167167

168168
expect(checkboxInstance.checked).toBe(false);
@@ -416,7 +416,7 @@ describe('MdCheckbox', () => {
416416
testComponent.isIndeterminate = true;
417417
fixture.detectChanges();
418418

419-
testComponent.isChecked = true;
419+
inputElement.click();
420420
fixture.detectChanges();
421421

422422
expect(checkboxNativeElement.classList).not.toContain(

src/lib/checkbox/checkbox.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,18 @@ export class MdCheckbox implements ControlValueAccessor {
332332
/**
333333
* Event handler for checkbox input element.
334334
* Toggles checked state if element is not disabled.
335+
* Do not toggle on (change) event since IE doesn't fire change event when
336+
* indeterminate checkbox is clicked.
335337
* @param event
336338
*/
337-
_onInteractionEvent(event: Event) {
338-
// We always have to stop propagation on the change event.
339-
// Otherwise the change event, from the input element, will bubble up and
340-
// emit its event object to the `change` output.
339+
_onInputClick(event: Event) {
340+
// We have to stop propagation for click events on the visual hidden input element.
341+
// By default, when a user clicks on a label element, a generated click event will be
342+
// dispatched on the associated input element. Since we are using a label element as our
343+
// root container, the click event on the `checkbox` will be executed twice.
344+
// The real click event will bubble up, and the generated click event also tries to bubble up.
345+
// This will lead to multiple click events.
346+
// Preventing bubbling for the second event will solve that issue.
341347
event.stopPropagation();
342348

343349
if (!this.disabled) {
@@ -356,14 +362,10 @@ export class MdCheckbox implements ControlValueAccessor {
356362
this._onInputFocus();
357363
}
358364

359-
_onInputClick(event: Event) {
360-
// We have to stop propagation for click events on the visual hidden input element.
361-
// By default, when a user clicks on a label element, a generated click event will be
362-
// dispatched on the associated input element. Since we are using a label element as our
363-
// root container, the click event on the `checkbox` will be executed twice.
364-
// The real click event will bubble up, and the generated click event also tries to bubble up.
365-
// This will lead to multiple click events.
366-
// Preventing bubbling for the second event will solve that issue.
365+
_onInteractionEvent(event: Event) {
366+
// We always have to stop propagation on the change event.
367+
// Otherwise the change event, from the input element, will bubble up and
368+
// emit its event object to the `change` output.
367369
event.stopPropagation();
368370
}
369371

0 commit comments

Comments
 (0)