Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c7f692a

Browse files
committedJan 3, 2025
refactor(form-control): signal inputs, host bindings, cleanup
1 parent 791db87 commit c7f692a

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed
 

‎projects/coreui-angular/src/lib/form/form-control/form-control.directive.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,57 @@
1-
import { booleanAttribute, Directive, ElementRef, HostBinding, inject, Input, OnInit } from '@angular/core';
1+
import { booleanAttribute, computed, Directive, ElementRef, inject, input, OnInit } from '@angular/core';
22

33
import { InputType } from '../../coreui.types';
44

55
@Directive({
6-
selector: 'input[cFormControl], textarea[cFormControl]'
6+
selector: 'input[cFormControl], textarea[cFormControl]',
7+
host: {
8+
'[class]': 'hostClasses()',
9+
'[attr.type]': 'type()'
10+
}
711
})
812
export class FormControlDirective implements OnInit {
913
readonly #hostElement = inject(ElementRef);
1014

1115
/**
1216
* Size the component small or large.
13-
* @type {'sm' | 'lg'}
17+
* @default undefined
1418
*/
15-
@Input() sizing?: '' | 'sm' | 'lg' | string = '';
19+
readonly sizing = input<'' | 'sm' | 'lg' | string>();
20+
1621
/**
1722
* Set component validation state to valid.
18-
* @type boolean | undefined
23+
* @default undefined
1924
*/
20-
@Input() valid?: boolean;
25+
readonly valid = input<boolean>();
2126

2227
/**
2328
* Specifies the type of input element.
2429
*/
25-
@HostBinding('attr.type')
26-
@Input()
27-
type: Omit<InputType, 'checkbox' | 'radio'> = 'text';
30+
readonly type = input<Omit<InputType, 'checkbox' | 'radio'>>('text');
2831

2932
/**
30-
* Render the component styled as plain text. Removes the default form field styling and preserve the correct margin and padding. Recommend to use alongside `readonly` [docs]
33+
* Render the component styled as plain text. Removes the default form field styling and preserve the correct margin and padding. Recommend to use alongside `readonly`
34+
* @default false
3135
*/
32-
@Input({ transform: booleanAttribute }) plaintext: string | boolean = false;
36+
readonly plaintext = input(false, { transform: booleanAttribute });
3337

34-
@HostBinding('class')
35-
get hostClasses(): any {
36-
const isRangeType = this.type === 'range';
38+
readonly hostClasses = computed(() => {
39+
const type = this.type();
40+
const isRange = type === 'range';
41+
const plaintext = this.plaintext();
42+
const sizing = this.sizing();
43+
const valid = this.valid();
3744

3845
return {
39-
'form-control': !isRangeType && !this.plaintext,
40-
'form-control-plaintext': !isRangeType && this.plaintext,
41-
'form-control-color': this.type === 'color',
42-
'form-range': isRangeType,
43-
[`form-control-${this.sizing}`]: !!this.sizing && !isRangeType,
44-
'is-valid': this.valid === true,
45-
'is-invalid': this.valid === false
46-
};
47-
}
46+
'form-control': !isRange && !plaintext,
47+
'form-control-plaintext': !isRange && plaintext,
48+
'form-control-color': type === 'color',
49+
'form-range': isRange,
50+
[`form-control-${sizing}`]: !!sizing && !isRange,
51+
'is-valid': valid === true,
52+
'is-invalid': valid === false
53+
} as Record<string, boolean>;
54+
});
4855

4956
get hostTag(): string {
5057
return this.#hostElement.nativeElement.tagName;

0 commit comments

Comments
 (0)
Please sign in to comment.