Skip to content

Commit a443df6

Browse files
committed
fix(chips): default click action on chip being prevented
Fixes all the click actions on a chip being prevented. The action wasn't being prevented up until c82aca9 where it got moved out of the `if (this.disabled)`. Since there isn't any info or tests around why it was done this way, I'm assuming that it was by accident. Fixes #9032.
1 parent 4639a87 commit a443df6

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/lib/chips/chip.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Directionality} from '@angular/cdk/bidi';
22
import {BACKSPACE, DELETE, SPACE} from '@angular/cdk/keycodes';
3-
import {createKeyboardEvent} from '@angular/cdk/testing';
3+
import {createKeyboardEvent, dispatchFakeEvent} from '@angular/cdk/testing';
44
import {Component, DebugElement} from '@angular/core';
55
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
66
import {By} from '@angular/platform-browser';
@@ -132,6 +132,24 @@ describe('Chips', () => {
132132

133133
expect(testComponent.chipRemove).toHaveBeenCalledWith({chip: chipInstance});
134134
});
135+
136+
it('should not prevent the default click action', () => {
137+
const event = dispatchFakeEvent(chipNativeElement, 'click');
138+
fixture.detectChanges();
139+
140+
expect(event.defaultPrevented).toBe(false);
141+
});
142+
143+
it('should prevent the default click action when the chip is disabled', () => {
144+
chipInstance.disabled = true;
145+
fixture.detectChanges();
146+
147+
const event = dispatchFakeEvent(chipNativeElement, 'click');
148+
fixture.detectChanges();
149+
150+
expect(event.defaultPrevented).toBe(true);
151+
});
152+
135153
});
136154

137155
describe('keyboard behavior', () => {

src/lib/chips/chip.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,12 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
322322
}
323323
}
324324

325-
/** Ensures events fire properly upon click. */
325+
/** Handles click events on the chip. */
326326
_handleClick(event: Event) {
327-
// Check disabled
328327
if (this.disabled) {
329-
return;
328+
event.preventDefault();
330329
}
331330

332-
event.preventDefault();
333331
event.stopPropagation();
334332
}
335333

0 commit comments

Comments
 (0)