Skip to content

Commit c648c30

Browse files
committed
refactor(rounded): signal inputs, host bindings, cleanup, tests
1 parent fdef420 commit c648c30

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
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 { RoundedDirective } from './rounded.directive';
23

34
describe('RoundedDirective', () => {
45
it('should create an instance', () => {
6+
TestBed.runInInjectionContext(() => {
57
const directive = new RoundedDirective();
68
expect(directive).toBeTruthy();
9+
});
710
});
811
});

projects/coreui-angular/src/lib/utilities/rounded.directive.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
import { Directive, HostBinding, Input } from '@angular/core';
1+
import { computed, Directive, input } from '@angular/core';
22
import { Rounded } from './rounded.type';
33

44
@Directive({
5-
selector: '[cRounded]'
5+
selector: '[cRounded]',
6+
exportAs: 'cRounded',
7+
host: { '[class]': 'hostClasses' }
68
})
79
export class RoundedDirective {
810
/**
911
* Set border radius variant and radius size
1012
* @type Rounded
1113
*/
12-
@Input('cRounded') rounded: Rounded = true;
14+
readonly cRounded = input<Rounded>(true);
1315

14-
@HostBinding('class')
15-
get hostClasses(): any {
16-
if (typeof this.rounded === 'boolean') {
16+
readonly hostClasses = computed( () => {
17+
const rounded = this.cRounded();
18+
if (typeof rounded === 'boolean') {
1719
return { rounded: true };
1820
}
19-
if (typeof this.rounded === 'number' || typeof this.rounded === 'string') {
21+
if (typeof rounded === 'number' || typeof rounded === 'string') {
2022
return {
21-
[`rounded-${this.rounded}`]: true
23+
[`rounded-${rounded}`]: true
2224
};
2325
}
24-
if (typeof this.rounded === 'object') {
26+
if (typeof rounded === 'object') {
2527
const roundedObj = {
2628
top: undefined,
2729
end: undefined,
@@ -30,7 +32,7 @@ export class RoundedDirective {
3032
circle: undefined,
3133
pill: undefined,
3234
size: undefined,
33-
...this.rounded
35+
...rounded
3436
};
3537
// @ts-ignore
3638
const keys = Object.keys(roundedObj).filter((key) => roundedObj[key] !== undefined);
@@ -49,5 +51,6 @@ export class RoundedDirective {
4951
// console.log('rounded keys', keys, classes);
5052
return Object.entries(classes).length === 0 ? { rounded: false } : classes;
5153
}
52-
}
54+
return { rounded: false };
55+
});
5356
}

0 commit comments

Comments
 (0)