Skip to content

Commit b997033

Browse files
committed
refactor(list-group): input signals, host bindings
1 parent b078260 commit b997033

File tree

5 files changed

+74
-60
lines changed

5 files changed

+74
-60
lines changed

projects/coreui-angular/src/lib/button-group/button-group/button-group.component.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ export class ButtonGroupComponent {
1111
* Size the component small or large.
1212
* @type { 'sm' | 'lg' }
1313
*/
14-
size: InputSignal<'sm' | 'lg' | undefined> = input();
14+
readonly size: InputSignal<'sm' | 'lg' | undefined> = input();
1515

1616
/**
1717
* Create a set of buttons that appear vertically stacked rather than horizontally. Split button dropdowns are not supported here.
1818
* @type boolean
1919
*/
20-
vertical: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute });
20+
readonly vertical: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute });
2121

2222
/**
2323
* Default role attr for ButtonGroup. [docs]
2424
* @type InputSignal<string>
2525
* @default 'group'
2626
*/
27-
role: InputSignal<string> = input('group');
27+
readonly role: InputSignal<string> = input('group');
2828

29-
hostClasses = computed(() => {
29+
readonly hostClasses = computed(() => {
3030
return {
3131
'btn-group': !this.vertical(),
3232
'btn-group-vertical': this.vertical(),
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Component, DebugElement, ElementRef } from '@angular/core';
1+
import { Component, ElementRef } from '@angular/core';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { ListGroupItemDirective } from './list-group-item.directive';
4-
import { By } from '@angular/platform-browser';
54

65
class MockElementRef extends ElementRef {}
76

@@ -11,10 +10,8 @@ class MockElementRef extends ElementRef {}
1110
class TestComponent {}
1211

1312
describe('ListGroupItemDirective', () => {
14-
1513
let component: TestComponent;
1614
let fixture: ComponentFixture<TestComponent>;
17-
let liEl: DebugElement;
1815

1916
beforeEach(() => {
2017
TestBed.configureTestingModule({
@@ -24,13 +21,14 @@ describe('ListGroupItemDirective', () => {
2421
});
2522
fixture = TestBed.createComponent(TestComponent);
2623
component = fixture.componentInstance;
27-
liEl = fixture.debugElement.query(By.css('li'));
2824

2925
fixture.detectChanges(); // initial binding
3026
});
3127

3228
it('should create an instance', () => {
33-
const directive = new ListGroupItemDirective(liEl);
34-
expect(directive).toBeTruthy();
29+
TestBed.runInInjectionContext(() => {
30+
const directive = new ListGroupItemDirective();
31+
expect(directive).toBeTruthy();
32+
});
3533
});
3634
});
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,74 @@
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';
211
import { Colors } from '../coreui.types';
312

413
@Directive({
514
selector: '[cListGroupItem], c-list-group-item',
615
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+
}
824
})
925
export class ListGroupItemDirective {
10-
11-
constructor(
12-
private hostElement: ElementRef
13-
) { }
26+
readonly hostElement = inject(ElementRef);
1427

1528
/**
1629
* Toggle the active state for the component.
17-
* @type boolean
30+
* @type InputSignal<boolean | undefined>
1831
*/
19-
@Input() active?: boolean;
32+
readonly active: InputSignal<boolean | undefined> = input();
2033

2134
/**
2235
* Sets the color context of the component to one of CoreUI’s themed colors.
23-
* @type Colors
36+
* @type InputSignal<boolean | undefined>
2437
*/
25-
@Input() color?: Colors;
38+
readonly color: InputSignal<Colors | undefined> = input();
2639

2740
/**
2841
* Set disabled attr for the host element. [docs]
2942
* @type boolean
3043
*/
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 });
4745

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(() => {
5447
const host: HTMLElement = this.hostElement.nativeElement;
5548
return {
5649
'list-group-item': true,
5750
'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()
6154
};
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+
});
6370

71+
readonly ariaCurrent = computed(() => {
72+
return <boolean>this.active() || null;
73+
});
6474
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { TestBed } from '@angular/core/testing';
12
import { ListGroupDirective } from './list-group.directive';
23

34
describe('ListGroupDirective', () => {
45
it('should create an instance', () => {
5-
const directive = new ListGroupDirective();
6-
expect(directive).toBeTruthy();
6+
TestBed.runInInjectionContext(() => {
7+
const directive = new ListGroupDirective();
8+
expect(directive).toBeTruthy();
9+
});
710
});
811
});
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
import { booleanAttribute, Directive, HostBinding, Input } from '@angular/core';
1+
import { booleanAttribute, computed, Directive, input, InputSignalWithTransform } from '@angular/core';
22
import { Sizes } from '../coreui.types';
33

44
@Directive({
55
selector: '[cListGroup]',
66
standalone: true,
7-
host: { class: 'list-group' }
7+
host: {
8+
class: 'list-group',
9+
'[class]': 'hostClasses()'
10+
}
811
})
912
export class ListGroupDirective {
1013
/**
1114
* Remove some borders and rounded corners to render list group items edge-to-edge in a parent component (e.g., `<CCard>`).
1215
* @type boolean
1316
*/
14-
@Input({ transform: booleanAttribute }) flush: string | boolean = false;
17+
readonly flush: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute });
1518

1619
/**
1720
* Specify horizontal layout type.
1821
*/
19-
@Input() horizontal?: boolean | Sizes;
22+
readonly horizontal = input<boolean | Sizes>();
2023

21-
@HostBinding('class')
22-
get hostClasses(): any {
24+
readonly hostClasses = computed(() => {
25+
const horizontal = this.horizontal();
2326
return {
2427
'list-group': true,
25-
'list-group-horizontal': this.horizontal === true || this.horizontal === '',
26-
[`list-group-horizontal-${this.horizontal}`]: !!this.horizontal && typeof this.horizontal !== 'boolean',
27-
'list-group-flush': this.flush
28-
};
29-
}
28+
'list-group-horizontal': horizontal === true || horizontal === '',
29+
[`list-group-horizontal-${horizontal}`]: !!horizontal && typeof horizontal !== 'boolean',
30+
'list-group-flush': this.flush()
31+
} as Record<string, boolean>;
32+
});
3033
}

0 commit comments

Comments
 (0)