Skip to content

Commit ba134f4

Browse files
devversionjosephperrott
authored andcommitted
fix(schematics): tree schematic not working (#12281)
1 parent f9b9e25 commit ba134f4

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/lib/schematics/tree/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import { of as observableOf } from 'rxjs';
44
import { FlatTreeControl } from '@angular/cdk/tree';
55
import { files } from './example-data';
66

7-
/** File node data with nested structure. */
7+
/** File node data with possible child nodes. */
88
export interface FileNode {
99
name: string;
1010
type: string;
1111
children?: FileNode[];
1212
}
1313

14-
/** Flat node with expandable and level information */
15-
export interface TreeNode {
14+
/**
15+
* Flattened tree node that has been created from a FileNode through the flattener. Flattened
16+
* nodes include level index and whether they can be expanded or not.
17+
*/
18+
export interface FlatTreeNode {
1619
name: string;
1720
type: string;
1821
level: number;
@@ -37,22 +40,22 @@ export interface TreeNode {
3740
export class <%= classify(name) %>Component {
3841

3942
/** The TreeControl controls the expand/collapse state of tree nodes. */
40-
treeControl: FlatTreeControl<TreeNode>;
43+
treeControl: FlatTreeControl<FlatTreeNode>;
4144

4245
/** The TreeFlattener is used to generate the flat list of items from hierarchical data. */
43-
treeFlattener: MatTreeFlattener<FileNode, TreeNode>;
46+
treeFlattener: MatTreeFlattener<FileNode, FlatTreeNode>;
4447

4548
/** The MatTreeFlatDataSource connects the control and flattener to provide data. */
46-
dataSource: MatTreeFlatDataSource<FileNode, TreeNode>;
49+
dataSource: MatTreeFlatDataSource<FileNode, FlatTreeNode>;
4750

4851
constructor() {
4952
this.treeFlattener = new MatTreeFlattener(
5053
this.transformer,
5154
this.getLevel,
5255
this.isExpandable,
5356
this.getChildren);
54-
55-
this.treeControl = new FlatTreeControl<TreeNode>(this.getLevel, this.isExpandable);
57+
58+
this.treeControl = new FlatTreeControl(this.getLevel, this.isExpandable);
5659
this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);
5760
this.dataSource.data = files;
5861
}
@@ -67,23 +70,23 @@ export class <%= classify(name) %>Component {
6770
};
6871
}
6972

70-
/** Get the level of the node */
71-
getLevel(node: TreeNode) {
73+
/** Get the level of the node */
74+
getLevel(node: FlatTreeNode) {
7275
return node.level;
7376
}
7477

7578
/** Get whether the node is expanded or not. */
76-
isExpandable(node: TreeNode) {
79+
isExpandable(node: FlatTreeNode) {
7780
return node.expandable;
7881
};
7982

83+
/** Get whether the node has children or not. */
84+
hasChild(index: number, node: FlatTreeNode) {
85+
return node.expandable;
86+
}
87+
8088
/** Get the children for the node. */
8189
getChildren(node: FileNode) {
8290
return observableOf(node.children);
8391
}
84-
85-
/** Get whether the node has children or not. */
86-
hasChild(index: number, node: TreeNode){
87-
return node.expandable;
88-
}
8992
}

src/lib/schematics/tree/files/__path__/__name@dasherize@if-flat__/example-data.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const files = [
1010
children: [
1111
{
1212
name: 'cdk',
13+
type: 'folder',
1314
children: [
1415
{ name: 'package.json', type: 'file' },
1516
{ name: 'BUILD.bazel', type: 'file' },

0 commit comments

Comments
 (0)