File

src/tabs/tab-header.directive.ts

Deprecated

as of v5. Prefer `cds-tab-header` for icons, secondary labels, dismissable close, and icon-only tabs.

Description

Tab header as an attribute on a focusable host inside cds-tab-header-group.

Prefer cds-tab-header for icons, secondary labels, dismissable close, and icon-only tabs.

Extends

TabHeaderBase

Implements

AfterViewInit

Metadata

Index

Properties
Methods
Inputs
Outputs
HostBindings
HostListeners
Accessors

Constructor

constructor(host: ElementRef)
Parameters :
Name Type Optional
host ElementRef No

Inputs

active
Type : boolean
Default value : false
Inherited from TabHeaderBase
Defined in TabHeaderBase:53

Selected tab; controls whether the linked pane content is shown.

cacheActive
Type : boolean
Inherited from TabHeaderBase
Defined in TabHeaderBase:28

Set to 'true' to have pane reference cached and not reloaded on tab switching.

disabled
Type : boolean
Default value : false
Inherited from TabHeaderBase
Defined in TabHeaderBase:58

Indicates whether or not the Tab item is disabled.

dismissable
Type : boolean
Default value : false
Inherited from TabHeaderBase
Defined in TabHeaderBase:74

Set to true to render this tab header as dismissable.

icon
Type : TemplateRef<any>
Inherited from TabHeaderBase
Defined in TabHeaderBase:63

Icon template; used with cds-tab-header / cds-tab-header-group.

paneReference
Type : Tab
Inherited from TabHeaderBase
Defined in TabHeaderBase:79

Reference to the corresponding tab pane.

paneTabIndex
Type : number | null
Inherited from TabHeaderBase
Defined in TabHeaderBase:44

Sets tabIndex on the linked Tab pane when the pane reference is set.

secondaryLabel
Type : string
Inherited from TabHeaderBase
Defined in TabHeaderBase:69

Optional secondary label rendered below the primary tab label. Only displayed when the parent group is using type="contained".

title
Type : string
Inherited from TabHeaderBase
Defined in TabHeaderBase:84

Title attribute used as the tooltip for the tab item. Falls back to the tab item's text content if not provided.

Outputs

selected
Type : EventEmitter
Inherited from TabHeaderBase
Defined in TabHeaderBase:89

Emits when this header becomes selected.

tabClose
Type : EventEmitter
Inherited from TabHeaderBase
Defined in TabHeaderBase:94

Emits when this tabs's close button is pressed.

HostBindings

attr.aria-disabled
Type : boolean
attr.aria-selected
Type : boolean
attr.tabIndex
Type : 0 | -1
attr.title
Type : string
attr.type
Type : string
Default value : "button"
class.cds--tabs__nav-item
Type : boolean
Default value : true
class.cds--tabs__nav-item--disabled
Type : boolean
class.cds--tabs__nav-item--selected
Type : boolean
class.cds--tabs__nav-link
Type : boolean
Default value : true

HostListeners

click
keydown
Arguments : '$event'

Methods

focus
focus()
Inherited from TabHeaderBase
Defined in TabHeaderBase:180
Returns : void
ngAfterViewInit
ngAfterViewInit()
Returns : void
onClick
onClick()
Decorators :
@HostListener('click')
Returns : void
onKeyDown
onKeyDown(event: KeyboardEvent)
Decorators :
@HostListener('keydown', ['$event'])
Parameters :
Name Type Optional
event KeyboardEvent No
Returns : void
selectTab
selectTab()
Inherited from TabHeaderBase
Defined in TabHeaderBase:106

Activates the linked pane and emits selected.

Returns : void

Properties

navItem
Default value : true
Decorators :
@HostBinding('class.cds--tabs__nav-item')
navLink
Default value : true
Decorators :
@HostBinding('class.cds--tabs__nav-link')
type
Type : string
Default value : "button"
Decorators :
@HostBinding('attr.type')
Protected _cacheActive
Default value : false
Inherited from TabHeaderBase
Defined in TabHeaderBase:96

Accessors

tabIndex
gettabIndex()
isSelected
getisSelected()
isDisabled
getisDisabled()
ariaSelected
getariaSelected()
ariaDisabled
getariaDisabled()
hostTitle
gethostTitle()
import {
	Directive,
	Input,
	Output,
	EventEmitter,
	ElementRef,
	AfterViewInit,
	HostBinding,
	HostListener,
	TemplateRef,
	forwardRef
} from "@angular/core";

import { Tab } from "./tab.component";

