"git@scm.cms.hu-berlin.de:iqb/verona-modules-aspect.git" did not exist on "5cb4ed760594c7d66d85f547938d5b818a64e55e"
Newer
Older
Directive, ElementRef, Input, OnChanges, SimpleChanges
selector: '[aspectInputBackgroundColor]'
export class InputBackgroundColorDirective implements OnChanges {
@Input() appearance!: string;
constructor(private elementRef: ElementRef) { }
ngOnChanges(changes: SimpleChanges): void {
if (changes.appearance && !changes.appearance.firstChange) {
this.clearFilledBackgroundColor();
}
setTimeout(() => { // wait for Angular to set up the component before manipulating it
this.setBackgroundColor();
});
}
private setBackgroundColor(): void {
if (this.appearance === 'outline') {
targetElements = this.elementRef.nativeElement.querySelector('div.mat-form-field-outline')?.children;
} else {
targetElements = [this.elementRef.nativeElement.querySelector('div.mat-form-field-flex')];
}
// This fails, when component is not set up yet, therefore the extra check
if (targetElements) {
Array.from(targetElements).forEach((element: HTMLElement) => {
element.style.setProperty('background-color', this.backgroundColor);
/* Clear styling of old appearance before switching */
private clearFilledBackgroundColor(): void {
const targetElement: HTMLElement = this.elementRef.nativeElement.querySelector('div.mat-form-field-flex');
targetElement?.style.removeProperty('background-color');
}