Skip to content

feat(tree): Add material styled tree #8458

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
Nov 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
2 changes: 1 addition & 1 deletion src/cdk/tree/control/flat-tree-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('CdkFlatTreeControl', () => {
expect(treeControl.expansionModel.selected.length)
.toBe(2, 'Expect two dataNodes in expansionModel');

treeControl.collapse(seconNode);
treeControl.collapse(secondNode);

expect(treeControl.isExpanded(secondNode))
.toBeFalsy('Expect second node to be collapsed');
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/tree/control/nested-tree-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Observable} from 'rxjs/Observable';
import {first} from 'rxjs/operators';
import {take} from 'rxjs/operators/take';
import {BaseTreeControl} from './base-tree-control';

/** Nested tree control. Able to expand/collapse a subtree recursively for NestedNode type. */
Expand Down Expand Up @@ -40,7 +40,7 @@ export class NestedTreeControl<T> extends BaseTreeControl<T> {
/** A helper function to get descendants recursively. */
protected _getDescendants(descendants: T[], dataNode: T): void {
descendants.push(dataNode);
first.call(this.getChildren(dataNode)).subscribe(children => {
this.getChildren(dataNode).pipe(take(1)).subscribe(children => {
if (children && children.length > 0) {
children.forEach((child: T) => this._getDescendants(descendants, child));
}
Expand Down
6 changes: 3 additions & 3 deletions src/cdk/tree/nested-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
OnDestroy,
QueryList,
} from '@angular/core';
import {takeUntil} from 'rxjs/operators';
import {takeUntil} from 'rxjs/operators/takeUntil';
import {CdkTree} from './tree';
import {CdkTreeNodeOutlet} from './outlet';
import {CdkTreeNode} from './node';
Expand Down Expand Up @@ -58,14 +58,14 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
}

ngAfterContentInit() {
takeUntil.call(this._tree.treeControl.getChildren(this.data), this._destroyed)
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();
});
takeUntil.call(this.nodeOutlet.changes, this._destroyed)
this.nodeOutlet.changes.pipe(takeUntil(this._destroyed))
.subscribe((_) => this._addChildrenNodes());
}

Expand Down
7 changes: 3 additions & 4 deletions src/cdk/tree/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
OnDestroy,
TemplateRef
} from '@angular/core';
import {takeUntil} from 'rxjs/operators';
import {takeUntil} from 'rxjs/operators/takeUntil';
import {Subject} from 'rxjs/Subject';
import {CdkTree} from './tree';
import {getTreeControlFunctionsMissingError} from './tree-errors';
Expand Down Expand Up @@ -69,15 +69,14 @@ export class CdkTreeNodeDef<T> {
host: {
'[attr.role]': 'role',
'class': 'cdk-tree-node',
'tabindex': '0',
},
})
export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
/**
* The most recently created `CdkTreeNode`. We save it in static variable so we can retrieve it
* in `CdkTree` and set the data to it.
*/
static mostRecentTreeNode: CdkTreeNode<any>;
static mostRecentTreeNode: CdkTreeNode<any> | null = null;

/** Subject that emits when the component has been destroyed. */
protected _destroyed = new Subject<void>();
Expand Down Expand Up @@ -118,7 +117,7 @@ export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
if (!this._tree.treeControl.getChildren) {
throw getTreeControlFunctionsMissingError();
}
takeUntil.call(this._tree.treeControl.getChildren(this._data), this._destroyed)
this._tree.treeControl.getChildren(this._data).pipe(takeUntil(this._destroyed))
.subscribe(children => {
this.role = children ? 'group' : 'treeitem';
});
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/tree/padding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {Directionality} from '@angular/cdk/bidi';
import {coerceNumberProperty} from '@angular/cdk/coercion';
import {Directive, ElementRef, Input, OnDestroy, Optional, Renderer2} from '@angular/core';
import {takeUntil} from 'rxjs/operators';
import {takeUntil} from 'rxjs/operators/takeUntil';
import {Subject} from 'rxjs/Subject';
import {CdkTreeNode} from './node';
import {CdkTree} from './tree';
Expand Down Expand Up @@ -49,7 +49,7 @@ export class CdkTreeNodePadding<T> implements OnDestroy {
@Optional() private _dir: Directionality) {
this._setPadding();
if (this._dir) {
takeUntil.call(this._dir.change, this._destroyed).subscribe(() => this._setPadding());
this._dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => this._setPadding());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/cdk/tree/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {CollectionViewer, DataSource} from '@angular/cdk/collections';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {Observable} from 'rxjs/Observable';
import {combineLatest} from 'rxjs/observable/combineLatest';
import {map} from 'rxjs/operators';
import {map} from 'rxjs/operators/map';

import {TreeControl} from './control/tree-control';
import {FlatTreeControl} from './control/flat-tree-control';
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('CdkTree', () => {
it('with rendered dataNodes', () => {
const nodes = getNodes(treeElement);

expect(nodes).not.toBe(undefined);
expect(nodes).toBeDefined('Expect nodes to be defined');
expect(nodes[0].classList).toContain('customNodeClass');
});

Expand Down
10 changes: 6 additions & 4 deletions src/cdk/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
ViewEncapsulation,
} from '@angular/core';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {takeUntil} from 'rxjs/operators';
import {takeUntil} from 'rxjs/operators/takeUntil';
import {Subject} from 'rxjs/Subject';
import {CdkTreeNodeDef, CdkTreeNode, CdkTreeNodeOutletContext} from './node';
import {CdkTreeNodeOutlet} from './outlet';
Expand Down Expand Up @@ -155,7 +155,7 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {

/** Set up a subscription for the data provided by the data source. */
private _observeRenderChanges() {
takeUntil.call(this.dataSource.connect(this), this._destroyed)
this.dataSource.connect(this).pipe(takeUntil(this._destroyed))
.subscribe(data => {
this._data = data;
this._renderNodeChanges(data);
Expand Down Expand Up @@ -191,7 +191,7 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
if (this._nodeDefs.length == 1) { return this._nodeDefs.first; }

const nodeDef =
this._nodeDefs.find(def => def.when && def.when(data, i)) || this._defaultNodeDef;
this._nodeDefs.find(def => def.when && def.when(i, data)) || this._defaultNodeDef;
if (!nodeDef) { throw getTreeMissingMatchingNodeDefError(); }

return nodeDef;
Expand All @@ -214,7 +214,9 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
// Set the data to just created `CdkTreeNode`.
// The `CdkTreeNode` created from `createEmbeddedView` will be saved in static variable
// `mostRecentTreeNode`. We get it from static variable and pass the node data to it.
CdkTreeNode.mostRecentTreeNode.data = nodeData;
if (CdkTreeNode.mostRecentTreeNode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what circumstances would this be undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to initialize mostRecentTreeNode because of the error /usr/local/google/home/tinagao/WebstormProjects/material2/src/cdk/tree/node.ts:79:10: Metadata collected contains an error that will be reported at runtime: Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler.

So I initialize the static variable to be null, and we have to check whether it's null here.

But the value shouldn't be null or undefined when we call it from tree.ts

CdkTreeNode.mostRecentTreeNode.data = nodeData;
}

this._changeDetectorRef.detectChanges();
}
Expand Down
4 changes: 4 additions & 0 deletions src/demo-app/demo-material-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
MatTabsModule,
MatToolbarModule,
MatTooltipModule,
MatTreeModule,
MatStepperModule,
} from '@angular/material';
import {MatNativeDateModule, MatRippleModule} from '@angular/material';
Expand All @@ -48,6 +49,7 @@ import {PlatformModule} from '@angular/cdk/platform';
import {ObserversModule} from '@angular/cdk/observers';
import {PortalModule} from '@angular/cdk/portal';
import {CdkTableModule} from '@angular/cdk/table';
import {CdkTreeModule} from '@angular/cdk/tree';

/**
* NgModule that includes all Material modules that are required to serve the demo-app.
Expand Down Expand Up @@ -85,8 +87,10 @@ import {CdkTableModule} from '@angular/cdk/table';
MatTabsModule,
MatToolbarModule,
MatTooltipModule,
MatTreeModule,
MatNativeDateModule,
CdkTableModule,
CdkTreeModule,
A11yModule,
BidiModule,
CdkAccordionModule,
Expand Down
2 changes: 2 additions & 0 deletions src/demo-app/system-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ System.config({
'@angular/cdk/scrolling': 'dist/packages/cdk/scrolling/index.js',
'@angular/cdk/stepper': 'dist/packages/cdk/stepper/index.js',
'@angular/cdk/table': 'dist/packages/cdk/table/index.js',
'@angular/cdk/tree': 'dist/packages/cdk/tree/index.js',

'@angular/material/autocomplete': 'dist/packages/material/autocomplete/index.js',
'@angular/material/button': 'dist/packages/material/button/index.js',
Expand Down Expand Up @@ -86,6 +87,7 @@ System.config({
'@angular/material/tabs': 'dist/packages/material/tabs/index.js',
'@angular/material/toolbar': 'dist/packages/material/toolbar/index.js',
'@angular/material/tooltip': 'dist/packages/material/tooltip/index.js',
'@angular/material/tree': 'dist/packages/material/tree/index.js',
},
packages: {
// Thirdparty barrels.
Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/theming/_all-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@import '../../tabs/tabs-theme';
@import '../../toolbar/toolbar-theme';
@import '../../tooltip/tooltip-theme';
@import '../../tree/tree-theme';
@import '../../snack-bar/snack-bar-theme';
@import '../../form-field/form-field-theme';

Expand Down Expand Up @@ -62,5 +63,6 @@
@include mat-tabs-theme($theme);
@include mat-toolbar-theme($theme);
@include mat-tooltip-theme($theme);
@include mat-tree-theme($theme);
@include mat-snack-bar-theme($theme);
}
2 changes: 2 additions & 0 deletions src/lib/core/typography/_all-typography.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
@import '../../tabs/tabs-theme';
@import '../../toolbar/toolbar-theme';
@import '../../tooltip/tooltip-theme';
@import '../../tree/tree-theme';
@import '../../snack-bar/snack-bar-theme';
@import '../option/option-theme';
@import '../option/optgroup-theme';
Expand Down Expand Up @@ -66,6 +67,7 @@
@include mat-tabs-typography($config);
@include mat-toolbar-typography($config);
@include mat-tooltip-typography($config);
@include mat-tree-typography($config);
@include mat-list-typography($config);
@include mat-option-typography($config);
@include mat-optgroup-typography($config);
Expand Down
1 change: 1 addition & 0 deletions src/lib/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ export * from '@angular/material/table';
export * from '@angular/material/tabs';
export * from '@angular/material/toolbar';
export * from '@angular/material/tooltip';
export * from '@angular/material/tree';
27 changes: 27 additions & 0 deletions src/lib/tree/_tree-theme.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@import '../core/theming/palette';
@import '../core/theming/theming';
@import '../core/typography/typography-utils';

@mixin mat-tree-theme($theme) {
$background: map-get($theme, background);
$foreground: map-get($theme, foreground);

.mat-tree {
background: mat-color($background, 'card');
}

.mat-tree-node {
color: mat-color($foreground, text);
}
}

@mixin mat-tree-typography($config) {
.mat-tree {
font-family: mat-font-family($config);
}

.mat-tree-node {
font-weight: mat-font-weight($config, body-1);
font-size: mat-font-size($config, body-1);
}
}
9 changes: 9 additions & 0 deletions src/lib/tree/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export * from './public-api';
73 changes: 73 additions & 0 deletions src/lib/tree/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {
ContentChildren,
Directive,
Input,
QueryList
} from '@angular/core';
import {
CdkNestedTreeNode,
CdkTreeNodeDef,
CdkTreeNode,
} from '@angular/cdk/tree';
import {MatTreeNodeOutlet} from './outlet';

/**
* Wrapper for the CdkTree node with Material design styles.
*/
// TODO(tinayuangao): use mixinTabIndex
@Directive({
selector: 'mat-tree-node',
exportAs: 'matTreeNode',
host: {
'[attr.role]': 'role',
'class': 'mat-tree-node',
'tabindex': '0',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add TODO for applying mixinTabIndex?

},
providers: [{provide: CdkTreeNode, useExisting: MatTreeNode}]
})
export class MatTreeNode<T> extends CdkTreeNode<T> {
@Input() role: 'treeitem' | 'group' = 'treeitem';
}

/**
* Wrapper for the CdkTree node definition with Material design styles.
*/
@Directive({
selector: '[matTreeNodeDef]',
inputs: [
'when: matTreeNodeDefWhen'
],
providers: [{provide: CdkTreeNodeDef, useExisting: MatTreeNodeDef}]
})
export class MatTreeNodeDef<T> extends CdkTreeNodeDef<T> {
@Input('matTreeNode') data: T;
}

/**
* Wrapper for the CdkTree nested node with Material design styles.
*/
@Directive({
selector: 'mat-nested-tree-node',
exportAs: 'matNestedTreeNode',
host: {
'[attr.role]': 'role',
'class': 'mat-nested-tree-node',
},
providers: [
{provide: CdkNestedTreeNode, useExisting: MatNestedTreeNode},
{provide: CdkTreeNode, useExisting: MatNestedTreeNode}
]
})
export class MatNestedTreeNode<T> extends CdkNestedTreeNode<T> {
@Input('matNestedTreeNode') node: T;

@ContentChildren(MatTreeNodeOutlet) nodeOutlet: QueryList<MatTreeNodeOutlet>;
}
23 changes: 23 additions & 0 deletions src/lib/tree/outlet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {CdkTreeNodeOutlet} from '@angular/cdk/tree';
import {
Directive,
ViewContainerRef,
} from '@angular/core';

/**
* Outlet for nested CdkNode. Put `[matTreeNodeOutlet]` on a tag to place children dataNodes
* inside the outlet.
*/
@Directive({
selector: '[matTreeNodeOutlet]'
})
export class MatTreeNodeOutlet implements CdkTreeNodeOutlet {
constructor(public viewContainer: ViewContainerRef) {}
}
Loading