File

src/ui-shell/sidenav/sidenav-item.component.ts

Description

SideNavItem can either be a child of SideNav or SideNavMenu

Implements

OnChanges

Metadata

selector ibm-sidenav-item
template
<li [ngClass]="{
	'bx--side-nav__item': !isSubMenu,
	'bx--side-nav__menu-item': isSubMenu
}"
[attr.role]="(isSubMenu ? 'none' : null)">
	<a
		class="bx--side-nav__link"
		[href]="href"
		[attr.role]="(isSubMenu ? 'menuitem' : null)"
		[attr.aria-current]="(active ? 'page' : null)"
		(click)="navigate($event)">
		<div *ngIf="!isSubMenu" class="bx--side-nav__icon">
			<ng-content select="[icon]"></ng-content>
		</div>
		<span class="bx--side-nav__link-text">
			<ng-content></ng-content>
		</span>
	</a>
</li>
	

Index

Properties
Methods
Inputs
Outputs
Accessors

Constructor

constructor(domSanitizer: DomSanitizer, router: Router)
Parameters :
Name Type Optional
domSanitizer DomSanitizer No
router Router No

Inputs

active

Toggles the active (current page) state for the link.

Default value : false

href

Link for the item. NOTE: do not pass unsafe or untrusted values, this has the potential to open you up to XSS attacks

Type : string

isSubMenu

Default value : false

route

Array of commands to send to the router when the link is activated See: https://angular.io/api/router/Router#navigate

Type : any[]

routeExtras

Router options. Used in conjunction with route See: https://angular.io/api/router/Router#navigate

Type : any

Outputs

navigation

Emits the navigation status promise when the link is activated

$event Type: EventEmitter
selected

Emits when active input is changed. This is mainly used to indicate to any parent menu items that a child sidenav item is active or not active.

$event Type: EventEmitter

Methods

navigate
navigate(event)
Parameters :
Name Optional
event No
Returns : void
ngOnChanges
ngOnChanges(changes)
Parameters :
Name Optional
changes No
Returns : void

Properties

Protected _href
_href: string
Type : string
Default value : "javascript:void(0)"

Accessors

href
gethref()
sethref(v: string)

Link for the item. NOTE: do not pass unsafe or untrusted values, this has the potential to open you up to XSS attacks

Parameters :
Name Type Optional
v string No
Returns : void
import {
	Component,
	Input,
	Optional,
	Output,
	EventEmitter,
	OnChanges
} from "@angular/core";
import { DomSanitizer } from "@angular/platform-browser";
import { Router } from "@angular/router";

/**
 * `SideNavItem` can either be a child of `SideNav` or `SideNavMenu`
 */
@Component({
	selector: "ibm-sidenav-item",
	template: `
		<li [ngClass]="{
			'bx--side-nav__item': !isSubMenu,
			'bx--side-nav__menu-item': isSubMenu
		}"
		[attr.role]="(isSubMenu ? 'none' : null)">
			<a
				class="bx--side-nav__link"
				[href]="href"
				[attr.role]="(isSubMenu ? 'menuitem' : null)"
				[attr.aria-current]="(active ? 'page' : null)"
				(click)="navigate($event)">
				<div *ngIf="!isSubMenu" class="bx--side-nav__icon">
					<ng-content select="[icon]"></ng-content>
				</div>
				<span class="bx--side-nav__link-text">
					<ng-content></ng-content>
				</span>
			</a>
		</li>
	`
})
export class SideNavItem implements OnChanges {
	/**
	 * Link for the item. NOTE: *do not* pass unsafe or untrusted values, this has the potential to open you up to XSS attacks
	 */
	@Input() set href(v: string) {
		// Needed when component is created dynamically with a model.
		if (v === undefined) {
			return;
		}
		this._href = v;
	}

	get href() {
		return this.domSanitizer.bypassSecurityTrustUrl(this._href) as string;
	}

	/**
	 * Toggles the active (current page) state for the link.
	 */
	@Input() active = false;

	/**
	 * Array of commands to send to the router when the link is activated
	 * See: https://angular.io/api/router/Router#navigate
	 */
	@Input() route: any[];

	@Input() isSubMenu = false;

	/**
	 * Router options. Used in conjunction with `route`
	 * See: https://angular.io/api/router/Router#navigate
	 */
	@Input() routeExtras: any;

	/**
	 * Emits the navigation status promise when the link is activated
	 */
	@Output() navigation = new EventEmitter<Promise<boolean>>();

	/**
	 * Emits when `active` input is changed. This is mainly used to indicate to any parent menu items that a
	 * child sidenav item is active or not active.
	 */
	@Output() selected = new EventEmitter<boolean>();

	protected _href = "javascript:void(0)";

	constructor(protected domSanitizer: DomSanitizer, @Optional() protected router: Router) {}

	ngOnChanges(changes) {
		if (changes.active) {
			this.selected.emit(this.active);
		}
	}

	navigate(event) {
		if (this.router && this.route) {
			event.preventDefault();
			const status = this.router.navigate(this.route, this.routeExtras);
			this.navigation.emit(status);
		}
	}
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""