Skip to content

Commit bd18c53

Browse files
committed
feat(carousel-indicators): allow custom content via TemplateId directive, refactor
1 parent d8c0f4e commit bd18c53

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<div class="carousel-indicators">
1+
@let tmpl = templates();
2+
<ng-container *ngTemplateOutlet="tmpl['carouselIndicatorsTemplate'] || defaultCarouselIndicatorsTemplate; context: {$implicit: items, active: active}" />
3+
4+
<ng-template #defaultCarouselIndicatorsTemplate>
25
@for (item of items; track item; let i = $index) {
36
<button
47
[attr.data-coreui-target]="i"
@@ -8,4 +11,4 @@
811
[attr.aria-current]="active === i">
912
</button>
1013
}
11-
</div>
14+
</ng-template>

projects/coreui-angular/src/lib/carousel/carousel-indicators/carousel-indicators.component.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { Component, DestroyRef, inject, OnInit } from '@angular/core';
1+
import { Component, computed, contentChildren, DestroyRef, inject, OnInit, TemplateRef } from '@angular/core';
22

33
import { CarouselState } from '../carousel-state';
44
import { CarouselService } from '../carousel.service';
55
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
6+
import { NgTemplateOutlet } from '@angular/common';
7+
import { TemplateIdDirective } from '../../shared';
68

79
@Component({
810
selector: 'c-carousel-indicators',
9-
templateUrl: './carousel-indicators.component.html'
11+
exportAs: 'cCarouselIndicators',
12+
imports: [NgTemplateOutlet],
13+
templateUrl: './carousel-indicators.component.html',
14+
host: { class: 'carousel-indicators' }
1015
})
1116
export class CarouselIndicatorsComponent implements OnInit {
1217
readonly #destroyRef = inject(DestroyRef);
@@ -16,6 +21,18 @@ export class CarouselIndicatorsComponent implements OnInit {
1621
items: (number | undefined)[] = [];
1722
active = 0;
1823

24+
readonly contentTemplates = contentChildren(TemplateIdDirective, { descendants: true });
25+
26+
readonly templates = computed(() => {
27+
return this.contentTemplates().reduce(
28+
(acc, child) => {
29+
acc[child.id] = child.templateRef;
30+
return acc;
31+
},
32+
{} as Record<string, TemplateRef<any>>
33+
);
34+
});
35+
1936
ngOnInit(): void {
2037
this.#carouselService.carouselIndex$.pipe(takeUntilDestroyed(this.#destroyRef)).subscribe((nextIndex) => {
2138
this.items = this.#carouselState?.state?.items?.map((item) => item.index) ?? [];

0 commit comments

Comments
 (0)