Skip to content

Commit b078260

Browse files
committedAug 30, 2024
refactor(img): input signals, host bindings
1 parent 3fb4c9a commit b078260

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed
 
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { TestBed } from '@angular/core/testing';
12
import { ImgDirective } from './img.directive';
23

34
describe('ImgDirective', () => {
45
it('should create an instance', () => {
5-
const directive = new ImgDirective();
6-
expect(directive).toBeTruthy();
6+
TestBed.runInInjectionContext(() => {
7+
const directive = new ImgDirective();
8+
expect(directive).toBeTruthy();
9+
});
710
});
811
});
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
1-
import { booleanAttribute, Directive, HostBinding, Input } from '@angular/core';
1+
import { booleanAttribute, computed, Directive, input, InputSignal, InputSignalWithTransform } from '@angular/core';
22

33
@Directive({
44
selector: '[cImg]',
5-
standalone: true
5+
standalone: true,
6+
host: {
7+
'[class]': 'hostClasses()',
8+
'[style]': 'hostStyles()'
9+
}
610
})
711
export class ImgDirective {
8-
912
/**
1013
* Set the horizontal aligment.
1114
* @type {'' | 'start' | 'end' | 'center'}
1215
*/
13-
@Input() align: '' | 'start' | 'end' | 'center' = '';
16+
readonly align: InputSignal<'' | 'start' | 'end' | 'center'> = input<'' | 'start' | 'end' | 'center'>('');
1417

1518
/**
1619
* Make image responsive.
1720
* @type boolean
1821
*/
19-
@Input({ transform: booleanAttribute }) fluid: string | boolean = false;
22+
readonly fluid: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute });
2023

2124
/**
2225
* Make image rounded.
2326
* @type boolean
2427
*/
25-
@Input({ transform: booleanAttribute }) rounded: string | boolean = false;
28+
readonly rounded: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute });
2629

2730
/**
2831
* Give an image a rounded 1px border appearance.
2932
* @type boolean
3033
*/
31-
@Input({ transform: booleanAttribute }) thumbnail: string | boolean = false;
34+
readonly thumbnail: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute });
3235

3336
/**
3437
* Color for image placeholder.
3538
*/
36-
@Input() placeholderColor = 'transparent';
39+
readonly placeholderColor = input('transparent');
3740

38-
@HostBinding('style')
39-
get getStyles(): any {
40-
return { backgroundColor: this.placeholderColor };
41-
}
41+
readonly hostStyles = computed(() => {
42+
return { backgroundColor: this.placeholderColor() };
43+
});
4244

43-
@HostBinding('class')
44-
get hostClasses(): any {
45-
const align = this.align;
45+
readonly hostClasses = computed(() => {
46+
const align = this.align();
4647
return {
4748
[`float-${align}`]: align === 'start' || align === 'end',
4849
'd-block': align === 'center',
4950
'mx-auto': align === 'center',
50-
'img-fluid': this.fluid,
51-
'rounded': this.rounded,
52-
'img-thumbnail': this.thumbnail
53-
};
54-
}
51+
'img-fluid': this.fluid(),
52+
rounded: this.rounded(),
53+
'img-thumbnail': this.thumbnail()
54+
} as Record<string, boolean>;
55+
});
5556
}

0 commit comments

Comments
 (0)
Please sign in to comment.