Skip to content

Commit 8f52541

Browse files
committed
refactor(collapse): animate and visible with linkedSignal()
1 parent 59b1559 commit 8f52541

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

projects/coreui-angular/src/lib/collapse/collapse.directive.spec.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { CollapseDirective } from './collapse.directive';
22
import { Component, DebugElement, ElementRef, Renderer2 } from '@angular/core';
3-
import { ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
44
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
55
import { By } from '@angular/platform-browser';
66

77
class MockElementRef extends ElementRef {}
88

99
@Component({
10-
template: '<div cCollapse horizontal>Test</div>',
10+
template: '<div cCollapse [horizontal]="horizontal" [(visible)]="visible" [animate]="false">Test</div>',
1111
imports: [CollapseDirective]
1212
})
13-
class TestComponent {}
13+
class TestComponent {
14+
horizontal = false;
15+
visible = false;
16+
}
1417

1518
describe('CollapseDirective', () => {
1619
let component: TestComponent;
@@ -26,7 +29,7 @@ describe('CollapseDirective', () => {
2629
fixture = TestBed.createComponent(TestComponent);
2730
component = fixture.componentInstance;
2831
elementRef = fixture.debugElement.query(By.directive(CollapseDirective));
29-
32+
component.visible = false;
3033
fixture.detectChanges(); // initial binding
3134
});
3235

@@ -37,7 +40,17 @@ describe('CollapseDirective', () => {
3740
});
3841
});
3942

40-
it('should have css classes', () => {
43+
it('should have css classes', fakeAsync(() => {
44+
expect(elementRef.nativeElement.style.display).toContain('none');
45+
expect(elementRef.nativeElement).not.toHaveClass('collapse-horizontal');
46+
component.horizontal = true;
47+
component.visible = true;
48+
fixture.detectChanges();
4149
expect(elementRef.nativeElement).toHaveClass('collapse-horizontal');
42-
});
50+
expect(elementRef.nativeElement.style.display).toBe('');
51+
component.horizontal = false;
52+
component.visible = false;
53+
fixture.detectChanges();
54+
expect(elementRef.nativeElement).not.toHaveClass('collapse-horizontal');
55+
}));
4356
});

projects/coreui-angular/src/lib/collapse/collapse.directive.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ElementRef,
1010
inject,
1111
input,
12+
linkedSignal,
1213
OnDestroy,
1314
output,
1415
Renderer2,
@@ -46,10 +47,9 @@ export class CollapseDirective implements OnDestroy {
4647
*/
4748
readonly animateInput = input(true, { transform: booleanAttribute, alias: 'animate' });
4849

49-
readonly animate = signal(true);
50-
51-
readonly #animateInputEffect = effect(() => {
52-
this.animate.set(this.animateInput());
50+
readonly animate = linkedSignal({
51+
source: () => this.animateInput(),
52+
computation: (value: boolean) => value
5353
});
5454

5555
/**
@@ -68,17 +68,14 @@ export class CollapseDirective implements OnDestroy {
6868

6969
readonly visibleChange = output<boolean>();
7070

71-
readonly #visibleInputEffect = effect(() => {
72-
this.visible.set(this.visibleInput());
73-
});
74-
75-
readonly visible = signal(false);
71+
readonly visible = linkedSignal({ source: () => this.visibleInput(), computation: (value: boolean) => value });
7672

7773
readonly #initialized = signal(false);
7874

7975
readonly #visibleEffect = effect(() => {
76+
const visible = this.visible();
8077
if (this.#initialized()) {
81-
this.createPlayer(this.visible());
78+
this.createPlayer(visible);
8279
}
8380
});
8481

0 commit comments

Comments
 (0)