Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensured that it is possible to set custom class on fa-layers element #100

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/lib/layers/layers.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ describe('FaLayersCompoennt', () => {
expect(queryByCss(fixture, '.fa-fw')).toBeTruthy();
});


it('should not include fixed width', () => {
@Component({
selector: 'fa-host',
Expand All @@ -101,5 +100,34 @@ describe('FaLayersCompoennt', () => {
fixture.detectChanges();
expect(queryByCss(fixture, '.fa-fw')).toBeFalsy();
});

it('should allow setting custom class on the host element', () => {
@Component({
selector: 'fa-host',
template: `
<fa-layers class="custom-class" [fixedWidth]="fixedWidth" [size]="size"></fa-layers>
<fa-layers [class.custom-class]="true" [fixedWidth]="fixedWidth" [size]="size"></fa-layers>
<fa-layers [ngClass]="{'custom-class': true}" [fixedWidth]="fixedWidth" [size]="size"></fa-layers>
<fa-layers [fixedWidth]="fixedWidth" [size]="size" class="custom-class"></fa-layers>
<fa-layers [fixedWidth]="fixedWidth" [size]="size" [class.custom-class]="true"></fa-layers>
<fa-layers [fixedWidth]="fixedWidth" [size]="size" [ngClass]="{'custom-class': true}"></fa-layers>
`
})
class HostComponent {
fixedWidth = true;
size = '4x';
}

const fixture = initTest(HostComponent);

fixture.detectChanges();
const elements = fixture.debugElement.queryAll(By.css('fa-layers'));
for (const element of elements) {
expect(element.nativeElement.className).toContain('custom-class');
expect(element.nativeElement.className).toContain('fa-layers');
expect(element.nativeElement.className).toContain('fa-fw');
expect(element.nativeElement.className).toContain('fa-4x');
}
});
});

36 changes: 18 additions & 18 deletions src/lib/layers/layers.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Component, Input, SimpleChanges, OnChanges, OnInit, HostBinding } from '@angular/core';
import { Component, ElementRef, HostBinding, Input, OnChanges, OnInit, Renderer2, SimpleChanges } from '@angular/core';
import { SizeProp } from '@fortawesome/fontawesome-svg-core';
import { faLayerClassList } from '../shared/utils/classlist.util';
import { FaProps } from '../shared/models/props.model';

/**
* Fontawesome layers.
Expand All @@ -12,27 +10,29 @@ import { FaProps } from '../shared/models/props.model';
})
export class FaLayersComponent implements OnInit, OnChanges {
@Input() size?: SizeProp;
@Input() fixedWidth?: boolean;

@HostBinding('class')
hostClass = 'fa-layers';
@Input()
@HostBinding('class.fa-fw')
fixedWidth?: boolean;

constructor(
private renderer: Renderer2,
private elementRef: ElementRef,
) {
}

ngOnInit() {
this.updateClasses();
this.renderer.addClass(this.elementRef.nativeElement, 'fa-layers');
}

ngOnChanges(changes: SimpleChanges) {
if (changes) {
this.updateClasses();
if ('size' in changes) {
if (changes.size.currentValue != null) {
this.renderer.addClass(this.elementRef.nativeElement, `fa-${changes.size.currentValue}`);
}
if (changes.size.previousValue != null) {
this.renderer.removeClass(this.elementRef.nativeElement, `fa-${changes.size.previousValue}`);
}
}
}

updateClasses() {
const classOpts: FaProps = {
size: this.size || null,
fixedWidth: this.fixedWidth,
};
this.hostClass = `fa-layers ${faLayerClassList(classOpts).join(' ')}`;
}

}
11 changes: 0 additions & 11 deletions src/lib/shared/utils/classlist.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,3 @@ export const faClassList = (props: FaProps): string[] => {
.map(key => (classes[key] ? key : null))
.filter(key => key);
};

export const faLayerClassList = (props: FaProps): string[] => {
const classes = {
'fa-fw': props.fixedWidth,
[`fa-${props.size}`]: props.size !== null,
};

return Object.keys(classes)
.map(key => (classes[key] ? key : null))
.filter(key => key);
};