Skip to content

Commit 93f696e

Browse files
authored
Merge pull request #540 from rudolpho1/master
Support render template in dropdown component
2 parents 3b6a429 + 16e7898 commit 93f696e

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

src/dropdown/dropdown.component.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
ViewChild,
1010
AfterContentInit,
1111
HostListener,
12-
OnDestroy
12+
OnDestroy,
13+
TemplateRef
1314
} from "@angular/core";
1415
import { NG_VALUE_ACCESSOR } from "@angular/forms";
1516

@@ -66,7 +67,12 @@ import { DropdownService } from "./dropdown.service";
6667
<path d="M12 4.7l-.7-.7L8 7.3 4.7 4l-.7.7L7.3 8 4 11.3l.7.7L8 8.7l3.3 3.3.7-.7L8.7 8z"></path>
6768
</svg>
6869
</div>
69-
<span class="bx--list-box__label">{{getDisplayValue() | async}}</span>
70+
<span *ngIf="isRenderString()" class="bx--list-box__label">{{getDisplayStringValue() | async}}</span>
71+
<ng-template
72+
*ngIf="!isRenderString()"
73+
[ngTemplateOutletContext]="getRenderTemplateContext()"
74+
[ngTemplateOutlet]="displayValue">
75+
</ng-template>
7076
<ibm-icon-chevron-down16
7177
*ngIf="!skeleton"
7278
class="bx--list-box__menu-icon"
@@ -97,9 +103,9 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
97103
*/
98104
@Input() placeholder = "";
99105
/**
100-
* The selected value from the `Dropdown`.
106+
* The selected value from the `Dropdown`. Can be a string or template.
101107
*/
102-
@Input() displayValue = "";
108+
@Input() displayValue: string | TemplateRef<any> = "";
103109
/**
104110
* Size to render the dropdown field.
105111
*/
@@ -358,23 +364,41 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
358364
* if there is just a selection the ListItem content property will be returned,
359365
* otherwise the placeholder will be returned.
360366
*/
361-
getDisplayValue(): Observable<string> {
367+
getDisplayStringValue(): Observable<string> {
362368
if (!this.view) {
363369
return;
364370
}
365371
let selected = this.view.getSelected();
366-
if (selected && !this.displayValue) {
372+
if (selected && (!this.displayValue || !this.isRenderString())) {
367373
if (this.type === "multi") {
368374
return of(this.placeholder);
369375
} else {
370376
return of(selected[0].content);
371377
}
372-
} else if (selected) {
373-
return of(this.displayValue);
378+
} else if (selected && this.isRenderString()) {
379+
return of(this.displayValue as string);
374380
}
375381
return of(this.placeholder);
376382
}
377383

384+
isRenderString(): boolean {
385+
return typeof this.displayValue === "string";
386+
}
387+
388+
getRenderTemplateContext() {
389+
if (!this.view) {
390+
return;
391+
}
392+
let selected = this.view.getSelected();
393+
if (this.type === "multi") {
394+
return {items: selected};
395+
} else if (selected && selected.length > 0) {
396+
return {item: selected[0]}; // this is to be compatible with the dropdown-list template
397+
} else {
398+
return {};
399+
}
400+
}
401+
378402
getSelectedCount(): number {
379403
if (this.view.getSelected()) {
380404
return this.view.getSelected().length;

src/dropdown/dropdown.stories.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,43 @@ storiesOf("Dropdown", module)
118118
theme: select("theme", ["dark", "light"], "dark")
119119
}
120120
}))
121+
.add("With Template", () => ({
122+
template: `
123+
<div style="width: 300px;">
124+
<ibm-dropdown
125+
[theme]="theme"
126+
placeholder="Select"
127+
[displayValue]="dropdownRenderer"
128+
[disabled]="disabled"
129+
(selected)="selected($event)"
130+
(onClose)="onClose($event)">
131+
<ibm-dropdown-list [items]="items" [listTpl]="dropdownRenderer"></ibm-dropdown-list>
132+
</ibm-dropdown>
133+
<ng-template #dropdownRenderer let-item="item">
134+
<div *ngIf="item && item.content" style="font-size: 14px;">
135+
<svg focusable="false" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg"
136+
width="16" height="16" viewBox="0 0 16 16" aria-hidden="true" style="will-change: transform;">
137+
<path d="M9.3 3.7l3.8 3.8H1v1h12.1l-3.8 3.8.7.7 5-5-5-5z"></path>
138+
</svg>
139+
&nbsp;{{item.content}}
140+
</div>
141+
</ng-template>
142+
</div>
143+
`,
144+
props: {
145+
disabled: boolean("disabled", false),
146+
items: object("items", [
147+
{ content: "one", selected: true },
148+
{ content: "two" },
149+
{ content: "three" },
150+
{ content: "four" }
151+
]),
152+
selected: action("Selected fired for dropdown"),
153+
onClose: action("Dropdown closed"),
154+
theme: select("theme", ["dark", "light"], "dark")
155+
}
156+
157+
}))
121158
.add("Skeleton", () => ({
122159
template: `
123160
<div style="width: 300px">

0 commit comments

Comments
 (0)