Skip to content

Commit 084be9e

Browse files
committed
fix(chips): able to remove disabled chip via remove button
Fixes users being able to remove disabled chips by clicking on their remove button. Fixes #15708.
1 parent 533eca6 commit 084be9e

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/lib/chips/chip-remove.spec.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,54 @@ describe('Chip Remove', () => {
3131

3232
describe('basic behavior', () => {
3333
it('should applies the `mat-chip-remove` CSS class', () => {
34-
let hrefElement = chipNativeElement.querySelector('a')!;
34+
let buttonElement = chipNativeElement.querySelector('button')!;
3535

36-
expect(hrefElement.classList).toContain('mat-chip-remove');
36+
expect(buttonElement.classList).toContain('mat-chip-remove');
3737
});
3838

3939
it('should emits (removed) on click', () => {
40-
let hrefElement = chipNativeElement.querySelector('a')!;
40+
let buttonElement = chipNativeElement.querySelector('button')!;
4141

4242
testChip.removable = true;
4343
fixture.detectChanges();
4444

4545
spyOn(testChip, 'didRemove');
4646

47-
hrefElement.click();
47+
buttonElement.click();
48+
fixture.detectChanges();
4849

4950
expect(testChip.didRemove).toHaveBeenCalled();
5051
});
52+
53+
it('should not remove if parent chip is disabled', () => {
54+
let buttonElement = chipNativeElement.querySelector('button')!;
55+
56+
testChip.disabled = true;
57+
testChip.removable = true;
58+
fixture.detectChanges();
59+
60+
spyOn(testChip, 'didRemove');
61+
62+
buttonElement.click();
63+
fixture.detectChanges();
64+
65+
expect(testChip.didRemove).not.toHaveBeenCalled();
66+
});
67+
5168
});
5269
});
5370

5471
@Component({
5572
template: `
56-
<mat-chip [removable]="removable" (removed)="didRemove()"><a matChipRemove></a></mat-chip>
73+
<mat-chip
74+
[removable]="removable"
75+
[disabled]="disabled"
76+
(removed)="didRemove()"><button matChipRemove></button></mat-chip>
5777
`
5878
})
5979
class TestChip {
6080
removable: boolean;
81+
disabled = false;
6182

6283
didRemove() {}
6384
}

src/lib/chips/chip.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,10 @@ export class MatChipRemove {
396396

397397
/** Calls the parent chip's public `remove()` method if applicable. */
398398
_handleClick(event: Event): void {
399-
if (this._parentChip.removable) {
400-
this._parentChip.remove();
399+
const parentChip = this._parentChip;
400+
401+
if (parentChip.removable && !parentChip.disabled) {
402+
parentChip.remove();
401403
}
402404

403405
// We need to stop event propagation because otherwise the event will bubble up to the

0 commit comments

Comments
 (0)