Skip to content

Commit 95baf06

Browse files
committed
feat: add icon str ref to contained list item
Signed-off-by: Maicon Godinho <maicongodinho1@gmail.com>
1 parent f47e5ae commit 95baf06

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

src/contained-list/contained-list-item.component.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
<ng-container *ngIf="!clickable">
2424
<div class="cds--contained-list-item__content">
2525
<div *ngIf="icon" class="cds--contained-list-item__icon">
26-
<ng-template [ngTemplateOutlet]="icon"></ng-template>
26+
<ng-container *ngIf="!isTemplate(icon)"><svg [ibmIcon]="icon" size="16"></svg></ng-container>
27+
<ng-template *ngIf="isTemplate(icon)" [ngTemplateOutlet]="icon"></ng-template>
2728
</div>
2829
<ng-content></ng-content>
2930
</div>
@@ -58,8 +59,12 @@ export class ContainedListItem {
5859

5960
/**
6061
* Provide an optional icon to render in front of the item's content.
62+
*
63+
* Note that if you intend to use this as a string ref, it's important to remember
64+
* to register the icon that you wish to add. In this case, it's also worth noting
65+
* that only icons with a size of 16 are currently supported.
6166
*/
62-
@Input() icon: TemplateRef<any>;
67+
@Input() icon: TemplateRef<any> | string;
6368

6469
/**
6570
* Emits click event.
@@ -90,7 +95,11 @@ export class ContainedListItem {
9095
return !!this.icon;
9196
}
9297

93-
onClick() {
98+
public onClick() {
9499
this.click.emit();
95100
}
101+
102+
public isTemplate(value: string | TemplateRef<any>) {
103+
return value instanceof TemplateRef;
104+
}
96105
}

src/contained-list/contained-list.component.spec.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { Component } from "@angular/core";
22
import { ComponentFixture, TestBed } from "@angular/core/testing";
33
import { By } from "@angular/platform-browser";
44
import { ButtonModule } from "../button";
5-
import { IconModule } from "../icon";
5+
import { IconModule, IconService } from "../icon";
66
import { ContainedListItem } from "./contained-list-item.component";
7-
7+
import Apple16 from "@carbon/icons/es/apple/16";
8+
import Fish16 from "@carbon/icons/es/fish/16";
89
import { ContainedList } from "./contained-list.component";
910
import { ContainedListKind, ContainedListSize } from "./contained-list.enums";
1011

@@ -25,20 +26,25 @@ import { ContainedListKind, ContainedListSize } from "./contained-list.enums";
2526
</ng-template>
2627
2728
<ng-template #icon>
28-
<svg ibmIcon="add" size="16"></svg>
29+
<svg ibmIcon="fish" size="16"></svg>
2930
</ng-template>
3031
3132
<cds-contained-list [label]="label" [action]="action">
3233
<cds-contained-list-item>List item</cds-contained-list-item>
3334
<cds-contained-list-item [icon]="icon">List item with icon</cds-contained-list-item>
35+
<cds-contained-list-item icon="apple">List item with string ref icon</cds-contained-list-item>
3436
<cds-contained-list-item [action]="action">List item with action</cds-contained-list-item>
3537
<cds-contained-list-item #clickableListItem [clickable]="true">
3638
<ng-container ibmContainedListItemButton>Clickable list item</ng-container>
3739
</cds-contained-list-item>
3840
</cds-contained-list>
3941
`
4042
})
41-
class WrapperComponent {}
43+
class WrapperComponent {
44+
constructor(private iconService: IconService) {
45+
this.iconService.registerAll([Apple16, Fish16]);
46+
}
47+
}
4248

4349
describe("ContainedList", () => {
4450
let component: ContainedList;
@@ -127,23 +133,31 @@ describe("ContainedList", () => {
127133
const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent);
128134
wrapperFixture.detectChanges();
129135

130-
const iconElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(2) svg"));
136+
const iconElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(2) svg[ibmIcon='fish']"));
137+
expect(iconElement).toBeTruthy();
138+
});
139+
140+
it("should render the icon", () => {
141+
const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent);
142+
wrapperFixture.detectChanges();
143+
144+
const iconElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(3) svg[ng-reflect-ibm-icon='apple']"));
131145
expect(iconElement).toBeTruthy();
132146
});
133147

134148
it("should render the action", () => {
135149
const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent);
136150
wrapperFixture.detectChanges();
137151

138-
const actionElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(3) ibm-icon-button"));
152+
const actionElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(4) ibm-icon-button"));
139153
expect(actionElement).toBeTruthy();
140154
});
141155

142156
it("should render with the clickable state", () => {
143157
const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent);
144158
wrapperFixture.detectChanges();
145159

146-
const clickableListItemElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(4)"));
160+
const clickableListItemElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(5)"));
147161
expect(clickableListItemElement.nativeElement).toHaveClass("cds--contained-list-item--clickable");
148162

149163
const buttonElement = clickableListItemElement.nativeElement.querySelector("button");

src/contained-list/contained-list.stories.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,11 @@ const withIconsTemplate: Story<ContainedList> = () => ({
202202
<svg ibmIcon="wheat" size="16"></svg>
203203
</ng-template>
204204
205-
<ng-template #strawberry let-icon>
206-
<svg ibmIcon="strawberry" size="16"></svg>
207-
</ng-template>
208-
209-
<ng-template #fish let-icon>
210-
<svg ibmIcon="fish" size="16"></svg>
211-
</ng-template>
212-
213205
<cds-contained-list label="List title">
214206
<cds-contained-list-item [icon]="apple">List item</cds-contained-list-item>
215207
<cds-contained-list-item [icon]="wheat">List item</cds-contained-list-item>
216-
<cds-contained-list-item [icon]="strawberry">List item</cds-contained-list-item>
217-
<cds-contained-list-item [icon]="fish">List item</cds-contained-list-item>
208+
<cds-contained-list-item icon="strawberry">List item</cds-contained-list-item>
209+
<cds-contained-list-item icon="fish">List item</cds-contained-list-item>
218210
</cds-contained-list>
219211
`
220212
});

0 commit comments

Comments
 (0)