Skip to content

Commit 916b2f2

Browse files
authored
Merge pull request #2279 from miqh/fix/tag-disable
fix(tag): disable state (#2061)
2 parents 51e0f05 + 4385df9 commit 916b2f2

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { TestBed } from "@angular/core/testing";
2+
import { By } from "@angular/platform-browser";
3+
import { Component } from "@angular/core";
4+
5+
import { IconModule } from "../icon";
6+
import { TagFilter } from "./tag-filter.component";
7+
8+
@Component({
9+
template: `
10+
<ibm-tag-filter [disabled]="disabled">TagFilter</ibm-tag-filter>`
11+
})
12+
class TagFilterTest {
13+
disabled = false;
14+
}
15+
16+
describe("TagFilter", () => {
17+
let fixture, debugElement;
18+
beforeEach(() => {
19+
TestBed.configureTestingModule({
20+
declarations: [
21+
TagFilter,
22+
TagFilterTest
23+
],
24+
imports: [
25+
IconModule
26+
]
27+
});
28+
});
29+
30+
it("should call onClick on label click", () => {
31+
fixture = TestBed.createComponent(TagFilterTest);
32+
debugElement = fixture.debugElement.query(By.css(".bx--tag__label"));
33+
fixture.detectChanges();
34+
spyOn(debugElement.componentInstance.click, "emit");
35+
debugElement.nativeElement.click();
36+
fixture.detectChanges();
37+
expect(debugElement.componentInstance.click.emit).toHaveBeenCalledWith({ action: "click" });
38+
});
39+
40+
it("should call both onClick and onClose on close icon click", () => {
41+
fixture = TestBed.createComponent(TagFilterTest);
42+
debugElement = fixture.debugElement.query(By.css(".bx--tag__close-icon"));
43+
fixture.detectChanges();
44+
spyOn(debugElement.componentInstance.close, "emit");
45+
spyOn(debugElement.componentInstance.click, "emit");
46+
debugElement.nativeElement.click();
47+
fixture.detectChanges();
48+
expect(debugElement.componentInstance.close.emit).toHaveBeenCalled();
49+
expect(debugElement.componentInstance.click.emit).toHaveBeenCalledWith({ action: "close" });
50+
});
51+
52+
it("should not call onClick when disabled", () => {
53+
fixture = TestBed.createComponent(TagFilterTest);
54+
fixture.componentInstance.disabled = true;
55+
debugElement = fixture.debugElement.query(By.css(".bx--tag__label"));
56+
fixture.detectChanges();
57+
spyOn(debugElement.componentInstance.click, "emit");
58+
debugElement.nativeElement.click();
59+
fixture.detectChanges();
60+
expect(debugElement.componentInstance.click.emit).not.toHaveBeenCalled();
61+
});
62+
63+
it("should not call onClick nor onClose when disabled", () => {
64+
fixture = TestBed.createComponent(TagFilterTest);
65+
fixture.componentInstance.disabled = true;
66+
debugElement = fixture.debugElement.query(By.css(".bx--tag__close-icon"));
67+
fixture.detectChanges();
68+
spyOn(debugElement.componentInstance.close, "emit");
69+
spyOn(debugElement.componentInstance.click, "emit");
70+
debugElement.nativeElement.click();
71+
fixture.detectChanges();
72+
expect(debugElement.componentInstance.close.emit).not.toHaveBeenCalled();
73+
expect(debugElement.componentInstance.click.emit).not.toHaveBeenCalled();
74+
});
75+
});

src/tag/tag-filter.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export class TagFilter extends Tag {
4848

4949
onClick(event: any) {
5050
event.stopImmediatePropagation();
51-
this.click.emit({ action: "click" });
51+
if (!this.disabled) {
52+
this.click.emit({ action: "click" });
53+
}
5254
}
5355

5456
onClose(event: any) {
@@ -58,7 +60,7 @@ export class TagFilter extends Tag {
5860
}
5961

6062
@HostBinding("attr.class") get attrClass() {
61-
return `bx--tag bx--tag--filter bx--tag--${this.type} ${this.class}`;
63+
return `bx--tag bx--tag--filter bx--tag--${this.type} ${this.class}${this.disabled ? " bx--tag--disabled" : ""}`;
6264
}
6365

6466
@HostBinding("attr.aria-label") get attrAriaLabel() {

src/tag/tag.stories.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ storiesOf("Components|Tag", module)
4242
title="Filter"
4343
closeButtonLabel="Clear"
4444
>filter</ibm-tag-filter>
45+
<ibm-tag-filter
46+
type="blue"
47+
title="Disabled filter"
48+
closeButtonLabel="Clear"
49+
[disabled]="true"
50+
>disabled filter</ibm-tag-filter>
4551
`
4652
}))
4753
.add("Documentation", () => ({

0 commit comments

Comments
 (0)