|
9 | 9 | ViewChild, |
10 | 10 | AfterContentInit, |
11 | 11 | HostListener, |
12 | | - OnDestroy |
| 12 | + OnDestroy, |
| 13 | + TemplateRef |
13 | 14 | } from "@angular/core"; |
14 | 15 | import { NG_VALUE_ACCESSOR } from "@angular/forms"; |
15 | 16 |
|
@@ -66,7 +67,12 @@ import { DropdownService } from "./dropdown.service"; |
66 | 67 | <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> |
67 | 68 | </svg> |
68 | 69 | </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> |
70 | 76 | <ibm-icon-chevron-down16 |
71 | 77 | *ngIf="!skeleton" |
72 | 78 | class="bx--list-box__menu-icon" |
@@ -97,9 +103,9 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy { |
97 | 103 | */ |
98 | 104 | @Input() placeholder = ""; |
99 | 105 | /** |
100 | | - * The selected value from the `Dropdown`. |
| 106 | + * The selected value from the `Dropdown`. Can be a string or template. |
101 | 107 | */ |
102 | | - @Input() displayValue = ""; |
| 108 | + @Input() displayValue: string | TemplateRef<any> = ""; |
103 | 109 | /** |
104 | 110 | * Size to render the dropdown field. |
105 | 111 | */ |
@@ -358,23 +364,41 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy { |
358 | 364 | * if there is just a selection the ListItem content property will be returned, |
359 | 365 | * otherwise the placeholder will be returned. |
360 | 366 | */ |
361 | | - getDisplayValue(): Observable<string> { |
| 367 | + getDisplayStringValue(): Observable<string> { |
362 | 368 | if (!this.view) { |
363 | 369 | return; |
364 | 370 | } |
365 | 371 | let selected = this.view.getSelected(); |
366 | | - if (selected && !this.displayValue) { |
| 372 | + if (selected && (!this.displayValue || !this.isRenderString())) { |
367 | 373 | if (this.type === "multi") { |
368 | 374 | return of(this.placeholder); |
369 | 375 | } else { |
370 | 376 | return of(selected[0].content); |
371 | 377 | } |
372 | | - } else if (selected) { |
373 | | - return of(this.displayValue); |
| 378 | + } else if (selected && this.isRenderString()) { |
| 379 | + return of(this.displayValue as string); |
374 | 380 | } |
375 | 381 | return of(this.placeholder); |
376 | 382 | } |
377 | 383 |
|
| 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 | + |
378 | 402 | getSelectedCount(): number { |
379 | 403 | if (this.view.getSelected()) { |
380 | 404 | return this.view.getSelected().length; |
|
0 commit comments