File
Implements
Index
Properties
|
|
|
Methods
|
|
|
Inputs
|
|
|
Outputs
|
|
|
HostBindings
|
|
|
HostListeners
|
|
|
Accessors
|
|
|
Constructor
constructor(renderer: Renderer2, hostElement: ElementRef)
|
|
|
Parameters :
| Name |
Type |
Optional |
| renderer |
Renderer2
|
No
|
| hostElement |
ElementRef
|
No
|
|
|
active
|
Type : boolean
|
|
|
Used to activate the option. Only one option may be active at a time
|
|
name
|
Type : string
|
Default value : "option"
|
|
|
Internal name for the option.
Should be something that identifies the option to the application.
Accessible from the ContentSwitcher selected emitter
|
Outputs
|
onClick
|
Type : EventEmitter
|
|
|
|
onFocus
|
Type : EventEmitter
|
|
|
|
selected
|
Type : EventEmitter
|
|
|
Emits when the option is selected.
|
HostBindings
|
attr.aria-selected
|
Type : boolean
|
Default value : false
|
|
|
|
attr.role
|
Type : string
|
Default value : "tab"
|
|
|
|
attr.tabIndex
|
Type : string
|
Default value : "-1"
|
|
|
|
class
|
Type : string
|
Default value : "cds--content-switcher-btn"
|
|
|
|
class.cds--content-switcher--selected
|
Type : boolean
|
Default value : false
|
|
|
HostListeners
|
click
|
Arguments : '$event'
|
|
|
|
focus
|
Arguments : '$event'
|
|
|
Methods
|
doFocus
|
doFocus(event: FocusEvent)
|
Decorators :
@HostListener('focus', ['$event'])
|
|
|
Parameters :
| Name |
Type |
Optional |
| event |
FocusEvent
|
No
|
|
|
hostClick
|
hostClick(event: MouseEvent)
|
Decorators :
@HostListener('click', ['$event'])
|
|
|
Parameters :
| Name |
Type |
Optional |
| event |
MouseEvent
|
No
|
|
|
Protected
_active
|
Default value : false
|
|
|
|
ariaSelected
|
Default value : false
|
Decorators :
@HostBinding('attr.aria-selected')
|
|
|
|
role
|
Type : string
|
Default value : "tab"
|
Decorators :
@HostBinding('attr.role')
|
|
|
|
selectedClass
|
Default value : false
|
Decorators :
@HostBinding('class.cds--content-switcher--selected')
|
|
|
|
Public
selectionMode
|
Type : "automatic" | "manual"
|
Default value : "automatic"
|
|
|
When automatic, the focused switcher will be selected by default. For manual,
user will have to manually select via Enter/space (which fire the native click handler)
|
|
switcherClass
|
Type : string
|
Default value : "cds--content-switcher-btn"
|
Decorators :
@HostBinding('class')
|
|
|
|
tabindex
|
Type : string
|
Default value : "-1"
|
Decorators :
@HostBinding('attr.tabIndex')
|
|
|
Accessors
|
active
|
getactive()
|
|
|
setactive(value: boolean)
|
|
|
Used to activate the option. Only one option may be active at a time
Parameters :
| Name |
Type |
Optional |
| value |
boolean
|
No
|
|
import {
Directive,
HostBinding,
Input,
HostListener,
Output,
EventEmitter,
ElementRef,
OnInit,
Renderer2
} from "@angular/core";
@Directive({
selector: "[cdsContentOption], [ibmContentOption]"
})
export class ContentSwitcherOption implements OnInit {
/**
* Used to activate the option. Only one option may be `active` at a time
*/
@Input() set active (value: boolean) {
this._active = value;
this.selectedClass = value;
this.ariaSelected = value;
this.tabindex = value ? "0" : "-1";
}
get active() {
return this._active;
}
/**
* Internal name for the option.
* Should be something that identifies the option to the application.
* Accessible from the `ContentSwitcher` `selected` emitter
*/
@Input() name = "option";
/**
* Emits when the option is selected.
*/
@Output() selected = new EventEmitter<boolean>();
@Output() onClick = new EventEmitter<MouseEvent>();
@Output() onFocus = new EventEmitter<FocusEvent>();
@HostBinding("class") switcherClass = "cds--content-switcher-btn";
@HostBinding("class.cds--content-switcher--selected") selectedClass = false;
@HostBinding("attr.role") role = "tab";
@HostBinding("attr.aria-selected") ariaSelected = false;
@HostBinding("attr.tabIndex") tabindex = "-1";
/**
* @internal
* When `automatic`, the focused switcher will be selected by default. For `manual`,
* user will have to manually select via Enter/space (which fire the native click handler)
*/
public selectionMode: "automatic" | "manual" = "automatic";
protected _active = false;
constructor(private renderer: Renderer2, private hostElement: ElementRef) {}
@HostListener("click", ["$event"])
hostClick(event: MouseEvent) {
this.onClick.emit(event);
// skip setting and emitting if the option is already active
if (this.active) { return; }
this.active = true;
this.selected.emit(true);
}
@HostListener("focus", ["$event"])
doFocus(event: FocusEvent) {
this.onFocus.emit(event);
// skip setting and emitting if the option is already active
if (this.active) { return; }
// In manual selection mode, focus only moves focus, not select switcher
if (this.selectionMode === "manual") {
return;
}
this.active = true;
this.selected.emit(true);
}
/*
* Wrap projected content in a span with the `cds--content-switcher__label` class.
*/
ngOnInit(): void {
const hostNativeElement = (this.hostElement.nativeElement as HTMLElement);
const spanWrapper = this.renderer.createElement("span");
this.renderer.addClass(spanWrapper, "cds--content-switcher__label");
const hostChildren: ChildNode[] = [];
hostNativeElement.childNodes.forEach(node => hostChildren.push(node));
hostChildren.forEach(node => {
this.renderer.removeChild(hostNativeElement, node);
this.renderer.appendChild(spanWrapper, node);
});
this.renderer.appendChild(hostNativeElement, spanWrapper);
}
}