Skip to content

Commit 94df9fb

Browse files
committed
fix(material/tree): not rendering initial data from flat data source
The `MatTreeFlatDataSource` has an `initialData` parameter which doesn't work, because it only assigns the data to one of the three streams used to render the tree. These changes also make some underscored properties private. Fixes #22282.
1 parent a2c4b76 commit 94df9fb

File tree

4 files changed

+10
-14
lines changed

4 files changed

+10
-14
lines changed

src/material/tree/data-source/flat-data-source.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,31 +128,33 @@ export class MatTreeFlattener<T, F, K = F> {
128128
*/
129129
export class MatTreeFlatDataSource<T, F, K = F> extends DataSource<F> {
130130
_flattenedData = new BehaviorSubject<F[]>([]);
131-
132131
_expandedData = new BehaviorSubject<F[]>([]);
133132

134-
_data: BehaviorSubject<T[]>;
135133
get data() { return this._data.value; }
136134
set data(value: T[]) {
137135
this._data.next(value);
138136
this._flattenedData.next(this._treeFlattener.flattenNodes(this.data));
139137
this._treeControl.dataNodes = this._flattenedData.value;
140138
}
139+
private _data = new BehaviorSubject<T[]>([]);
141140

142141
constructor(private _treeControl: FlatTreeControl<F, K>,
143142
private _treeFlattener: MatTreeFlattener<T, F, K>,
144-
initialData: T[] = []) {
143+
initialData?: T[]) {
145144
super();
146-
this._data = new BehaviorSubject<T[]>(initialData);
145+
146+
if (initialData) {
147+
// Assign the data through the constructor to ensure that all of the logic is executed.
148+
this.data = initialData;
149+
}
147150
}
148151

149152
connect(collectionViewer: CollectionViewer): Observable<F[]> {
150-
const changes = [
153+
return merge(
151154
collectionViewer.viewChange,
152155
this._treeControl.expansionModel.changed,
153156
this._flattenedData
154-
];
155-
return merge(...changes).pipe(map(() => {
157+
).pipe(map(() => {
156158
this._expandedData.next(
157159
this._treeFlattener.expandFlattenedNodes(this._flattenedData.value, this._treeControl));
158160
return this._expandedData.value;

src/material/tree/data-source/nested-data-source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import {map} from 'rxjs/operators';
1818
* or collapse. The expansion/collapsion will be handled by TreeControl and each non-leaf node.
1919
*/
2020
export class MatTreeNestedDataSource<T> extends DataSource<T> {
21-
_data = new BehaviorSubject<T[]>([]);
2221

2322
/**
2423
* Data for the nested tree
2524
*/
2625
get data() { return this._data.value; }
2726
set data(value: T[]) { this._data.next(value); }
27+
private _data = new BehaviorSubject<T[]>([]);
2828

2929
connect(collectionViewer: CollectionViewer): Observable<T[]> {
3030
return merge(...[collectionViewer.viewChange, this._data])

src/material/tree/tree.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,10 +785,6 @@ class MatTreeWithNullOrUndefinedChild {
785785

786786
dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener, TREE_DATA);
787787

788-
constructor() {
789-
this.dataSource.data = TREE_DATA;
790-
}
791-
792788
hasChild = (_: number, node: ExampleFlatNode) => node.expandable;
793789
}
794790

tools/public_api_guard/material/tree.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export declare class MatTree<T, K = T> extends CdkTree<T, K> {
2424
}
2525

2626
export declare class MatTreeFlatDataSource<T, F, K = F> extends DataSource<F> {
27-
_data: BehaviorSubject<T[]>;
2827
_expandedData: BehaviorSubject<F[]>;
2928
_flattenedData: BehaviorSubject<F[]>;
3029
get data(): T[];
@@ -53,7 +52,6 @@ export declare class MatTreeModule {
5352
}
5453

5554
export declare class MatTreeNestedDataSource<T> extends DataSource<T> {
56-
_data: BehaviorSubject<T[]>;
5755
get data(): T[];
5856
set data(value: T[]);
5957
connect(collectionViewer: CollectionViewer): Observable<T[]>;

0 commit comments

Comments
 (0)