Skip to content

Commit 6034a01

Browse files
committed
refactor(html-attr): cleanup
1 parent 9c2725b commit 6034a01

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

projects/coreui-angular/src/lib/shared/html-attr.directive.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Directive, ElementRef, Input, OnInit, Renderer2} from '@angular/core';
1+
import { Directive, ElementRef, Input, OnInit, Renderer2 } from '@angular/core';
22

33
@Directive({
44
selector: '[cHtmlAttr]',
@@ -7,8 +7,7 @@ import {Directive, ElementRef, Input, OnInit, Renderer2} from '@angular/core';
77
})
88
export class HtmlAttributesDirective implements OnInit {
99

10-
// @ts-ignore
11-
@Input() cHtmlAttr: {[key: string]: any };
10+
@Input() cHtmlAttr?: { [key: string]: any };
1211

1312
constructor(
1413
private renderer: Renderer2,
@@ -18,7 +17,7 @@ export class HtmlAttributesDirective implements OnInit {
1817
ngOnInit(): void {
1918
const attribs = this.cHtmlAttr;
2019
for (const attr in attribs) {
21-
if (attr === 'style' && typeof(attribs[attr]) === 'object' ) {
20+
if (attr === 'style' && typeof (attribs[attr]) === 'object') {
2221
this.setStyle(attribs[attr]);
2322
} else if (attr === 'class') {
2423
this.addClass(attribs[attr]);
@@ -36,16 +35,16 @@ export class HtmlAttributesDirective implements OnInit {
3635
}
3736
}
3837

39-
private addClass(classes: string): void {
38+
private addClass(classes: string | string[]): void {
4039
const classArray = (Array.isArray(classes) ? classes : classes.split(' '));
4140
classArray.filter((element) => element.length > 0).forEach(element => {
42-
this.renderer.addClass(this.el.nativeElement, element );
41+
this.renderer.addClass(this.el.nativeElement, element);
4342
});
4443
}
4544

4645
private setAttrib(key: string, value: string | null): void {
4746
value !== null ?
48-
this.renderer.setAttribute(this.el.nativeElement, key, value ) :
47+
this.renderer.setAttribute(this.el.nativeElement, key, value) :
4948
this.renderer.removeAttribute(this.el.nativeElement, key);
5049
}
5150
}

projects/coreui-icons-angular/src/lib/shared/html-attr.directive.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import { HtmlAttributesDirective } from './html-attr.directive';
88
template: `
99
<div [cHtmlAttr]="{class: 'test', style: {backgroundColor: 'red'}, id: 'id-1'}"></div>`
1010
})
11-
class TestComponent {
12-
}
11+
class TestComponent {}
1312

1413
describe('HtmlAttributesDirective', () => {
1514

projects/coreui-icons-angular/src/lib/shared/html-attr.directive.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ export class HtmlAttributesDirective implements OnInit {
2727
}
2828
}
2929

30-
private setStyle(styles: any): void {
31-
// tslint:disable-next-line:forin
30+
private setStyle(styles: { [x: string]: any; }): void {
3231
for (const style in styles) {
33-
this.renderer.setStyle(this.el.nativeElement, style, styles[style]);
32+
if (style) {
33+
this.renderer.setStyle(this.el.nativeElement, style, styles[style]);
34+
}
3435
}
3536
}
3637

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy