Skip to content

Commit

Permalink
Refactor BasePageComponent to use input() for bglight property and up…
Browse files Browse the repository at this point in the history
…date currentClass method; enhance HighlightDirective with improved mouse event handling
  • Loading branch information
robsonalvesdev committed Dec 19, 2024
1 parent b1da18e commit 43a72c4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 22 deletions.
20 changes: 1 addition & 19 deletions src/app/components/base-page/base-page.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,6 @@ describe('BasePageComponent', () => {
});

it('should have bglight input property set to false by default', () => {
expect(component.bglight).toBeFalse();
});

it('should allow setting bglight input property to true', () => {
component.bglight = true;
fixture.detectChanges();
expect(component.bglight).toBeTrue();
});

it('currentClass should return correct class object when bglight is false', () => {
component.bglight = false;
fixture.detectChanges();
expect(component.currentClass()).toEqual({ 'bg-light': false });
});

it('currentClass should return correct class object when bglight is true', () => {
component.bglight = true;
fixture.detectChanges();
expect(component.currentClass()).toEqual({ 'bg-light': true });
expect(component.bglight()).toBeFalse();
});
});
6 changes: 3 additions & 3 deletions src/app/components/base-page/base-page.component.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, input } from '@angular/core';

@Component({
template: '',
imports: [CommonModule],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BasePageComponent {
@Input({ required: false }) bglight = false;
readonly bglight = input(false);

// HACK: This is a workaround for the issue with the `ngClass` directive
currentClass = () => {
return {
'bg-light': this.bglight,
'bg-light': this.bglight(),
};
};
}
2 changes: 2 additions & 0 deletions src/app/directives/highlight.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ export class HighlightDirective {
@HostListener('mouseenter') onMouseEnter() {
this.highlight('btn-light');
}

@HostListener('mouseleave') onMouseLeave() {
this.highlight('btn-light', false);
}

private highlight(color: string, visibility = true) {
if (visibility) {
this.el.nativeElement.classList.add(color);
Expand Down

0 comments on commit 43a72c4

Please sign in to comment.