Skip to content

Commit b230db6

Browse files
committedAug 27, 2024
refactor(placeholder): input signals, host bindings
1 parent 8b336fe commit b230db6

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { TestBed } from '@angular/core/testing';
12
import { PlaceholderAnimationDirective } from './placeholder-animation.directive';
23

34
describe('PlaceholderAnimationDirective', () => {
45
it('should create an instance', () => {
5-
const directive = new PlaceholderAnimationDirective();
6-
expect(directive).toBeTruthy();
6+
TestBed.runInInjectionContext(() => {
7+
const directive = new PlaceholderAnimationDirective();
8+
expect(directive).toBeTruthy();
9+
});
710
});
811
});
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
import { AfterContentInit, ContentChild, Directive, HostBinding, Input } from '@angular/core';
1+
import { computed, contentChild, Directive, input, InputSignal } from '@angular/core';
22
import { PlaceholderDirective } from './placeholder.directive';
33

44
@Directive({
55
selector: '[cPlaceholderAnimation]',
6-
standalone: true
6+
standalone: true,
7+
host: {
8+
'[class]': 'hostClasses()'
9+
}
710
})
8-
export class PlaceholderAnimationDirective implements AfterContentInit {
9-
11+
export class PlaceholderAnimationDirective {
1012
/**
1113
* Animation type for placeholder
1214
* @type 'glow' | 'wave'
1315
* @default undefined
1416
*/
15-
@Input('cPlaceholderAnimation') animation?: 'glow' | 'wave';
16-
17-
@ContentChild(PlaceholderDirective) placeholder!: PlaceholderDirective;
17+
readonly animation: InputSignal<'glow' | 'wave' | undefined> = input<'glow' | 'wave' | undefined>(undefined, {
18+
alias: 'cPlaceholderAnimation'
19+
});
1820

19-
#animate: boolean = false;
21+
readonly placeholder = contentChild(PlaceholderDirective);
2022

21-
@HostBinding('class')
22-
get hostClasses(): any {
23+
readonly hostClasses = computed(() => {
2324
return {
24-
[`placeholder-${this.animation}`]: this.#animate && !!this.animation
25+
[`placeholder-${this.animation()}`]: this.placeholder()?.visible() && !!this.animation()
2526
};
26-
}
27-
28-
ngAfterContentInit() {
29-
this.#animate = this.placeholder?.visible;
30-
}
27+
});
3128
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { TestBed } from '@angular/core/testing';
12
import { PlaceholderDirective } from './placeholder.directive';
23

34
describe('PlaceholderDirective', () => {
45
it('should create an instance', () => {
5-
const directive = new PlaceholderDirective();
6-
expect(directive).toBeTruthy();
6+
TestBed.runInInjectionContext(() => {
7+
const directive = new PlaceholderDirective();
8+
expect(directive).toBeTruthy();
9+
});
710
});
811
});
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
1-
import { booleanAttribute, Directive, HostBinding, Input } from '@angular/core';
1+
import { booleanAttribute, computed, Directive, input, InputSignalWithTransform } from '@angular/core';
22

33
@Directive({
44
selector: '[cPlaceholder]',
55
exportAs: 'cPlaceholder',
6-
standalone: true
6+
standalone: true,
7+
host: {
8+
'[class]': 'hostClasses()',
9+
'[attr.aria-hidden]': 'ariaHidden()'
10+
}
711
})
812
export class PlaceholderDirective {
9-
1013
/**
1114
* placeholder toggler
1215
* @type boolean
1316
* @default false
1417
*/
15-
@Input({ alias: 'cPlaceholder', transform: booleanAttribute }) visible: boolean = false;
18+
readonly visible: InputSignalWithTransform<boolean, unknown> = input<boolean, unknown>(false, {
19+
transform: booleanAttribute,
20+
alias: 'cPlaceholder'
21+
});
1622

1723
/**
1824
* Size the placeholder xs, small, large.
1925
*/
20-
@Input('cPlaceholderSize') size?: 'xs' | 'sm' | 'lg';
26+
readonly size = input<'xs' | 'sm' | 'lg' | undefined>(undefined, { alias: 'cPlaceholderSize' });
2127

22-
@HostBinding('attr.aria-hidden')
23-
get ariaHidden(): boolean | null {
24-
return this.visible ? null : true;
25-
};
28+
readonly ariaHidden = computed(() => {
29+
return this.visible() ? null : true;
30+
});
2631

27-
@HostBinding('class')
28-
get hostClasses(): any {
32+
readonly hostClasses = computed(() => {
2933
return {
30-
'placeholder': this.visible,
31-
[`placeholder-${this.size}`]: !!this.size
34+
placeholder: this.visible(),
35+
[`placeholder-${this.size()}`]: !!this.size()
3236
};
33-
}
37+
});
3438
}

0 commit comments

Comments
 (0)
Please sign in to comment.