@engie-group/fluid-design-system-angular
Version:
Fluid Design System Angular
185 lines (158 loc) • 3.93 kB
text/typescript
import {CommonModule} from '@angular/common';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
HostBinding,
HostListener,
inject,
Input,
Output,
ViewEncapsulation,
} from '@angular/core';
import {CheckboxComponent} from '../checkbox/checkbox.component';
import {IconComponent} from '../icon/icon.component';
import {TListItemType} from './list-item.model';
export class ListItemComponent {
private readonly cdr = inject(ChangeDetectorRef);
/**
* @ignore
*/
get className() {
return 'nj-list-deprecated__item';
}
/**
* Whether item is active or not
*/
isActive: boolean;
/**
* Whether item is disabled or not
*/
isDisabled: boolean;
/**
* @ignore
*/
get isClickable() {
const hasRoleOption = this.role === 'option';
return this.hasClickableChildren || hasRoleOption;
}
/**
* Whether the border should be on the right
*/
hasRightBorder = false;
/** Override aria role if required. */
role?: string;
/**
* @ignore
*/
get getTabindex() {
return this.role === 'option' ? -1 : null;
}
ariaSelected: boolean = null;
/**
* Name of the icon
*/
iconName: string;
/**
* Aria label of the icon
*/
iconAriaLabel: string;
/**
* List item type <br>
* `list` -> `<li>...</li>` <br>
* `link` -> `<li><a>...</a></li>` <br>
* `button` -> `<li><button>...</button></li>`
*
* @default "list"
*/
type?: TListItemType;
/**
* List item href when `type` is `link`
*/
href?: string;
/**
* List item attribute value
*/
value: string;
/**
* Whether content should be in a checkbox
*/
isCheckboxContent: boolean = false;
/**
* Whether only icon should be displayed
*/
isIconOnly: boolean = false;
/**
* Checkbox id if `isCheckboxContent` is set to `true`
*/
checkboxContentId: string;
/**
* Emits an event on item click
*/
itemClick = new EventEmitter<MouseEvent>();
/**
* @ignore
*/
onMouseClick(event: MouseEvent) {
// Ignore the event if the list item is a button or a link, the click event is bound to
// the wrapped <button> or <a>
if (!this.hasClickableChildren) {
this.itemClick.emit(event);
}
}
constructor(public readonly el: ElementRef<HTMLElement>) {
}
/**
* Get value of item, returns value if set else returns textContent of item.
* Used in `<nj-select>` component
*/
getValue(): string {
return this.value ?? this.getLabel();
}
/**
* Get label of item, returns textContent of item.
* Used in `<nj-select>` component
*/
getLabel(): string {
return this.el?.nativeElement?.textContent;
}
/**
* @ignore
*/
updateSelected(isSelected: boolean) {
this.ariaSelected = isSelected;
this.isActive = isSelected;
this.cdr.markForCheck();
}
/**
* @ignore
*/
get hasClickableChildren() {
return this.type === 'button' || this.type === 'link';
}
}