Skip to content

Commit 2b38341

Browse files
committed
main - 9055ac5 docs(material/tree): update examples to use modern childrenAccessor API (#32751)
1 parent 8c30df6 commit 2b38341

24 files changed

Lines changed: 441 additions & 1032 deletions
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree</span> [<span class="hljs-attr">dataSource</span>]=<span class="hljs-string">&quot;dataSource&quot;</span> [<span class="hljs-attr">treeControl</span>]=<span class="hljs-string">&quot;treeControl&quot;</span>&gt;</span>
1+
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree</span> #<span class="hljs-attr">tree</span> [<span class="hljs-attr">dataSource</span>]=<span class="hljs-string">&quot;dataSource&quot;</span> [<span class="hljs-attr">childrenAccessor</span>]=<span class="hljs-string">&quot;childrenAccessor&quot;</span>&gt;</span>
22
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree-node</span> *<span class="hljs-attr">matTreeNodeDef</span>=<span class="hljs-string">&quot;let node&quot;</span> <span class="hljs-attr">matTreeNodePadding</span>&gt;</span>
33
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">matIconButton</span> <span class="hljs-attr">disabled</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
4-
{{node.item}}
4+
{{node.name}}
55
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-tree-node</span>&gt;</span>
66
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree-node</span> *<span class="hljs-attr">matTreeNodeDef</span>=<span class="hljs-string">&quot;let node; when: hasChild&quot;</span> <span class="hljs-attr">matTreeNodePadding</span> <span class="hljs-attr">matTreeNodeToggle</span>
7-
[<span class="hljs-attr">cdkTreeNodeTypeaheadLabel</span>]=<span class="hljs-string">&quot;node.item&quot;</span>&gt;</span>
8-
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">matIconButton</span>
9-
[<span class="hljs-attr">attr.aria-label</span>]=<span class="hljs-string">&quot;&#x27;Toggle &#x27; + node.item&quot;</span> <span class="hljs-attr">matTreeNodeToggle</span>&gt;</span>
7+
[<span class="hljs-attr">cdkTreeNodeTypeaheadLabel</span>]=<span class="hljs-string">&quot;node.name&quot;</span> (<span class="hljs-attr">expandedChange</span>)=<span class="hljs-string">&quot;onNodeExpanded(node, $event)&quot;</span>&gt;</span>
8+
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">matIconButton</span> [<span class="hljs-attr">attr.aria-label</span>]=<span class="hljs-string">&quot;&#x27;Toggle &#x27; + node.name&quot;</span> <span class="hljs-attr">matTreeNodeToggle</span>&gt;</span>
109
<span class="hljs-tag">&lt;<span class="hljs-name">mat-icon</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;mat-icon-rtl-mirror&quot;</span>&gt;</span>
11-
{{treeControl.isExpanded(node) ? &#x27;expand_more&#x27; : &#x27;chevron_right&#x27;}}
10+
{{tree.isExpanded(node) ? &#x27;expand_more&#x27; : &#x27;chevron_right&#x27;}}
1211
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-icon</span>&gt;</span>
1312
<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
14-
{{node.item}}
13+
{{node.name}}
1514
@if (node.isLoading()) {
16-
<span class="hljs-tag">&lt;<span class="hljs-name">mat-progress-bar</span>
17-
<span class="hljs-attr">mode</span>=<span class="hljs-string">&quot;indeterminate&quot;</span>
18-
<span class="hljs-attr">class</span>=<span class="hljs-string">&quot;example-tree-progress-bar&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">mat-progress-bar</span>&gt;</span>
15+
<span class="hljs-tag">&lt;<span class="hljs-name">mat-progress-bar</span> <span class="hljs-attr">mode</span>=<span class="hljs-string">&quot;indeterminate&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;example-tree-progress-bar&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">mat-progress-bar</span>&gt;</span>
1916
}
2017
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-tree-node</span>&gt;</span>
21-
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-tree</span>&gt;</span>
18+
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-tree</span>&gt;</span>

docs-content/examples-highlighted/material/tree/tree-dynamic/tree-dynamic-example-ts.html

Lines changed: 47 additions & 113 deletions
Large diffs are not rendered by default.

docs-content/examples-highlighted/material/tree/tree-flat-overview/tree-flat-overview-example-html.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree</span> [<span class="hljs-attr">dataSource</span>]=<span class="hljs-string">&quot;dataSource&quot;</span> [<span class="hljs-attr">treeControl</span>]=<span class="hljs-string">&quot;treeControl&quot;</span>&gt;</span>
1+
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree</span> #<span class="hljs-attr">tree</span> [<span class="hljs-attr">dataSource</span>]=<span class="hljs-string">&quot;dataSource&quot;</span> [<span class="hljs-attr">childrenAccessor</span>]=<span class="hljs-string">&quot;childrenAccessor&quot;</span>&gt;</span>
22
<span class="hljs-comment">&lt;!-- This is the tree node template for leaf nodes --&gt;</span>
33
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree-node</span> *<span class="hljs-attr">matTreeNodeDef</span>=<span class="hljs-string">&quot;let node&quot;</span> <span class="hljs-attr">matTreeNodePadding</span>&gt;</span>
44
<span class="hljs-comment">&lt;!-- use a disabled button to provide padding for tree leaf --&gt;</span>
@@ -11,7 +11,7 @@
1111
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">matIconButton</span> <span class="hljs-attr">matTreeNodeToggle</span>
1212
[<span class="hljs-attr">attr.aria-label</span>]=<span class="hljs-string">&quot;&#x27;Toggle &#x27; + node.name&quot;</span>&gt;</span>
1313
<span class="hljs-tag">&lt;<span class="hljs-name">mat-icon</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;mat-icon-rtl-mirror&quot;</span>&gt;</span>
14-
{{treeControl.isExpanded(node) ? &#x27;expand_more&#x27; : &#x27;chevron_right&#x27;}}
14+
{{tree.isExpanded(node) ? &#x27;expand_more&#x27; : &#x27;chevron_right&#x27;}}
1515
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-icon</span>&gt;</span>
1616
<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
1717
{{node.name}}

docs-content/examples-highlighted/material/tree/tree-flat-overview/tree-flat-overview-example-ts.html

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
<span class="hljs-keyword">import</span> {<span class="hljs-title class_">FlatTreeControl</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;@angular/cdk/tree&#x27;</span>;
21
<span class="hljs-keyword">import</span> {<span class="hljs-title class_">ChangeDetectionStrategy</span>, <span class="hljs-title class_">Component</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;@angular/core&#x27;</span>;
3-
<span class="hljs-keyword">import</span> {<span class="hljs-title class_">MatTreeFlatDataSource</span>, <span class="hljs-title class_">MatTreeFlattener</span>, <span class="hljs-title class_">MatTreeModule</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;@angular/material/tree&#x27;</span>;
2+
<span class="hljs-keyword">import</span> {<span class="hljs-title class_">MatTreeModule</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;@angular/material/tree&#x27;</span>;
43
<span class="hljs-keyword">import</span> {<span class="hljs-title class_">MatIconModule</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;@angular/material/icon&#x27;</span>;
54
<span class="hljs-keyword">import</span> {<span class="hljs-title class_">MatButtonModule</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;@angular/material/button&#x27;</span>;
65

@@ -13,13 +12,6 @@
1312
<span class="hljs-attr">children</span>?: <span class="hljs-title class_">FoodNode</span>[];
1413
}
1514

16-
<span class="hljs-comment">/** Flat node with expandable and level information */</span>
17-
<span class="hljs-keyword">interface</span> <span class="hljs-title class_">ExampleFlatNode</span> {
18-
<span class="hljs-attr">expandable</span>: <span class="hljs-built_in">boolean</span>;
19-
<span class="hljs-attr">name</span>: <span class="hljs-built_in">string</span>;
20-
<span class="hljs-attr">level</span>: <span class="hljs-built_in">number</span>;
21-
}
22-
2315
<span class="hljs-comment">/**
2416
* <span class="hljs-doctag">@title</span> Tree with flat nodes
2517
*/</span>
@@ -30,33 +22,11 @@
3022
<span class="hljs-attr">changeDetection</span>: <span class="hljs-title class_">ChangeDetectionStrategy</span>.<span class="hljs-property">OnPush</span>,
3123
})
3224
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">TreeFlatOverviewExample</span> {
33-
<span class="hljs-keyword">private</span> _transformer = <span class="hljs-function">(<span class="hljs-params"><span class="hljs-attr">node</span>: <span class="hljs-title class_">FoodNode</span>, <span class="hljs-attr">level</span>: <span class="hljs-built_in">number</span></span>) =&gt;</span> {
34-
<span class="hljs-keyword">return</span> {
35-
<span class="hljs-attr">expandable</span>: !!node.<span class="hljs-property">children</span> &amp;&amp; node.<span class="hljs-property">children</span>.<span class="hljs-property">length</span> &gt; <span class="hljs-number">0</span>,
36-
<span class="hljs-attr">name</span>: node.<span class="hljs-property">name</span>,
37-
<span class="hljs-attr">level</span>: level,
38-
};
39-
};
40-
41-
treeControl = <span class="hljs-keyword">new</span> <span class="hljs-title class_">FlatTreeControl</span>&lt;<span class="hljs-title class_">ExampleFlatNode</span>&gt;(
42-
<span class="hljs-function"><span class="hljs-params">node</span> =&gt;</span> node.<span class="hljs-property">level</span>,
43-
<span class="hljs-function"><span class="hljs-params">node</span> =&gt;</span> node.<span class="hljs-property">expandable</span>,
44-
);
45-
46-
treeFlattener = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MatTreeFlattener</span>(
47-
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_transformer</span>,
48-
<span class="hljs-function"><span class="hljs-params">node</span> =&gt;</span> node.<span class="hljs-property">level</span>,
49-
<span class="hljs-function"><span class="hljs-params">node</span> =&gt;</span> node.<span class="hljs-property">expandable</span>,
50-
<span class="hljs-function"><span class="hljs-params">node</span> =&gt;</span> node.<span class="hljs-property">children</span>,
51-
);
52-
53-
dataSource = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MatTreeFlatDataSource</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">treeControl</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">treeFlattener</span>);
25+
dataSource = <span class="hljs-variable constant_">EXAMPLE_DATA</span>;
5426

55-
<span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) {
56-
<span class="hljs-variable language_">this</span>.<span class="hljs-property">dataSource</span>.<span class="hljs-property">data</span> = <span class="hljs-variable constant_">EXAMPLE_DATA</span>;
57-
}
27+
childrenAccessor = <span class="hljs-function">(<span class="hljs-params"><span class="hljs-attr">node</span>: <span class="hljs-title class_">FoodNode</span></span>) =&gt;</span> node.<span class="hljs-property">children</span> ?? [];
5828

59-
hasChild = <span class="hljs-function">(<span class="hljs-params"><span class="hljs-attr">_</span>: <span class="hljs-built_in">number</span>, <span class="hljs-attr">node</span>: <span class="hljs-title class_">ExampleFlatNode</span></span>) =&gt;</span> node.<span class="hljs-property">expandable</span>;
29+
hasChild = <span class="hljs-function">(<span class="hljs-params"><span class="hljs-attr">_</span>: <span class="hljs-built_in">number</span>, <span class="hljs-attr">node</span>: <span class="hljs-title class_">FoodNode</span></span>) =&gt;</span> !!node.<span class="hljs-property">children</span> &amp;&amp; node.<span class="hljs-property">children</span>.<span class="hljs-property">length</span> &gt; <span class="hljs-number">0</span>;
6030
}
6131

6232
<span class="hljs-keyword">const</span> <span class="hljs-attr">EXAMPLE_DATA</span>: <span class="hljs-title class_">FoodNode</span>[] = [
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree</span> [<span class="hljs-attr">dataSource</span>]=<span class="hljs-string">&quot;dataSource&quot;</span> [<span class="hljs-attr">treeControl</span>]=<span class="hljs-string">&quot;treeControl&quot;</span>&gt;</span>
1+
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree</span> #<span class="hljs-attr">tree</span> [<span class="hljs-attr">dataSource</span>]=<span class="hljs-string">&quot;dataSource&quot;</span> [<span class="hljs-attr">childrenAccessor</span>]=<span class="hljs-string">&quot;childrenAccessor&quot;</span>&gt;</span>
22
<span class="hljs-comment">&lt;!-- This is the tree node template for leaf nodes --&gt;</span>
33
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree-node</span> *<span class="hljs-attr">matTreeNodeDef</span>=<span class="hljs-string">&quot;let node&quot;</span> <span class="hljs-attr">matTreeNodePadding</span>&gt;</span>
44
<span class="hljs-comment">&lt;!-- use a disabled button to provide padding for tree leaf --&gt;</span>
@@ -7,13 +7,12 @@
77
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-tree-node</span>&gt;</span>
88
<span class="hljs-comment">&lt;!-- This is the tree node template for expandable nodes --&gt;</span>
99
<span class="hljs-tag">&lt;<span class="hljs-name">mat-tree-node</span> *<span class="hljs-attr">matTreeNodeDef</span>=<span class="hljs-string">&quot;let node;when: hasChild&quot;</span> <span class="hljs-attr">matTreeNodePadding</span> <span class="hljs-attr">matTreeNodeToggle</span>
10-
[<span class="hljs-attr">cdkTreeNodeTypeaheadLabel</span>]=<span class="hljs-string">&quot;node.name&quot;</span>&gt;</span>
11-
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">matIconButton</span> <span class="hljs-attr">matTreeNodeToggle</span>
12-
[<span class="hljs-attr">attr.aria-label</span>]=<span class="hljs-string">&quot;&#x27;Toggle &#x27; + node.name&quot;</span>&gt;</span>
10+
[<span class="hljs-attr">cdkTreeNodeTypeaheadLabel</span>]=<span class="hljs-string">&quot;node.name&quot;</span>&gt;</span>
11+
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">matIconButton</span> <span class="hljs-attr">matTreeNodeToggle</span> [<span class="hljs-attr">attr.aria-label</span>]=<span class="hljs-string">&quot;&#x27;Toggle &#x27; + node.name&quot;</span>&gt;</span>
1312
<span class="hljs-tag">&lt;<span class="hljs-name">mat-icon</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;mat-icon-rtl-mirror&quot;</span>&gt;</span>
14-
{{treeControl.isExpanded(node) ? &#x27;expand_more&#x27; : &#x27;chevron_right&#x27;}}
13+
{{tree.isExpanded(node) ? &#x27;expand_more&#x27; : &#x27;chevron_right&#x27;}}
1514
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-icon</span>&gt;</span>
1615
<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
1716
{{node.name}}
1817
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-tree-node</span>&gt;</span>
19-
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-tree</span>&gt;</span>
18+
<span class="hljs-tag">&lt;/<span class="hljs-name">mat-tree</span>&gt;</span>

0 commit comments

Comments
 (0)