Skip to content

Commit 3e36538

Browse files
authored
fix: allow users to pass template for label (#2923)
Signed-off-by: Akshat Patel <[email protected]>
1 parent 9a7e305 commit 3e36538

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/treeview/tree-node.component.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ import { Node } from "./tree-node.types";
8080
size="16">
8181
</svg>
8282
</ng-container>
83-
<ng-template *ngIf="isTemplate(icon)" [ngTemplateOutlet]="icon"></ng-template>
83+
<ng-template
84+
*ngIf="isTemplate(icon)"
85+
[ngTemplateOutlet]="icon"
86+
[ngTemplateOutletContext]="{ $implicit: iconContext }">
87+
</ng-template>
8488
{{label}}
8589
</span>
8690
</div>
@@ -113,6 +117,7 @@ export class TreeNodeComponent implements AfterContentChecked, OnInit, OnDestroy
113117
@Input() selected = false;
114118
@Input() value;
115119
@Input() icon: string | TemplateRef<any>;
120+
@Input() iconContext: any;
116121
@Input() children: Node[] = [];
117122

118123
/**
@@ -138,6 +143,7 @@ export class TreeNodeComponent implements AfterContentChecked, OnInit, OnDestroy
138143
this.selected = node.selected ?? this.selected;
139144
this.depth = node.depth ?? this.depth;
140145
this.children = node.children ?? this.children;
146+
this.iconContext = node.iconText ?? this.iconContext;
141147
}
142148

143149
get node() {

src/treeview/tree-node.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface Node {
99
expanded?: boolean;
1010
selected?: boolean;
1111
icon?: string | TemplateRef<any>;
12+
iconContext?: any;
1213
children?: Node[];
1314
[key: string]: any;
1415
}

src/treeview/treeview.component.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ import { TreeViewService } from "./treeview.service";
3232
*ngIf="label"
3333
[id]="id"
3434
class="cds--label">
35-
{{label}}
35+
<ng-container *ngIf="!isTemplate(label)">{{label}}</ng-container>
36+
<ng-template
37+
*ngIf="isTemplate(label)"
38+
[ngTemplateOutlet]="label"
39+
[ngTemplateOutletContext]="{ $implicit: labelContext }">
40+
</ng-template>
3641
</label>
3742
<div
3843
class="cds--tree"
@@ -80,6 +85,10 @@ export class TreeViewComponent implements AfterViewInit, OnInit, OnDestroy {
8085
* Tree view label
8186
*/
8287
@Input() label: string | TemplateRef<any>;
88+
/**
89+
* Optional context for label if it's a template
90+
*/
91+
@Input() labelContext: any;
8392
/**
8493
* Specify the size of the list items in the tree
8594
*/
@@ -154,6 +163,10 @@ export class TreeViewComponent implements AfterViewInit, OnInit, OnDestroy {
154163
}
155164
}
156165

166+
public isTemplate(value) {
167+
return value instanceof TemplateRef;
168+
}
169+
157170
public isProjected() {
158171
return this.treeViewService.contentProjected;
159172
}

0 commit comments

Comments
 (0)