Skip to content

test(tree): Add more tests for cdk-tree #8967

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

Merged
merged 1 commit into from
Dec 15, 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
23 changes: 13 additions & 10 deletions src/cdk/tree/nested-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
QueryList,
} from '@angular/core';
import {takeUntil} from 'rxjs/operators/takeUntil';

import {CdkTree} from './tree';
import {CdkTreeNodeOutlet} from './outlet';
import {CdkTreeNode} from './node';
Expand Down Expand Up @@ -43,9 +44,9 @@ import {CdkTreeNode} from './node';
'class': 'cdk-tree-node cdk-nested-tree-node',
'tabindex': '0',
},
providers: [{provide: CdkTreeNode, useExisting: CdkNestedTreeNode}]
})
export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContentInit, OnDestroy {

/** The children data dataNodes of current node. They will be placed in `CdkTreeNodeOutlet`. */
protected _children: T[];

Expand All @@ -59,14 +60,16 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent

ngAfterContentInit() {
this._tree.treeControl.getChildren(this.data).pipe(takeUntil(this._destroyed))
.subscribe(result => {
// In case when nodeOutlet is not in the DOM when children changes, save it in the node
// and add to nodeOutlet when it's available.
this._children = result as T[];
this._addChildrenNodes();
});
.subscribe(result => {
if (result && result.length) {
// In case when nodeOutlet is not in the DOM when children changes, save it in the node
// and add to nodeOutlet when it's available.
this._children = result as T[];
this._addChildrenNodes();
}
});
this.nodeOutlet.changes.pipe(takeUntil(this._destroyed))
.subscribe((_) => this._addChildrenNodes());
.subscribe((_) => this._addChildrenNodes());
}

ngOnDestroy() {
Expand All @@ -78,7 +81,7 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
/** Add children dataNodes to the NodeOutlet */
protected _addChildrenNodes(): void {
this._clear();
if (this.nodeOutlet.length && this._children) {
if (this.nodeOutlet.length && this._children && this._children.length) {
this._children.forEach((child, index) => {
this._tree.insertNode(child, index, this.nodeOutlet.first.viewContainer);
});
Expand All @@ -87,7 +90,7 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent

/** Clear the children dataNodes. */
protected _clear(): void {
if (this.nodeOutlet.first) {
if (this.nodeOutlet && this.nodeOutlet.first) {
this.nodeOutlet.first.viewContainer.clear();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/tree/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
}
this._tree.treeControl.getChildren(this._data).pipe(takeUntil(this._destroyed))
.subscribe(children => {
this.role = children ? 'group' : 'treeitem';
this.role = children && children.length ? 'group' : 'treeitem';
});
}
}
Expand Down
Loading