Skip to content

Commit abdce49

Browse files
committed
refactor(bg-color): signal inputs, host bindings, cleanup, tests
1 parent 4337554 commit abdce49

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { TestBed } from '@angular/core/testing';
12
import { BgColorDirective } from './bg-color.directive';
23

34
describe('BgColorDirective', () => {
45
it('should create an instance', () => {
6+
TestBed.runInInjectionContext(() => {
57
const directive = new BgColorDirective();
68
expect(directive).toBeTruthy();
9+
});
710
});
811
});
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
import { Directive, HostBinding, Input } from '@angular/core';
1+
import { computed, Directive, input } from '@angular/core';
22
import { BackgroundColors } from '../coreui.types';
33

44
@Directive({
5-
selector: '[cBgColor]'
5+
selector: '[cBgColor]',
6+
exportAs: 'cBgColor',
7+
host: { '[class]': 'hostClasses' }
68
})
79
export class BgColorDirective {
810
/**
911
* Set the background of an element to any contextual class
1012
*/
11-
@Input('cBgColor') color: BackgroundColors = '';
13+
readonly cBgColor = input<BackgroundColors>('');
14+
1215
/**
1316
* Add linear gradient as background image to the backgrounds.
14-
* @type boolean
17+
* @return boolean
1518
*/
16-
@Input() gradient?: boolean;
19+
readonly gradient = input<boolean>();
1720

18-
@HostBinding('class')
19-
get hostClasses(): any {
21+
readonly hostClasses = computed(() => {
22+
const color = this.cBgColor();
2023
return {
21-
[`bg-${this.color}`]: !!this.color,
22-
'bg-gradient': this.gradient
24+
[`bg-${color}`]: !!color,
25+
'bg-gradient': this.gradient()
2326
};
24-
}
27+
});
2528
}

0 commit comments

Comments
 (0)