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

+6-7
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

+1-2
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

+4-3
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)