Skip to content

Commit c305f8c

Browse files
committed
feat(cdk/tree): move demos back to their own dirs
1 parent 8ecdb74 commit c305f8c

File tree

9 files changed

+251
-3
lines changed

9 files changed

+251
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.example-tree-node {
2+
display: flex;
3+
align-items: center;
4+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<cdk-tree [dataSource]="dataSource" [levelAccessor]="levelAccessor">
2+
<!-- This is the tree node template for leaf nodes -->
3+
<cdk-tree-node *cdkTreeNodeDef="let node" cdkTreeNodePadding
4+
[style.display]="shouldRender(node) ? 'flex' : 'none'"
5+
class="example-tree-node">
6+
<!-- use a disabled button to provide padding for tree leaf -->
7+
<button mat-icon-button disabled></button>
8+
{{node.name}}
9+
</cdk-tree-node>
10+
<!-- This is the tree node template for expandable nodes -->
11+
<cdk-tree-node *cdkTreeNodeDef="let node; when: hasChild" cdkTreeNodePadding
12+
[style.display]="shouldRender(node) ? 'flex' : 'none'"
13+
[isExpandable]="node.expandable"
14+
class="example-tree-node">
15+
<button mat-icon-button cdkTreeNodeToggle
16+
[attr.aria-label]="'Toggle ' + node.name"
17+
(click)="node.isExpanded = !node.isExpanded"
18+
[style.visibility]="node.expandable ? 'visible' : 'hidden'">
19+
<mat-icon class="mat-icon-rtl-mirror">
20+
{{node.isExpanded ? 'expand_more' : 'chevron_right'}}
21+
</mat-icon>
22+
</button>
23+
{{node.name}}
24+
</cdk-tree-node>
25+
</cdk-tree>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {ArrayDataSource} from '@angular/cdk/collections';
2+
import {Component} from '@angular/core';
3+
import {FlatFoodNode, FLAT_DATA} from '../tree-data';
4+
5+
/**
6+
* @title Tree with flat nodes
7+
*/
8+
@Component({
9+
selector: 'cdk-tree-flat-level-accessor-example',
10+
templateUrl: 'cdk-tree-flat-level-accessor-example.html',
11+
styleUrls: ['cdk-tree-flat-level-accessor-example.css'],
12+
})
13+
export class CdkTreeFlatLevelAccessorExample {
14+
levelAccessor = (dataNode: FlatFoodNode) => dataNode.level;
15+
16+
dataSource = new ArrayDataSource(FLAT_DATA);
17+
18+
hasChild = (_: number, node: FlatFoodNode) => node.expandable;
19+
20+
getParentNode(node: FlatFoodNode) {
21+
const nodeIndex = FLAT_DATA.indexOf(node);
22+
23+
for (let i = nodeIndex - 1; i >= 0; i--) {
24+
if (FLAT_DATA[i].level === node.level - 1) {
25+
return FLAT_DATA[i];
26+
}
27+
}
28+
29+
return null;
30+
}
31+
32+
shouldRender(node: FlatFoodNode) {
33+
let parent = this.getParentNode(node);
34+
while (parent) {
35+
if (!parent.isExpanded) {
36+
return false;
37+
}
38+
parent = this.getParentNode(parent);
39+
}
40+
return true;
41+
}
42+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.example-tree-node {
2+
display: flex;
3+
align-items: center;
4+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<cdk-tree [dataSource]="dataSource" [levelAccessor]="levelAccessor">
2+
<!-- This is the tree node template for leaf nodes -->
3+
<cdk-nested-tree-node *cdkTreeNodeDef="let node" class="example-tree-node">
4+
<!-- use a disabled button to provide padding for tree leaf -->
5+
<button mat-icon-button disabled></button>
6+
{{node.name}}
7+
</cdk-nested-tree-node>
8+
<!-- This is the tree node template for expandable nodes -->
9+
<cdk-nested-tree-node *cdkTreeNodeDef="let node; when: hasChild"
10+
[isExpandable]="node.expandable"
11+
class="example-tree-node">
12+
<button mat-icon-button cdkTreeNodeToggle
13+
[attr.aria-label]="'Toggle ' + node.name"
14+
(click)="node.isExpanded = !node.isExpanded"
15+
[style.visibility]="node.expandable ? 'visible' : 'hidden'">
16+
<mat-icon class="mat-icon-rtl-mirror">
17+
{{node.isExpanded ? 'expand_more' : 'chevron_right'}}
18+
</mat-icon>
19+
</button>
20+
{{node.name}}
21+
</cdk-nested-tree-node>
22+
</cdk-tree>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {ArrayDataSource} from '@angular/cdk/collections';
2+
import {Component} from '@angular/core';
3+
import {FlatFoodNode, FLAT_DATA} from '../tree-data';
4+
5+
/**
6+
* @title Tree with nested nodes
7+
*/
8+
@Component({
9+
selector: 'cdk-tree-nested-level-accessor-example',
10+
templateUrl: 'cdk-tree-nested-level-accessor-example.html',
11+
styleUrls: ['cdk-tree-nested-level-accessor-example.css'],
12+
})
13+
export class CdkTreeNestedLevelAccessorExample {
14+
levelAccessor = (dataNode: FlatFoodNode) => dataNode.level;
15+
16+
dataSource = new ArrayDataSource(FLAT_DATA);
17+
18+
hasChild = (_: number, node: FlatFoodNode) => node.expandable;
19+
20+
getParentNode(node: FlatFoodNode) {
21+
const nodeIndex = FLAT_DATA.indexOf(node);
22+
23+
for (let i = nodeIndex - 1; i >= 0; i--) {
24+
if (FLAT_DATA[i].level === node.level - 1) {
25+
return FLAT_DATA[i];
26+
}
27+
}
28+
29+
return null;
30+
}
31+
32+
shouldRender(node: FlatFoodNode) {
33+
let parent = this.getParentNode(node);
34+
while (parent) {
35+
if (!parent.isExpanded) {
36+
return false;
37+
}
38+
parent = this.getParentNode(parent);
39+
}
40+
return true;
41+
}
42+
}

src/components-examples/cdk/tree/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@ import {CdkTreeModule} from '@angular/cdk/tree';
22
import {NgModule} from '@angular/core';
33
import {MatLegacyButtonModule} from '@angular/material/legacy-button';
44
import {MatIconModule} from '@angular/material/icon';
5+
import {CdkTreeFlatLevelAccessorExample} from './cdk-tree-flat-level-accessor/cdk-tree-flat-level-accessor-example';
56
import {CdkTreeFlatExample} from './cdk-tree-flat/cdk-tree-flat-example';
7+
import {CdkTreeNestedLevelAccessorExample} from './cdk-tree-nested-level-accessor/cdk-tree-nested-level-accessor-example';
68
import {CdkTreeNestedExample} from './cdk-tree-nested/cdk-tree-nested-example';
7-
import {CdkTreeFlatLevelAccessorExample} from './cdk-tree-redesign/cdk-tree-flat-level-accessor-example';
89

9-
export {CdkTreeFlatExample, CdkTreeNestedExample, CdkTreeFlatLevelAccessorExample};
10+
export {
11+
CdkTreeFlatExample,
12+
CdkTreeNestedExample,
13+
CdkTreeFlatLevelAccessorExample,
14+
CdkTreeNestedLevelAccessorExample,
15+
};
1016

11-
const EXAMPLES = [CdkTreeFlatExample, CdkTreeNestedExample, CdkTreeFlatLevelAccessorExample];
17+
const EXAMPLES = [
18+
CdkTreeFlatExample,
19+
CdkTreeNestedExample,
20+
CdkTreeFlatLevelAccessorExample,
21+
CdkTreeNestedLevelAccessorExample,
22+
];
1223

1324
@NgModule({
1425
imports: [CdkTreeModule, MatLegacyButtonModule, MatIconModule],
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/** Flat node with expandable and level information */
2+
export interface FlatFoodNode {
3+
expandable: boolean;
4+
name: string;
5+
level: number;
6+
isExpanded?: boolean;
7+
}
8+
9+
export const FLAT_DATA: FlatFoodNode[] = [
10+
{
11+
name: 'Fruit',
12+
expandable: true,
13+
level: 0,
14+
},
15+
{
16+
name: 'Apple',
17+
expandable: false,
18+
level: 1,
19+
},
20+
{
21+
name: 'Banana',
22+
expandable: false,
23+
level: 1,
24+
},
25+
{
26+
name: 'Fruit loops',
27+
expandable: false,
28+
level: 1,
29+
},
30+
{
31+
name: 'Vegetables',
32+
expandable: true,
33+
level: 0,
34+
},
35+
{
36+
name: 'Green',
37+
expandable: true,
38+
level: 1,
39+
},
40+
{
41+
name: 'Broccoli',
42+
expandable: false,
43+
level: 2,
44+
},
45+
{
46+
name: 'Brussels sprouts',
47+
expandable: false,
48+
level: 2,
49+
},
50+
{
51+
name: 'Orange',
52+
expandable: true,
53+
level: 1,
54+
},
55+
{
56+
name: 'Pumpkins',
57+
expandable: false,
58+
level: 2,
59+
},
60+
{
61+
name: 'Carrots',
62+
expandable: false,
63+
level: 2,
64+
},
65+
];
66+
67+
/**
68+
* Food data with nested structure.
69+
* Each node has a name and an optional list of children.
70+
*/
71+
export interface NestedFoodNode {
72+
name: string;
73+
children?: NestedFoodNode[];
74+
}
75+
76+
export const NESTED_DATA: NestedFoodNode[] = [
77+
{
78+
name: 'Fruit',
79+
children: [{name: 'Apple'}, {name: 'Banana'}, {name: 'Fruit loops'}],
80+
},
81+
{
82+
name: 'Vegetables',
83+
children: [
84+
{
85+
name: 'Green',
86+
children: [{name: 'Broccoli'}, {name: 'Brussels sprouts'}],
87+
},
88+
{
89+
name: 'Orange',
90+
children: [{name: 'Pumpkins'}, {name: 'Carrots'}],
91+
},
92+
],
93+
},
94+
];

src/dev-app/tree/tree-demo.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
<mat-expansion-panel-header>CDK Nested tree</mat-expansion-panel-header>
2020
<cdk-tree-nested-example></cdk-tree-nested-example>
2121
</mat-expansion-panel>
22+
<mat-expansion-panel>
23+
<mat-expansion-panel-header>CDK Nested tree (levelAccessor)</mat-expansion-panel-header>
24+
<cdk-tree-nested-level-accessor-example></cdk-tree-nested-level-accessor-example>
25+
</mat-expansion-panel>
2226
<mat-expansion-panel>
2327
<mat-expansion-panel-header>Todo list tree</mat-expansion-panel-header>
2428
<tree-checklist-example></tree-checklist-example>

0 commit comments

Comments
 (0)