@engie-group/fluid-design-system-angular
Version:
Fluid Design System Angular
142 lines (121 loc) • 2.8 kB
text/typescript
import { CommonModule } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
ElementRef,
EventEmitter,
Input,
Output,
ViewEncapsulation
} from '@angular/core';
import { ThemeComponentsVariants } from '../../models/theme-variant.model';
import { IconButtonComponent } from '../icon-button/icon-button.component';
import { TagSize } from './tag.model';
export class TagComponent {
private readonly tagClassName = 'nj-tag';
/**
* Tag variant
*
* @default `grey`
*/
variant?: ThemeComponentsVariants;
/**
* Tag size
*/
size?: TagSize = 'md';
/**
* Tag iconName
*/
iconName?: string;
/**
* Tag href. If set, tag renders a link
*/
href?: string;
/**
* target of link tag
*/
target?: string;
/**
* If set, tag renders a button
*/
isClickable?: boolean;
/**
* Whether tag can be closed
*/
isClosable?: boolean;
/**
* Whether tag should remove himself when close is clicked
*/
shouldAutoDestruct = true;
/**
* Label for the close button, if present
* @example "Remove [tag name]"
*/
closeLabel?: string;
/**
* Whether tag is disabled
*/
isDisabled?: boolean;
/**
* Whether tag has custom icon
*/
hasCustomIcon?: boolean;
/**
* Output event when clickable tag is clicked
*/
tagClick = new EventEmitter<MouseEvent>();
/**
* Output event when tag is closed. Focus must be set to either previous tag, next tag or any relevant element.
*/
closeClick = new EventEmitter<MouseEvent>();
constructor(private el: ElementRef) {}
/**
* @ignore
*/
getTagVariantClass(): string {
if (!this.variant) {
return '';
}
return `${this.tagClassName}--${this.variant}`;
}
/**
* @ignore
*/
getTagSizeClass(): string {
if (!this.size || this.size === 'md') {
return '';
}
return `${this.tagClassName}--${this.size}`;
}
/**
* @ignore
*/
getTagDisabledClass(): string {
if (!this.isDisabled) {
return '';
}
return `${this.tagClassName}--disabled`;
}
/**
* @ignore
*/
removeTag(event: MouseEvent) {
event?.preventDefault();
event?.stopImmediatePropagation();
if (this.shouldAutoDestruct) {
this.el?.nativeElement?.remove();
}
this.closeClick.emit(event);
}
focusIconButton() {
this.el?.nativeElement?.querySelector('nj-icon-button button')?.focus();
}
}