ngx-eagle
Version:
UI component infrastructure and Design components for mobile and desktop Angular web applications.
113 lines (104 loc) • 3.03 kB
text/typescript
import { NgIf, NgStyle } from '@angular/common';
import {
Component,
ElementRef,
EventEmitter,
Input,
OnInit,
Output,
Renderer2,
booleanAttribute,
} from '@angular/core';
import { NgxMode } from './typings';
export class TagComponent implements OnInit {
ngxBordered: boolean = true;
ngxChecked: boolean = false;
ngxMode: NgxMode = 'default';
readonly ngxOnClose = new EventEmitter<MouseEvent>();
readonly ngxCheckedChange = new EventEmitter<boolean>();
backgroundColor: string = '#1890FF';
color: string = '#ffffff';
constructor(
private renderer: Renderer2,
private elementRef: ElementRef,
) {
this.backgroundColor = this.elementRef.nativeElement.style.backgroundColor;
}
ngOnInit(): void {
this.setTagColor();
}
updateCheckedStatus(): void {
if (this.ngxMode === 'checkable') {
this.ngxChecked = !this.ngxChecked;
this.setTagColor();
this.ngxCheckedChange.emit(this.ngxChecked);
}
}
setTagColor() {
let bgColor = '';
let color = '';
let borderColor = '';
switch (this.ngxMode) {
case 'default':
bgColor = this.backgroundColor;
color = this.color;
break;
case 'checkable':
bgColor = this.ngxChecked ? this.backgroundColor : 'transparent';
color = this.ngxChecked ? this.color : 'currentColor';
break;
case 'closeable':
bgColor = this.backgroundColor;
color = this.color;
break;
}
borderColor = this.color === '#ffffff' ? 'currentColor' : this.color;
this.renderer.setStyle(
this.elementRef.nativeElement,
'background-color',
bgColor
);
this.renderer.setStyle(this.elementRef.nativeElement, 'color', color);
this.renderer.setStyle(
this.elementRef.nativeElement,
'border-color',
this.ngxBordered ? borderColor : 'transparent'
);
}
closeTag(e: MouseEvent): void {
this.ngxOnClose.emit(e);
if (!e.defaultPrevented) {
this.renderer.removeChild(
this.renderer.parentNode(this.elementRef.nativeElement),
this.elementRef.nativeElement
);
}
}
}