Skip to content

Commit 22736a1

Browse files
committed
fix: Allow users to pass in templates in tabs to allow onDestroy hook to be called for content
Signed-off-by: Akshat Patel <[email protected]>
1 parent 8487fc4 commit 22736a1

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/tabs/tab.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ import {
6363
[ngStyle]="{'display': active ? null : 'none'}"
6464
[attr.aria-labelledby]="id + '-header'"
6565
aria-live="polite">
66-
<ng-content></ng-content>
66+
<ng-template *ngIf="isTemplate(tabContent); else projectedContent" [ngTemplateOutlet]="tabContent"></ng-template>
6767
</div>
68+
<ng-template #projectedContent>
69+
<ng-content></ng-content>
70+
</ng-template>
71+
6872
`
6973
})
7074
export class Tab implements OnInit {
@@ -111,6 +115,9 @@ export class Tab implements OnInit {
111115
@Input() set cacheActive(shouldCache: boolean) {
112116
this._cacheActive = shouldCache;
113117
}
118+
119+
@Input() tabContent: TemplateRef<any>;
120+
114121
/**
115122
* Value 'selected' to be emitted after a new `Tab` is selected.
116123
*/
@@ -150,4 +157,8 @@ export class Tab implements OnInit {
150157
shouldRender() {
151158
return this.active || this.cacheActive;
152159
}
160+
161+
public isTemplate(value) {
162+
return value instanceof TemplateRef;
163+
}
153164
}

src/tabs/tabs.stories.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,24 @@ const Template = (args) => ({
2727
[followFocus]="followFocus"
2828
[isNavigation]="isNavigation"
2929
[cacheActive]="cacheActive">
30-
<cds-tab heading="one">Tab Content 1</cds-tab>
31-
<cds-tab heading="two">Tab Content 2</cds-tab>
32-
<cds-tab heading="three">Tab Content 3</cds-tab>
30+
<cds-tab heading="one" [tabContent]="one"></cds-tab>
31+
<cds-tab heading="two" [tabContent]="two"></cds-tab>
32+
<cds-tab heading="three" [tabContent]="three"></cds-tab>
3333
<cds-tab heading="three">Tab Content 3</cds-tab>
3434
</cds-tabs>
35+
36+
37+
<ng-template #one>
38+
Tab Content 1
39+
</ng-template>
40+
41+
<ng-template #two>
42+
Tab Content 2
43+
</ng-template>
44+
45+
<ng-template #three>
46+
Tab Content 3
47+
</ng-template>
3548
`
3649
});
3750
export const Basic = Template.bind({});

0 commit comments

Comments
 (0)