Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7340728

Browse files
committedOct 29, 2024
refactor(html-attr.directive): signal input, cleanup
1 parent b8fa8b6 commit 7340728

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed
 
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
1-
import { Component, DebugElement, Renderer2, Type } from '@angular/core';
1+
import { Component, DebugElement, ElementRef, Renderer2 } from '@angular/core';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44

55
import { HtmlAttributesDirective } from './html-attr.directive';
66

77
@Component({
8-
template: `<div [cHtmlAttr]="{class: 'test', style: {backgroundColor: 'red'}, id: 'id-1'}"></div>`
8+
template: `<div [cHtmlAttr]="{ class: 'test', style: { backgroundColor: 'red' }, id: 'id-1' }"></div>`
99
})
1010
class TestComponent {}
1111

12-
describe('HtmlAttributesDirective', () => {
12+
class MockElementRef extends ElementRef {}
1313

14+
describe('HtmlAttributesDirective', () => {
1415
let fixture: ComponentFixture<TestComponent>;
15-
let inputEl: DebugElement;
16-
let renderer: Renderer2;
16+
let debugElement: DebugElement;
1717

1818
beforeEach(() => {
1919
TestBed.configureTestingModule({
2020
declarations: [TestComponent],
21-
imports: [HtmlAttributesDirective]
21+
imports: [HtmlAttributesDirective],
22+
providers: [Renderer2, { provide: ElementRef, useClass: MockElementRef }]
2223
});
2324
fixture = TestBed.createComponent(TestComponent);
24-
inputEl = fixture.debugElement.query(By.css('div'));
25-
renderer = fixture.componentRef.injector.get(Renderer2 as Type<Renderer2>);
25+
debugElement = fixture.debugElement.query(By.css('div'));
2626
});
2727

2828
it('should create an instance', () => {
29-
const directive = new HtmlAttributesDirective(renderer, inputEl);
30-
expect(directive).toBeTruthy();
29+
TestBed.runInInjectionContext(() => {
30+
const directive = new HtmlAttributesDirective();
31+
expect(directive).toBeTruthy();
32+
});
3133
});
3234

3335
it('should render a class attr', () => {
3436
fixture.detectChanges();
35-
expect(inputEl.nativeElement).toHaveClass('test');
37+
expect(debugElement.nativeElement).toHaveClass('test');
3638
});
3739

3840
it('should render a style attr', () => {
3941
fixture.detectChanges();
40-
expect(inputEl.nativeElement.style.backgroundColor).toBe('red');
42+
// console.log(inputEl.nativeElement.style.backgroundColor);
43+
expect(debugElement.nativeElement.style.backgroundColor).toBe('red');
4144
});
4245

4346
it('should render an id attr', () => {
4447
fixture.detectChanges();
45-
expect(inputEl.nativeElement.getAttribute('id')).toBe('id-1');
48+
expect(debugElement.nativeElement.getAttribute('id')).toBe('id-1');
4649
});
4750
});
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import { Directive, ElementRef, Input, OnInit, Renderer2 } from '@angular/core';
1+
import { Directive, effect, ElementRef, inject, input, Renderer2 } from '@angular/core';
22

33
@Directive({
44
selector: '[cHtmlAttr]',
55
exportAs: 'cHtmlAttr',
66
standalone: true
77
})
8-
export class HtmlAttributesDirective implements OnInit {
9-
@Input() cHtmlAttr?: { [key: string]: any };
8+
export class HtmlAttributesDirective {
9+
readonly cHtmlAttr = input<Record<string, any>>();
1010

11-
constructor(
12-
private renderer: Renderer2,
13-
private el: ElementRef
14-
) {}
11+
readonly #renderer = inject(Renderer2);
12+
readonly #elementRef = inject(ElementRef);
1513

16-
ngOnInit(): void {
17-
const attribs = this.cHtmlAttr;
14+
readonly attrEffect = effect(() => {
15+
const attribs = this.cHtmlAttr();
1816
for (const attr in attribs) {
1917
if (attr === 'style' && typeof attribs[attr] === 'object') {
2018
this.setStyle(attribs[attr]);
@@ -24,12 +22,12 @@ export class HtmlAttributesDirective implements OnInit {
2422
this.setAttrib(attr, attribs[attr]);
2523
}
2624
}
27-
}
25+
});
2826

29-
private setStyle(styles: { [x: string]: any }): void {
27+
private setStyle(styles: Record<string, any>): void {
3028
for (const style in styles) {
3129
if (style) {
32-
this.renderer.setStyle(this.el.nativeElement, style, styles[style]);
30+
this.#renderer.setStyle(this.#elementRef.nativeElement, style, styles[style]);
3331
}
3432
}
3533
}
@@ -39,13 +37,13 @@ export class HtmlAttributesDirective implements OnInit {
3937
classArray
4038
.filter((element) => element.length > 0)
4139
.forEach((element) => {
42-
this.renderer.addClass(this.el.nativeElement, element);
40+
this.#renderer.addClass(this.#elementRef.nativeElement, element);
4341
});
4442
}
4543

4644
private setAttrib(key: string, value: string | null): void {
4745
value !== null
48-
? this.renderer.setAttribute(this.el.nativeElement, key, value)
49-
: this.renderer.removeAttribute(this.el.nativeElement, key);
46+
? this.#renderer.setAttribute(this.#elementRef.nativeElement, key, value)
47+
: this.#renderer.removeAttribute(this.#elementRef.nativeElement, key);
5048
}
5149
}

0 commit comments

Comments
 (0)
Please sign in to comment.