|
1 | | -import { booleanAttribute, Directive, ElementRef, HostBinding, inject, Input, OnInit } from '@angular/core'; |
| 1 | +import { booleanAttribute, computed, Directive, ElementRef, inject, input, OnInit } from '@angular/core'; |
2 | 2 |
|
3 | 3 | import { InputType } from '../../coreui.types'; |
4 | 4 |
|
5 | 5 | @Directive({ |
6 | | - selector: 'input[cFormControl], textarea[cFormControl]' |
| 6 | + selector: 'input[cFormControl], textarea[cFormControl]', |
| 7 | + host: { |
| 8 | + '[class]': 'hostClasses()', |
| 9 | + '[attr.type]': 'type()' |
| 10 | + } |
7 | 11 | }) |
8 | 12 | export class FormControlDirective implements OnInit { |
9 | 13 | readonly #hostElement = inject(ElementRef); |
10 | 14 |
|
11 | 15 | /** |
12 | 16 | * Size the component small or large. |
13 | | - * @type {'sm' | 'lg'} |
| 17 | + * @default undefined |
14 | 18 | */ |
15 | | - @Input() sizing?: '' | 'sm' | 'lg' | string = ''; |
| 19 | + readonly sizing = input<'' | 'sm' | 'lg' | string>(); |
| 20 | + |
16 | 21 | /** |
17 | 22 | * Set component validation state to valid. |
18 | | - * @type boolean | undefined |
| 23 | + * @default undefined |
19 | 24 | */ |
20 | | - @Input() valid?: boolean; |
| 25 | + readonly valid = input<boolean>(); |
21 | 26 |
|
22 | 27 | /** |
23 | 28 | * Specifies the type of input element. |
24 | 29 | */ |
25 | | - @HostBinding('attr.type') |
26 | | - @Input() |
27 | | - type: Omit<InputType, 'checkbox' | 'radio'> = 'text'; |
| 30 | + readonly type = input<Omit<InputType, 'checkbox' | 'radio'>>('text'); |
28 | 31 |
|
29 | 32 | /** |
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 |
31 | 35 | */ |
32 | | - @Input({ transform: booleanAttribute }) plaintext: string | boolean = false; |
| 36 | + readonly plaintext = input(false, { transform: booleanAttribute }); |
33 | 37 |
|
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(); |
37 | 44 |
|
38 | 45 | 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 | + }); |
48 | 55 |
|
49 | 56 | get hostTag(): string { |
50 | 57 | return this.#hostElement.nativeElement.tagName; |
|
0 commit comments