Skip to content

Commit e167388

Browse files
authored
Merge pull request #2910 from Akshat55/tab-template-projection
fix: Allow users to pass in templates in tabs to allow onDestroy hook to be called for content
2 parents 51098fe + 73d0886 commit e167388

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/tabs/tab.component.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ import {
6363
[ngStyle]="{'display': active ? null : 'none'}"
6464
[attr.aria-labelledby]="id + '-header'"
6565
aria-live="polite">
66+
<ng-template
67+
*ngIf="isTemplate(tabContent)"
68+
[ngTemplateOutlet]="tabContent"
69+
[ngTemplateOutletContext]="{ $implicit: templateContext }">
70+
</ng-template>
6671
<ng-content></ng-content>
6772
</div>
6873
`
@@ -111,11 +116,18 @@ export class Tab implements OnInit {
111116
@Input() set cacheActive(shouldCache: boolean) {
112117
this._cacheActive = shouldCache;
113118
}
119+
/**
120+
* Allows life cycle hooks to be called on the rendered content
121+
*/
122+
@Input() tabContent: TemplateRef<any>;
123+
/**
124+
* Optional data for templates passed as implicit context
125+
*/
126+
@Input() templateContext: any;
114127
/**
115128
* Value 'selected' to be emitted after a new `Tab` is selected.
116129
*/
117130
@Output() selected: EventEmitter<void> = new EventEmitter<void>();
118-
119131
/**
120132
* Used to set the id property on the element.
121133
*/
@@ -150,4 +162,8 @@ export class Tab implements OnInit {
150162
shouldRender() {
151163
return this.active || this.cacheActive;
152164
}
165+
166+
public isTemplate(value) {
167+
return value instanceof TemplateRef;
168+
}
153169
}

src/tabs/tabs.stories.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,18 @@ const Template = (args) => ({
2929
[cacheActive]="cacheActive">
3030
<cds-tab heading="one">Tab Content 1</cds-tab>
3131
<cds-tab heading="two">Tab Content 2</cds-tab>
32-
<cds-tab heading="three">Tab Content 3</cds-tab>
33-
<cds-tab heading="three">Tab Content 3</cds-tab>
32+
<cds-tab heading="three" [tabContent]="three"></cds-tab>
33+
<cds-tab heading="four" [tabContent]="four"></cds-tab>
3434
</cds-tabs>
35+
36+
<!-- Use templates if you would like to have lifecycle hooks called when cacheActive is false -->
37+
<ng-template #three>
38+
Tab Content 3
39+
</ng-template>
40+
41+
<ng-template #four>
42+
Tab Content 4
43+
</ng-template>
3544
`
3645
});
3746
export const Basic = Template.bind({});

0 commit comments

Comments
 (0)