|
1 |
| -import { booleanAttribute, Directive, ElementRef, HostBinding, Input } from '@angular/core'; |
| 1 | +import { |
| 2 | + booleanAttribute, |
| 3 | + computed, |
| 4 | + Directive, |
| 5 | + ElementRef, |
| 6 | + inject, |
| 7 | + input, |
| 8 | + InputSignal, |
| 9 | + InputSignalWithTransform |
| 10 | +} from '@angular/core'; |
2 | 11 | import { Colors } from '../coreui.types';
|
3 | 12 |
|
4 | 13 | @Directive({
|
5 | 14 | selector: '[cListGroupItem], c-list-group-item',
|
6 | 15 | exportAs: 'cListGroupItem',
|
7 |
| - standalone: true |
| 16 | + standalone: true, |
| 17 | + host: { |
| 18 | + '[class]': 'hostClasses()', |
| 19 | + '[attr.aria-disabled]': 'ariaDisabled()', |
| 20 | + '[attr.aria-current]': 'ariaCurrent()', |
| 21 | + '[attr.disabled]': 'attrDisabled()', |
| 22 | + '[attr.tabindex]': 'tabIndex()' |
| 23 | + } |
8 | 24 | })
|
9 | 25 | export class ListGroupItemDirective {
|
10 |
| - |
11 |
| - constructor( |
12 |
| - private hostElement: ElementRef |
13 |
| - ) { } |
| 26 | + readonly hostElement = inject(ElementRef); |
14 | 27 |
|
15 | 28 | /**
|
16 | 29 | * Toggle the active state for the component.
|
17 |
| - * @type boolean |
| 30 | + * @type InputSignal<boolean | undefined> |
18 | 31 | */
|
19 |
| - @Input() active?: boolean; |
| 32 | + readonly active: InputSignal<boolean | undefined> = input(); |
20 | 33 |
|
21 | 34 | /**
|
22 | 35 | * Sets the color context of the component to one of CoreUI’s themed colors.
|
23 |
| - * @type Colors |
| 36 | + * @type InputSignal<boolean | undefined> |
24 | 37 | */
|
25 |
| - @Input() color?: Colors; |
| 38 | + readonly color: InputSignal<Colors | undefined> = input(); |
26 | 39 |
|
27 | 40 | /**
|
28 | 41 | * Set disabled attr for the host element. [docs]
|
29 | 42 | * @type boolean
|
30 | 43 | */
|
31 |
| - @Input({ transform: booleanAttribute }) disabled: string | boolean = false; |
32 |
| - |
33 |
| - @HostBinding('attr.aria-disabled') |
34 |
| - get isDisabled(): boolean | null { |
35 |
| - return <boolean>this.disabled || null; |
36 |
| - } |
37 |
| - |
38 |
| - @HostBinding('attr.disabled') |
39 |
| - get attrDisabled() { |
40 |
| - return this.disabled ? '' : null; |
41 |
| - }; |
42 |
| - |
43 |
| - @HostBinding('attr.tabindex') |
44 |
| - get getTabindex(): string | null { |
45 |
| - return this.disabled ? '-1' : null; |
46 |
| - } |
| 44 | + readonly disabled: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute }); |
47 | 45 |
|
48 |
| - @HostBinding('attr.aria-current') get ariaCurrent(): boolean { |
49 |
| - return !!this.active; |
50 |
| - } |
51 |
| - |
52 |
| - @HostBinding('class') |
53 |
| - get hostClasses(): any { |
| 46 | + readonly hostClasses = computed(() => { |
54 | 47 | const host: HTMLElement = this.hostElement.nativeElement;
|
55 | 48 | return {
|
56 | 49 | 'list-group-item': true,
|
57 | 50 | 'list-group-item-action': host.nodeName === 'A' || host.nodeName === 'BUTTON',
|
58 |
| - active: !!this.active, |
59 |
| - disabled: this.isDisabled, |
60 |
| - [`list-group-item-${this.color}`]: !!this.color |
| 51 | + active: !!this.active(), |
| 52 | + disabled: this._disabled(), |
| 53 | + [`list-group-item-${this.color()}`]: !!this.color() |
61 | 54 | };
|
62 |
| - } |
| 55 | + }); |
| 56 | + |
| 57 | + readonly _disabled = computed(() => this.disabled()); |
| 58 | + |
| 59 | + readonly ariaDisabled = computed(() => { |
| 60 | + return this._disabled() ? true : null; |
| 61 | + }); |
| 62 | + |
| 63 | + readonly attrDisabled = computed(() => { |
| 64 | + return this._disabled() ? '' : null; |
| 65 | + }); |
| 66 | + |
| 67 | + readonly tabIndex = computed(() => { |
| 68 | + return this._disabled() ? '-1' : null; |
| 69 | + }); |
63 | 70 |
|
| 71 | + readonly ariaCurrent = computed(() => { |
| 72 | + return <boolean>this.active() || null; |
| 73 | + }); |
64 | 74 | }
|
0 commit comments