src/tabs/tab.component.ts
The Tab component is a child of the Tabs component.
It represents one Tab item and its content within a panel of other Tab items.
Tab takes a string or TemplateRef for the header, and any content for the body of the tab.
Disabled states should be handled by the application (ie. switch to the tab, but display some
indication as to why the tab is disabled).
When the tab is selected the select output will be triggered.
The select output will also be triggered for the active tab when the tabs are loaded or updated.
Tab with string header:
Example :<cds-tab heading='tab1'>
tab 1 content
</cds-tab>Tab with custom header:
Example :<ng-template #tabHeading>
<svg cdsIcon="facebook"
size="sm"
style="margin-right: 7px;">
</svg>
Hello Tab 1
</ng-template>
<cds-tabs>
<cds-tab [heading]="tabHeading">
Tab 1 content <svg cdsIcon="alert" size="lg"></svg>
</cds-tab>
<cds-tab heading='Tab2'>
Tab 2 content
</cds-tab>
<cds-tab heading='Tab3'>
Tab 3 content
</cds-tab>
</cds-tabs>
OnInit
| selector | cds-tab, ibm-tab |
| template | |
Properties |
|
Methods |
|
Inputs |
Outputs |
HostBindings |
Accessors |
| active | |
Type : boolean
|
|
Default value : false
|
|
|
Defined in src/tabs/tab.component.ts:125
|
|
|
Indicates whether the |
|
| badgeIndicator | |
Type : boolean
|
|
Default value : false
|
|
|
Defined in src/tabs/tab.component.ts:172
|
|
|
Preview: Icon-only tabs — show a notification dot on the icon. |
|
| cacheActive | |
Type : boolean
|
|
|
Defined in src/tabs/tab.component.ts:89
|
|
|
Set to |
|
| closeButtonAriaLabel | |
Type : string
|
|
Default value : "Press delete to remove tab"
|
|
|
Defined in src/tabs/tab.component.ts:160
|
|
|
Sets the aria-label of the close button when the parent |
|
| context | |
Type : any
|
|
|
Defined in src/tabs/tab.component.ts:120
|
|
|
Allows the user to pass data to the custom template for the |
|
| disabled | |
Type : boolean
|
|
Default value : false
|
|
|
Defined in src/tabs/tab.component.ts:129
|
|
|
Indicates whether or not the |
|
| enterDelayMs | |
Type : number
|
|
|
Defined in src/tabs/tab.component.ts:176
|
|
|
Icon-only tabs: tooltip show delay (ms). |
|
| heading | |
Type : string | TemplateRef<any>
|
|
|
Defined in src/tabs/tab.component.ts:109
|
|
|
The |
|
| icon | |
Type : TemplateRef<any>
|
|
|
Defined in src/tabs/tab.component.ts:151
|
|
|
Optional template that renders an icon inside the |
|
| iconLabel | |
Type : string
|
|
|
Defined in src/tabs/tab.component.ts:168
|
|
|
Icon-only tabs: accessible name and tooltip text. |
|
| iconOnly | |
Type : boolean
|
|
Default value : false
|
|
|
Defined in src/tabs/tab.component.ts:164
|
|
|
Icon-only tab: pair with |
|
| id | |
Type : string
|
|
Default value : `n-tab-${Tab.counter++}`
|
|
|
Defined in src/tabs/tab.component.ts:138
|
|
|
Sets the id of the |
|
| isTooltipOpen | |
Type : boolean
|
|
Default value : false
|
|
|
Defined in src/tabs/tab.component.ts:184
|
|
|
Icon-only tabs: open the tooltip on first render. |
|
| leaveDelayMs | |
Type : number
|
|
|
Defined in src/tabs/tab.component.ts:180
|
|
|
Icon-only tabs: tooltip hide delay (ms). |
|
| secondaryLabel | |
Type : string
|
|
|
Defined in src/tabs/tab.component.ts:156
|
|
|
Optional secondary label rendered below the primary tab label.
Only displayed when the parent |
|
| tabContent | |
Type : TemplateRef<any>
|
|
|
Defined in src/tabs/tab.component.ts:142
|
|
|
Allows lifecycle hooks to be called on the rendered content. |
|
| tabIndex | |
Type : number
|
|
Default value : 0
|
|
|
Defined in src/tabs/tab.component.ts:134
|
|
|
|
|
| templateContext | |
Type : any
|
|
|
Defined in src/tabs/tab.component.ts:146
|
|
|
Optional data for templates passed as implicit context. |
|
| title | |
Type : string
|
|
|
Defined in src/tabs/tab.component.ts:116
|
|
|
Optional override for the You might want to use this if you set |
|
| selected | |
Type : EventEmitter<void>
|
|
|
Defined in src/tabs/tab.component.ts:188
|
|
|
Emits when this tab becomes selected. |
|
| tabClose | |
Type : EventEmitter<void>
|
|
|
Defined in src/tabs/tab.component.ts:192
|
|
|
Emits when this tab's close button is pressed. |
|
| attr.aria-labelledby |
Type : string
|
|
Defined in src/tabs/tab.component.ts:71
|
| attr.aria-live |
Type : string
|
Default value : "polite"
|
|
Defined in src/tabs/tab.component.ts:99
|
| attr.hidden |
Type : string
|
|
Defined in src/tabs/tab.component.ts:80
|
|
|
| attr.id |
Type : string
|
|
Defined in src/tabs/tab.component.ts:68
|
| attr.role |
Type : string
|
Default value : "tabpanel"
|
|
Defined in src/tabs/tab.component.ts:98
|
| attr.tabindex |
Type : number
|
|
Defined in src/tabs/tab.component.ts:74
|
| class.cds--tab-content |
Type : boolean
|
Default value : true
|
|
Defined in src/tabs/tab.component.ts:97
|
| style.display |
Type : "block" | "none"
|
|
Defined in src/tabs/tab.component.ts:83
|
| doSelect |
doSelect()
|
|
Defined in src/tabs/tab.component.ts:209
|
|
Emit the status of the
Returns :
void
|
| Public isTemplate | ||||
isTemplate(value)
|
||||
|
Defined in src/tabs/tab.component.ts:220
|
||||
|
Parameters :
Returns :
boolean
|
| ngOnInit |
ngOnInit()
|
|
Defined in src/tabs/tab.component.ts:200
|
|
Checks for custom heading template on initialization and updates the value of the boolean 'headingIsTemplate'.
Returns :
void
|
| shouldRender |
shouldRender()
|
|
Defined in src/tabs/tab.component.ts:216
|
|
Returns value indicating whether this
Returns :
boolean
|
| Protected _cacheActive |
Default value : false
|
|
Defined in src/tabs/tab.component.ts:194
|
| Private Static counter |
Type : number
|
Default value : 0
|
|
Defined in src/tabs/tab.component.ts:96
|
| Public headingIsTemplate |
Default value : false
|
|
Defined in src/tabs/tab.component.ts:104
|
|
Boolean value reflects if the |
| panelAriaLive |
Type : string
|
Default value : "polite"
|
Decorators :
@HostBinding('attr.aria-live')
|
|
Defined in src/tabs/tab.component.ts:99
|
| panelRole |
Type : string
|
Default value : "tabpanel"
|
Decorators :
@HostBinding('attr.role')
|
|
Defined in src/tabs/tab.component.ts:98
|
| tabContentClass |
Default value : true
|
Decorators :
@HostBinding('class.cds--tab-content')
|
|
Defined in src/tabs/tab.component.ts:97
|
| hostId |
gethostId()
|
|
Defined in src/tabs/tab.component.ts:68
|
| hostAriaLabelledby |
gethostAriaLabelledby()
|
|
Defined in src/tabs/tab.component.ts:71
|
| hostTabIndex |
gethostTabIndex()
|
|
Defined in src/tabs/tab.component.ts:74
|
| hostHidden |
gethostHidden()
|
|
Defined in src/tabs/tab.component.ts:80
|
|
|
| hostDisplay |
gethostDisplay()
|
|
Defined in src/tabs/tab.component.ts:83
|
| cacheActive | ||||||
getcacheActive()
|
||||||
|
Defined in src/tabs/tab.component.ts:93
|
||||||
setcacheActive(shouldCache: boolean)
|
||||||
|
Defined in src/tabs/tab.component.ts:89
|
||||||
|
Set to
Parameters :
Returns :
void
|
import {
Component,
OnInit,
Input,
Output,
EventEmitter,
HostBinding,
TemplateRef
} from "@angular/core";
/**
* The `Tab` component is a child of the `Tabs` component.
* It represents one `Tab` item and its content within a panel of other `Tab` items.
*
* `Tab` takes a string or `TemplateRef` for the header, and any content for the body of the tab.
* Disabled states should be handled by the application (ie. switch to the tab, but display some
* indication as to _why_ the tab is disabled).
*
* When the tab is selected the `select` output will be triggered.
* The `select` output will also be triggered for the active tab when the tabs are loaded or updated.
*
*
* Tab with string header:
*
* ```html
* <cds-tab heading='tab1'>
* tab 1 content
* </cds-tab>
* ```
*
* Tab with custom header:
*
* ```html
* <ng-template #tabHeading>
* <svg cdsIcon="facebook"
* size="sm"
* style="margin-right: 7px;">
* </svg>
* Hello Tab 1
* </ng-template>
* <cds-tabs>
* <cds-tab [heading]="tabHeading">
* Tab 1 content <svg cdsIcon="alert" size="lg"></svg>
* </cds-tab>
* <cds-tab heading='Tab2'>
* Tab 2 content
* </cds-tab>
* <cds-tab heading='Tab3'>
* Tab 3 content
* </cds-tab>
* </cds-tabs>
* ```
*/
@Component({
selector: "cds-tab, ibm-tab",
template: `
<ng-container *ngIf="shouldRender()">
<ng-template
*ngIf="isTemplate(tabContent)"
[ngTemplateOutlet]="tabContent"
[ngTemplateOutletContext]="{ $implicit: templateContext }">
</ng-template>
<ng-content></ng-content>
</ng-container>
`
})
export class Tab implements OnInit {
@HostBinding("attr.id") get hostId() {
return this.id;
}
@HostBinding("attr.aria-labelledby") get hostAriaLabelledby() {
return `${this.id}-header`;
}
@HostBinding("attr.tabindex") get hostTabIndex() {
return this.tabIndex;
}
/**
* `hidden` + display keep inactive panels out of layout; `null` display when active preserves grid/flex.
*/
@HostBinding("attr.hidden") get hostHidden() {
return this.active ? null : "";
}
@HostBinding("style.display") get hostDisplay() {
return this.active ? "block" : "none";
}
/**
* Set to `true` to have `Tab` items cached and not reloaded on tab switching.
*/
@Input() set cacheActive(shouldCache: boolean) {
this._cacheActive = shouldCache;
}
get cacheActive() {
return this._cacheActive;
}
private static counter = 0;
@HostBinding("class.cds--tab-content") tabContentClass = true;
@HostBinding("attr.role") panelRole = "tabpanel";
@HostBinding("attr.aria-live") panelAriaLive = "polite";
/**
* Boolean value reflects if the `Tab` is using a custom template for the heading.
* Default value is false.
*/
public headingIsTemplate = false;
/**
* The `Tab`'s title to be displayed or custom template for the `Tab` heading.
*/
@Input() heading: string | TemplateRef<any>;
/**
* Optional override for the `tabItem's`'s title attribute which is set in `TabHeaders`.
* `tabItem`'s title attribute is automatically set to `heading`.
*
* You might want to use this if you set `heading` to a `TemplateRef`.
*/
@Input() title: string;
/**
* Allows the user to pass data to the custom template for the `Tab` heading.
*/
@Input() context: any;
/**
* Indicates whether the `Tab` is active/selected.
* Determines whether its tab panel content is rendered.
*/
@Input() active = false;
/**
* Indicates whether or not the `Tab` item is disabled.
*/
@Input() disabled = false;
/**
* `tabindex` on the tab panel, the parent may set this to `null` when `isNavigation` is `true`.
*/
@Input() tabIndex = 0;
/**
* Sets the id of the `Tab`. Will be uniquely generated if not provided.
*/
@Input() id = `n-tab-${Tab.counter++}`;
/**
* Allows lifecycle hooks to be called on the rendered content.
*/
@Input() tabContent: TemplateRef<any>;
/**
* Optional data for templates passed as implicit context.
*/
@Input() templateContext: any;
/**
* Optional template that renders an icon inside the `Tab` header.
* Useful for rendering a `cdsIcon` or any other icon next to the tab label.
*/
@Input() icon: TemplateRef<any>;
/**
* Optional secondary label rendered below the primary tab label.
* Only displayed when the parent `Tabs` is using `type="contained"`.
*/
@Input() secondaryLabel: string;
/**
* Sets the aria-label of the close button when the parent `Tabs` uses `dismissable`.
*/
@Input() closeButtonAriaLabel = "Press delete to remove tab";
/**
* Icon-only tab: pair with `icon` and `iconLabel`.
*/
@Input() iconOnly = false;
/**
* Icon-only tabs: accessible name and tooltip text.
*/
@Input() iconLabel: string;
/**
* **Preview**: Icon-only tabs — show a notification dot on the icon.
*/
@Input() badgeIndicator = false;
/**
* Icon-only tabs: tooltip show delay (ms).
*/
@Input() enterDelayMs: number;
/**
* Icon-only tabs: tooltip hide delay (ms).
*/
@Input() leaveDelayMs: number;
/**
* Icon-only tabs: open the tooltip on first render.
*/
@Input() isTooltipOpen = false;
/**
* Emits when this tab becomes selected.
*/
@Output() selected: EventEmitter<void> = new EventEmitter<void>();
/**
* Emits when this tab's close button is pressed.
*/
@Output() tabClose: EventEmitter<void> = new EventEmitter<void>();
protected _cacheActive = false;
/**
* Checks for custom heading template on initialization and updates the value
* of the boolean 'headingIsTemplate'.
*/
ngOnInit() {
if (this.heading instanceof TemplateRef) {
this.headingIsTemplate = true;
}
}
/**
* Emit the status of the `Tab`, specifically 'select' and 'selected' properties.
*/
doSelect() {
this.selected.emit();
}
/**
* Returns value indicating whether this `Tab` should be rendered in a tab panel.
*/
shouldRender() {
return this.active || this.cacheActive;
}
public isTemplate(value) {
return value instanceof TemplateRef;
}
}