Skip to content

Commit

Permalink
feat(design): change daffCompactableMixin to a directive
Browse files Browse the repository at this point in the history
  • Loading branch information
xelaint authored and damienwebdev committed Jul 30, 2024
1 parent aa77469 commit faef6bc
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 119 deletions.
4 changes: 2 additions & 2 deletions libs/design/callout/src/callout/callout.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { DaffCalloutComponent } from './callout.component';
class WrapperComponent {
color: DaffPalette;
textAlignment: DaffTextAlignment;
compact = false;
compact: boolean;
}

describe('@daffodil/design/callout | DaffCalloutComponent', () => {
Expand Down Expand Up @@ -86,6 +86,6 @@ describe('@daffodil/design/callout | DaffCalloutComponent', () => {
wrapper.compact = true;
fixture.detectChanges();

expect(component.compact).toEqual(true);
expect(de.nativeElement.classList.contains('daff-compact')).toEqual(true);
});
});
15 changes: 9 additions & 6 deletions libs/design/callout/src/callout/callout.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import {
Renderer2,
} from '@angular/core';

import { DaffCompactableDirective } from '@daffodil/design';
import {
DaffArticleEncapsulatedDirective,
DaffColorable,
daffColorMixin,
DaffCompactable,
daffCompactableMixin,
DaffManageContainerLayoutDirective,
DaffTextAlignableDirective,
} from '@daffodil/design';
Expand All @@ -21,10 +20,10 @@ import {
* An _elementRef and an instance of renderer2 are needed for the Colorable mixin
*/
class DaffCalloutBase {
constructor(public _elementRef: ElementRef, public _renderer: Renderer2) {}
constructor(public _elementRef: ElementRef, public _renderer: Renderer2) { }
}

const _daffCalloutBase = daffColorMixin(daffCompactableMixin(DaffCalloutBase));
const _daffCalloutBase = daffColorMixin(DaffCalloutBase);

/**
* @inheritdoc
Expand All @@ -36,18 +35,22 @@ const _daffCalloutBase = daffColorMixin(daffCompactableMixin(DaffCalloutBase));
encapsulation: ViewEncapsulation.None,
//todo(damienwebdev): remove once decorators hit stage 3 - https://github.com/microsoft/TypeScript/issues/7342
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
inputs: ['color', 'compact'],
inputs: ['color'],
hostDirectives: [
{ directive: DaffArticleEncapsulatedDirective },
{ directive: DaffManageContainerLayoutDirective },
{
directive: DaffTextAlignableDirective,
inputs: ['textAlignment'],
},
{
directive: DaffCompactableDirective,
inputs: ['compact'],
},
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DaffCalloutComponent extends _daffCalloutBase implements DaffColorable, DaffCompactable {
export class DaffCalloutComponent extends _daffCalloutBase implements DaffColorable {
constructor(
private elementRef: ElementRef,
private renderer: Renderer2,
Expand Down
4 changes: 2 additions & 2 deletions libs/design/hero/src/hero/hero.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DaffHeroComponent } from './hero.component';
class WrapperComponent {
color: DaffPalette;
textAlignment: DaffTextAlignment;
compact = false;
compact: boolean;
}

describe('@daffodil/design/hero | DaffHeroComponent', () => {
Expand Down Expand Up @@ -83,6 +83,6 @@ describe('@daffodil/design/hero | DaffHeroComponent', () => {
wrapper.compact = true;
fixture.detectChanges();

expect(component.compact).toEqual(true);
expect(de.nativeElement.classList.contains('daff-compact')).toEqual(true);
});
});
15 changes: 8 additions & 7 deletions libs/design/hero/src/hero/hero.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import {
ChangeDetectionStrategy,
HostBinding,
Renderer2,
SimpleChanges,
OnChanges,
} from '@angular/core';

import { DaffCompactableDirective } from '@daffodil/design';
import {
DaffArticleEncapsulatedDirective,
DaffColorable,
daffColorMixin,
DaffCompactable,
daffCompactableMixin,
DaffManageContainerLayoutDirective,
DaffTextAlignableDirective,
} from '@daffodil/design';
Expand All @@ -26,7 +23,7 @@ class DaffHeroBase {
constructor(public _elementRef: ElementRef, public _renderer: Renderer2) {}
}

const _daffHeroBase = daffColorMixin(daffCompactableMixin(DaffHeroBase));
const _daffHeroBase = daffColorMixin(DaffHeroBase);

/**
* @inheritdoc
Expand All @@ -38,18 +35,22 @@ const _daffHeroBase = daffColorMixin(daffCompactableMixin(DaffHeroBase));
encapsulation: ViewEncapsulation.None,
//todo(damienwebdev): remove once decorators hit stage 3 - https://github.com/microsoft/TypeScript/issues/7342
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
inputs: ['color', 'compact'],
inputs: ['color'],
hostDirectives: [
{ directive: DaffArticleEncapsulatedDirective },
{ directive: DaffManageContainerLayoutDirective },
{
directive: DaffTextAlignableDirective,
inputs: ['textAlignment'],
},
{
directive: DaffCompactableDirective,
inputs: ['compact'],
},
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DaffHeroComponent extends _daffHeroBase implements DaffColorable, DaffCompactable {
export class DaffHeroComponent extends _daffHeroBase implements DaffColorable {
constructor(
private elementRef: ElementRef,
private renderer: Renderer2,
Expand Down
45 changes: 0 additions & 45 deletions libs/design/src/core/compactable/compactable-mixin.ts

This file was deleted.

74 changes: 74 additions & 0 deletions libs/design/src/core/compactable/compactable.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
Component,
DebugElement,
} from '@angular/core';
import {
waitForAsync,
ComponentFixture,
TestBed,
} from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { DaffCompactableDirective } from './compactable.directive';

@Component({
template: `
<div daffCompactable [compact]="compact"></div>`,
})

class WrapperComponent {
compact: boolean;
}

describe('@daffodil/design | DaffCompactableDirective', () => {
let wrapper: WrapperComponent;
let de: DebugElement;
let fixture: ComponentFixture<WrapperComponent>;
let directive: DaffCompactableDirective;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [
WrapperComponent,
],
imports: [
DaffCompactableDirective,
],
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(WrapperComponent);
wrapper = fixture.componentInstance;
de = fixture.debugElement.query(By.css('[daffCompactable]'));

directive = de.injector.get(DaffCompactableDirective);
fixture.detectChanges();
});

it('should create', () => {
expect(wrapper).toBeTruthy();
expect(directive).toBeTruthy();
});

it('should take compact as an input', () => {
expect(directive.compact).toEqual(wrapper.compact);
});

it('should add a class of .daff-compact to the host element if compact is set to true', () => {
wrapper.compact = true;
fixture.detectChanges();

expect(de.classes).toEqual(jasmine.objectContaining({
'daff-compact': true,
}));
});

it('should not add a class of .daff-compact to the host element if compact is set to false', () => {
wrapper.compact = false;
fixture.detectChanges();

expect(de.classes['daff-skeleton']).toBeUndefined();
});
});
14 changes: 14 additions & 0 deletions libs/design/src/core/compactable/compactable.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
Directive,
HostBinding,
Input,
} from '@angular/core';

@Directive({
selector: '[daffCompactable]',
standalone: true,
})

export class DaffCompactableDirective {
@Input() @HostBinding('class.daff-compact') compact = false;
}
56 changes: 0 additions & 56 deletions libs/design/src/core/compactable/compactable.spec.ts

This file was deleted.

2 changes: 1 addition & 1 deletion libs/design/src/core/compactable/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { DaffCompactable } from './compactable';
export { daffCompactableMixin } from './compactable-mixin';
export { DaffCompactableDirective } from './compactable.directive';

0 comments on commit faef6bc

Please sign in to comment.