/**
 * Shared inputs, outputs, and selection logic for `[cdsTabHeader]`
 * and `cds-tab-header` as we prepare for deprecation.
 * Groups use `@ContentChildren(TabHeaderBase)` so both forms appear in DOM order,
 * subclasses supply the template and host behavior.
 */
@Directive()
// eslint-disable-next-line @angular-eslint/directive-class-suffix -- abstract base class, not a directive instance
export abstract class TabHeaderBase {
	/**
	 * Set to 'true' to have pane reference cached and not reloaded on tab switching.
	 */
	@Input() set cacheActive(shouldCache: boolean) {
		this._cacheActive = shouldCache;

		// Updates the pane references associated with the tab header when cache active is changed.
		if (this.paneReference) {
			this.paneReference.cacheActive = this.cacheActive;
		}
	}

	get cacheActive() {
		return this._cacheActive;
	}

	/**
	 * Sets `tabIndex` on the linked `Tab` pane when the pane reference is set.
	 */
	@Input() set paneTabIndex(tabIndex: number | null) {
		if (this.paneReference) {
			this.paneReference.tabIndex = tabIndex;
		}
	}

	/**
	 * Selected tab; controls whether the linked pane content is shown.
	 */
	@Input() active = false;

	/**
	 * Indicates whether or not the `Tab` item is disabled.
	 */
	@Input() disabled = false;

	/**
	 * Icon template; used with `cds-tab-header` / `cds-tab-header-group`.
	 */
	@Input() icon: TemplateRef<any>;

	/**
	 * Optional secondary label rendered below the primary tab label.
	 * Only displayed when the parent group is using `type="contained"`.
	 */
	@Input() secondaryLabel: string;

	/**
	 * Set to `true` to render this tab header as dismissable.
	 */
	@Input() dismissable = false;

	/**
	 * Reference to the corresponding tab pane.
	 */
	@Input() paneReference: Tab;

	/**
	 * Title attribute used as the tooltip for the tab item. Falls back to the tab item's text content if not provided.
	 */
	@Input() title: string;

	/**
	 * Emits when this header becomes selected.
	 */
	@Output() selected = new EventEmitter<any>();

	/**
	 * Emits when this tabs's close button is pressed.
	 */
	@Output() tabClose = new EventEmitter<void>();

	protected _cacheActive = false;

	/**
	 * Move keyboard focus to the tab item.
	 */
	abstract focus(): void;

	/**
	 * Activates the linked pane and emits `selected`.
	 */
	selectTab() {
		this.focus();
		if (!this.disabled) {
			this.selected.emit();
			this.active = true;
			if (this.paneReference) {
				this.paneReference.active = true;
			}
		}
	}
}

/**
 * Tab header as an attribute on a focusable host inside `cds-tab-header-group`.
 *
 * @deprecated as of v5.
 * Prefer `cds-tab-header` for icons, secondary labels, dismissable close, and icon-only tabs.
 */
@Directive({
	selector: "[cdsTabHeader], [ibmTabHeader]",
	providers: [
		// tslint:disable-next-line:no-forward-ref
		{ provide: TabHeaderBase, useExisting: forwardRef(() => TabHeader) }
	]
})
export class TabHeader extends TabHeaderBase implements AfterViewInit {
	@HostBinding("attr.tabIndex") get tabIndex() {
		return this.active ? 0 : -1;
	}

	@HostBinding("class.cds--tabs__nav-item--selected") get isSelected() {
		return this.active;
	}

	@HostBinding("class.cds--tabs__nav-item--disabled") get isDisabled() {
		return this.disabled;
	}

	@HostBinding("attr.type") type = "button";
	@HostBinding("attr.aria-selected") get ariaSelected() {
		return this.active;
	}
	@HostBinding("attr.aria-disabled") get ariaDisabled() {
		return this.disabled;
	}
	@HostBinding("class.cds--tabs__nav-item") navItem = true;
	@HostBinding("class.cds--tabs__nav-link") navLink = true;
	@HostBinding("attr.title") get hostTitle() {
		return this.title ?? null;
	}

	constructor(private host: ElementRef) {
		super();
	}

	@HostListener("click")
	onClick() {
		this.selectTab();
	}

	@HostListener("keydown", ["$event"])
	onKeyDown(event: KeyboardEvent) {
		if (this.dismissable && event.key === "Delete") {
			event.stopPropagation();
			this.tabClose.emit();
		}
	}

	ngAfterViewInit() {
		setTimeout(() => {
			this.title = this.title ? this.title : this.host.nativeElement.textContent;
		});
	}

	focus() {
		this.host.nativeElement.focus();
	}
}

results matching ""

    No results matching ""