Skip to content

Commit 232112a

Browse files
committed
refactor(form-label): signal inputs, host bindings, cleanup
1 parent 022046a commit 232112a

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { FormLabelDirective } from './form-label.directive';
2+
import { TestBed } from '@angular/core/testing';
23

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

33
@Directive({
44
selector: '[cLabel]',
5-
host: { class: 'form-label' }
5+
host: { class: 'form-label', '[class]': 'hostClasses()' }
66
})
77
export class FormLabelDirective {
88
/**
99
* For horizontal forms set labels to 'col' and make them vertically centered with their associated form controls.
10-
* @type 'col'
10+
* @default ''
1111
*/
12-
@Input('cLabel') col: 'col' | '' = '';
12+
readonly col = input<'col' | ''>('', { alias: 'cLabel' });
1313
/**
1414
* Size the label small or large.
15+
* @default ''
1516
*/
16-
@Input() sizing: '' | 'sm' | 'lg' | string = '';
17+
readonly sizing = input<'' | 'sm' | 'lg' | string>();
1718

18-
@HostBinding('class')
19-
get hostClasses(): any {
19+
readonly hostClasses = computed(() => {
20+
const col = this.col();
21+
const sizing = this.sizing();
2022
return {
2123
'form-label': true,
22-
'col-form-label': this.col === 'col',
23-
[`col-form-label-${this.sizing}`]: !!this.sizing && this.col === 'col'
24-
};
25-
}
24+
'col-form-label': col === 'col',
25+
[`col-form-label-${sizing}`]: !!sizing && col === 'col'
26+
} as Record<string, boolean>;
27+
});
2628
}

0 commit comments

Comments
 (0)