@engie-group/fluid-design-system-angular
Version:
Fluid Design System Angular
122 lines (103 loc) • 3.15 kB
text/typescript
import {NgIf, NgIfContext, NgTemplateOutlet} from '@angular/common';
import {
AfterContentInit,
booleanAttribute,
Component,
ContentChildren,
ElementRef,
inject,
Input,
QueryList,
TemplateRef,
ViewEncapsulation
} from '@angular/core';
import {EngieTemplateDirective} from '../../directives/engie-template.directive';
import {TSizeVariants} from '../../models/size-variant.model';
import {AccordionComponent} from '../accordion/accordion.component';
import {IconComponent} from '../icon/icon.component';
export class AccordionItemComponent implements AfterContentInit {
private element: ElementRef<HTMLDetailsElement> = inject(ElementRef);
private accordion = inject(AccordionComponent);
protected iconTemplate?: TemplateRef<unknown>;
protected headerTemplate?: TemplateRef<NgIfContext<string>>;
/**
* Size of the accordion item
*/
scale?: Extract<TSizeVariants, 'md' | 'lg'>;
/**
* Whether the toggle icon is place at the start of the item or not.
*/
hasLeadingToggleIcon?: boolean;
/**
* Whether the toggle use the alternative icons (plus/minus) or the default ones (arrow).
*/
useAlternativeToggleIcon?: boolean;
/**
* This attribute enables multiple <details> elements to be connected, with only one open at a time.
*/
name?: string;
/**
* Item header
*/
label?: string;
protected templateDirectives?: QueryList<EngieTemplateDirective>;
protected get classes() {
const classes = ['nj-accordion-item'];
if (this.scale && this.scale !== 'md') {
classes.push(`nj-accordion-item--${this.scale}`);
}
if (this.hasLeadingToggleIcon) {
classes.push('nj-accordion-item--leading-toggle');
}
return classes;
}
ngAfterContentInit() {
this.templateDirectives.forEach((templateDirective) => {
if (templateDirective?.selector === 'icon') {
this.iconTemplate = templateDirective.templateRef;
}
if (templateDirective?.selector === 'header') {
this.headerTemplate = templateDirective.templateRef as TemplateRef<NgIfContext<string>>;
}
});
}
protected handleClick(event: MouseEvent) {
if (this.name && !this.element.nativeElement.open) {
event.preventDefault();
this.accordion.collapseAllItems(this.name);
this.expand();
}
}
/**
* Expand the accordion item programmatically
*/
expand() {
if (this.element) {
this.element.nativeElement.open = true;
}
}
/**
* Collapse the accordion item programmatically
*/
collapse() {
if (this.element) {
this.element.nativeElement.open = false;
}
}
}