fix(aio): restore component-styles/exclude hidden children

This commit is contained in:
Ward Bell 2017-06-19 11:08:30 -07:00 committed by Hans
parent ca51e020cb
commit 0034bb28e5
6 changed files with 51 additions and 25 deletions

View File

@ -135,6 +135,11 @@
"title": "Component Interaction", "title": "Component Interaction",
"tooltip": "Share information between different directives and components." "tooltip": "Share information between different directives and components."
}, },
{
"url": "guide/component-styles",
"title": "Component Styles",
"tooltip": "Add CSS styles that are specific to a component."
},
{ {
"url": "guide/dynamic-component-loader", "url": "guide/dynamic-component-loader",
"title": "Dynamic Components", "title": "Dynamic Components",

View File

@ -20,7 +20,7 @@
</button> </button>
<div class="heading-children" [ngClass]="classes"> <div class="heading-children" [ngClass]="classes">
<aio-nav-item *ngFor="let node of node.children" [level]="level + 1" [isWide]="isWide" <aio-nav-item *ngFor="let node of nodeChildren" [level]="level + 1" [isWide]="isWide"
[node]="node" [selectedNodes]="selectedNodes"></aio-nav-item> [node]="node" [selectedNodes]="selectedNodes"></aio-nav-item>
</div> </div>
</div> </div>

View File

@ -1,4 +1,4 @@
import { TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser'; import { By } from '@angular/platform-browser';
import { SimpleChange, SimpleChanges, NO_ERRORS_SCHEMA } from '@angular/core'; import { SimpleChange, SimpleChanges, NO_ERRORS_SCHEMA } from '@angular/core';
@ -24,7 +24,7 @@ describe('NavItemComponent', () => {
// Enough to triggers component's ngOnChange method // Enough to triggers component's ngOnChange method
function onChanges() { function onChanges() {
component.ngOnChanges({node: <SimpleChange><any> 'anything' }); component.ngOnChanges();
} }
beforeEach(() => { beforeEach(() => {
@ -169,27 +169,46 @@ describe('NavItemComponent', () => {
}); });
describe('(via TestBed)', () => { describe('(via TestBed)', () => {
it('should pass the `isWide` property to all child nav-items', () => { let component: NavItemComponent;
let fixture: ComponentFixture<NavItemComponent>;
beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [NavItemComponent], declarations: [NavItemComponent],
schemas: [NO_ERRORS_SCHEMA] schemas: [NO_ERRORS_SCHEMA]
}); });
const fixture = TestBed.createComponent(NavItemComponent); fixture = TestBed.createComponent(NavItemComponent);
fixture.componentInstance.node = { component = fixture.componentInstance;
component.node = {
title: 'x', title: 'x',
children: [{ title: 'a' }, { title: 'b' }] children: [
{ title: 'a' },
{ title: 'b', hidden: true},
{ title: 'c' }
]
}; };
});
fixture.componentInstance.isWide = true; it('should not show the hidden child nav-item', () => {
component.ngOnChanges(); // assume component.ngOnChanges ignores arguments
fixture.detectChanges();
const children = fixture.debugElement.queryAll(By.directive(NavItemComponent));
expect(children.length).toEqual(2);
});
it('should pass the `isWide` property to all displayed child nav-items', () => {
component.isWide = true;
component.ngOnChanges(); // assume component.ngOnChanges ignores arguments
fixture.detectChanges(); fixture.detectChanges();
let children = fixture.debugElement.queryAll(By.directive(NavItemComponent)); let children = fixture.debugElement.queryAll(By.directive(NavItemComponent));
expect(children.length).toEqual(2); expect(children.length).toEqual(2, 'when IsWide is true');
children.forEach(child => expect(child.componentInstance.isWide).toBe(true)); children.forEach(child => expect(child.componentInstance.isWide).toBe(true));
fixture.componentInstance.isWide = false; component.isWide = false;
component.ngOnChanges();
fixture.detectChanges(); fixture.detectChanges();
children = fixture.debugElement.queryAll(By.directive(NavItemComponent)); children = fixture.debugElement.queryAll(By.directive(NavItemComponent));
expect(children.length).toEqual(2); expect(children.length).toEqual(2, 'when IsWide is false');
children.forEach(child => expect(child.componentInstance.isWide).toBe(false)); children.forEach(child => expect(child.componentInstance.isWide).toBe(false));
}); });
}); });

View File

@ -1,4 +1,4 @@
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core'; import { Component, Input, OnChanges} from '@angular/core';
import { NavigationNode } from 'app/navigation/navigation.model'; import { NavigationNode } from 'app/navigation/navigation.model';
@Component({ @Component({
@ -14,19 +14,21 @@ export class NavItemComponent implements OnChanges {
isExpanded = false; isExpanded = false;
isSelected = false; isSelected = false;
classes: {[index: string]: boolean }; classes: {[index: string]: boolean };
nodeChildren: NavigationNode[];
ngOnChanges(changes: SimpleChanges) { ngOnChanges() {
if (changes['selectedNodes'] || changes['node'] || changes['isWide']) { this.nodeChildren = this.node && this.node.children ? this.node.children.filter(n => !n.hidden) : [];
if (this.selectedNodes) {
const ix = this.selectedNodes.indexOf(this.node); if (this.selectedNodes) {
this.isSelected = ix !== -1; // this node is the selected node or its ancestor const ix = this.selectedNodes.indexOf(this.node);
this.isExpanded = this.isSelected || // expand if selected or ... this.isSelected = ix !== -1; // this node is the selected node or its ancestor
// preserve expanded state when display is wide; collapse in mobile. this.isExpanded = this.isSelected || // expand if selected or ...
(this.isWide && this.isExpanded); // preserve expanded state when display is wide; collapse in mobile.
} else { (this.isWide && this.isExpanded);
this.isSelected = false; } else {
} this.isSelected = false;
} }
this.setClasses(); this.setClasses();
} }

View File

@ -8,7 +8,7 @@ describe('NavMenuComponent (class-only)', () => {
it('should filter out hidden nodes', () => { it('should filter out hidden nodes', () => {
const component = new NavMenuComponent(); const component = new NavMenuComponent();
const nodes: NavigationNode[] = const nodes: NavigationNode[] =
[ { title: 'a' }, { title: 'b', hidden: 'true'}, { title: 'c'} ]; [ { title: 'a' }, { title: 'b', hidden: true}, { title: 'c'} ];
component.nodes = nodes; component.nodes = nodes;
expect(component.filteredNodes).toEqual([ nodes[0], nodes[2] ]); expect(component.filteredNodes).toEqual([ nodes[0], nodes[2] ]);
}); });

View File

@ -6,7 +6,7 @@ export interface NavigationNode {
url?: string; url?: string;
title?: string; title?: string;
tooltip?: string; tooltip?: string;
hidden?: string; hidden?: boolean;
children?: NavigationNode[]; children?: NavigationNode[];
} }