Skip to content

Commit

Permalink
fix(chips): able to remove disabled chip via remove button (#15732)
Browse files Browse the repository at this point in the history
Fixes users being able to remove disabled chips by clicking on their remove button.

Fixes #15708.
  • Loading branch information
crisbeto authored and jelbourn committed May 14, 2019
1 parent 0c62798 commit 20a0930
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
31 changes: 26 additions & 5 deletions src/material/chips/chip-remove.spec.ts
Expand Up @@ -31,33 +31,54 @@ describe('Chip Remove', () => {

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

expect(hrefElement.classList).toContain('mat-chip-remove');
expect(buttonElement.classList).toContain('mat-chip-remove');
});

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

testChip.removable = true;
fixture.detectChanges();

spyOn(testChip, 'didRemove');

hrefElement.click();
buttonElement.click();
fixture.detectChanges();

expect(testChip.didRemove).toHaveBeenCalled();
});

it('should not remove if parent chip is disabled', () => {
let buttonElement = chipNativeElement.querySelector('button')!;

testChip.disabled = true;
testChip.removable = true;
fixture.detectChanges();

spyOn(testChip, 'didRemove');

buttonElement.click();
fixture.detectChanges();

expect(testChip.didRemove).not.toHaveBeenCalled();
});

});
});

@Component({
template: `
<mat-chip [removable]="removable" (removed)="didRemove()"><a matChipRemove></a></mat-chip>
<mat-chip
[removable]="removable"
[disabled]="disabled"
(removed)="didRemove()"><button matChipRemove></button></mat-chip>
`
})
class TestChip {
removable: boolean;
disabled = false;

didRemove() {}
}
6 changes: 4 additions & 2 deletions src/material/chips/chip.ts
Expand Up @@ -394,8 +394,10 @@ export class MatChipRemove {

/** Calls the parent chip's public `remove()` method if applicable. */
_handleClick(event: Event): void {
if (this._parentChip.removable) {
this._parentChip.remove();
const parentChip = this._parentChip;

if (parentChip.removable && !parentChip.disabled) {
parentChip.remove();
}

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

0 comments on commit 20a0930

Please sign in to comment.