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

Use [ngClass] to avoid conflicts with [class.x] bindings #1651

Merged
merged 2 commits into from
Feb 28, 2017
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
40 changes: 35 additions & 5 deletions src/spec/tabset.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const html = `
<tab heading="tab0">tab0 content</tab>
<tab *ngFor="let tab of tabs"
[disabled]="tab.disabled"
[customClass]="tab.customClass"
[active]="tab.active"
[removable]="tab.removable"
(select)="_select($event)"
Expand All @@ -19,6 +20,10 @@ const html = `
</tabset>
`;

function getTabItems(nativeEl: HTMLElement): NodeListOf<Element> {
return nativeEl.querySelectorAll('.nav-item');
}

function getTabTitles(nativeEl: HTMLElement): NodeListOf<Element> {
return nativeEl.querySelectorAll('.nav-link');
}
Expand All @@ -28,18 +33,18 @@ function getTabContent(nativeEl: HTMLElement): NodeListOf<Element> {
}

function expectActiveTabs(nativeEl: HTMLElement, active: boolean[]): void {
const tabTitles = getTabTitles(nativeEl);
const tabItems = getTabItems(nativeEl);
const tabContent = getTabContent(nativeEl);

expect(tabTitles.length).toBe(active.length);
expect(tabItems.length).toBe(active.length);
expect(tabContent.length).toBe(active.length);

for (let i = 0; i < active.length; i++) {
if (active[i]) {
expect(tabTitles[i].classList).toContain('active');
expect(tabItems[i].classList).toContain('active');
expect(tabContent[i].classList).toContain('active');
} else {
expect(tabTitles[i].classList).not.toContain('active');
expect(tabItems[i].classList).not.toContain('active');
expect(tabContent[i].classList).not.toContain('active');
}
}
Expand Down Expand Up @@ -175,6 +180,31 @@ describe('Component: Tabs', () => {
heading: 'tab3'
}));
});

it('should set class on a tab item through [customClass]', () => {
const tabItems = getTabItems(element);

expect(tabItems[1].classList).toContain('testCustomClass');
});

it('should prevent interference of [customClass] and state classes', () => {
const tabItems = getTabItems(element);
const tabTitles = getTabTitles(element);

expect(tabItems[1].classList).toContain('testCustomClass');

(tabTitles[1] as HTMLAnchorElement).click();
fixture.detectChanges();
expect(tabItems[1].classList).toContain('testCustomClass');
expectActiveTabs(element, [false, true, false, false]);

context.tabs[0].customClass = 'otherCustomClass';
fixture.detectChanges();

expect(tabItems[1].classList).not.toContain('testCustomClass');
expect(tabItems[1].classList).toContain('otherCustomClass');
expectActiveTabs(element, [false, true, false, false]);
});
});

@Component({
Expand All @@ -186,7 +216,7 @@ class TestTabsetComponent {
public isVertical:Boolean = false;
public isJustified:Boolean = false;
public tabs:any[] = [
{title: 'tab1', content: 'tab1 content'},
{title: 'tab1', content: 'tab1 content', customClass:'testCustomClass'},
{title: 'tab2', content: 'tab2 content', disabled: true},
{title: 'tab3', content: 'tab3 content', removable: true}
];
Expand Down
2 changes: 1 addition & 1 deletion src/tabs/tabset.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TabsetConfig } from './tabset.config';
selector: 'tabset',
template: `
<ul class="nav" [ngClass]="classMap" (click)="$event.preventDefault()">
<li *ngFor="let tabz of tabs" class="nav-item {{tabz.customClass}}"
<li *ngFor="let tabz of tabs" [ngClass]="['nav-item', tabz.customClass || '']"
[class.active]="tabz.active" [class.disabled]="tabz.disabled">
<a href="javascript:void(0);" class="nav-link"
[class.active]="tabz.active" [class.disabled]="tabz.disabled"
Expand Down