Skip to content

fix(tree): allow use of FlatTreeControl's trackBy function in MatTreeFlatDataSource and MatTreeFlattener #19600

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 2 commits into from
Jun 29, 2020
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
10 changes: 5 additions & 5 deletions src/material/tree/data-source/flat-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {map, take} from 'rxjs/operators';
* }
* and the output flattened type is `F` with additional information.
*/
export class MatTreeFlattener<T, F> {
export class MatTreeFlattener<T, F, K = F> {

constructor(public transformFunction: (node: T, level: number) => F,
public getLevel: (node: F) => number,
Expand Down Expand Up @@ -97,7 +97,7 @@ export class MatTreeFlattener<T, F> {
* Expand flattened node with current expansion status.
* The returned list may have different length.
*/
expandFlattenedNodes(nodes: F[], treeControl: TreeControl<F>): F[] {
expandFlattenedNodes(nodes: F[], treeControl: TreeControl<F, K>): F[] {
let results: F[] = [];
let currentExpand: boolean[] = [];
currentExpand[0] = true;
Expand Down Expand Up @@ -126,7 +126,7 @@ export class MatTreeFlattener<T, F> {
* The nested tree nodes of type `T` are flattened through `MatTreeFlattener`, and converted
* to type `F` for `MatTree` to consume.
*/
export class MatTreeFlatDataSource<T, F> extends DataSource<F> {
export class MatTreeFlatDataSource<T, F, K = F> extends DataSource<F> {
_flattenedData = new BehaviorSubject<F[]>([]);

_expandedData = new BehaviorSubject<F[]>([]);
Expand All @@ -139,8 +139,8 @@ export class MatTreeFlatDataSource<T, F> extends DataSource<F> {
this._treeControl.dataNodes = this._flattenedData.value;
}

constructor(private _treeControl: FlatTreeControl<F>,
private _treeFlattener: MatTreeFlattener<T, F>,
constructor(private _treeControl: FlatTreeControl<F, K>,
private _treeFlattener: MatTreeFlattener<T, F, K>,
initialData: T[] = []) {
super();
this._data = new BehaviorSubject<T[]>(initialData);
Expand Down
8 changes: 4 additions & 4 deletions tools/public_api_guard/material/tree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ export declare class MatTree<T> extends CdkTree<T> {
static ɵfac: i0.ɵɵFactoryDef<MatTree<any>, never>;
}

export declare class MatTreeFlatDataSource<T, F> extends DataSource<F> {
export declare class MatTreeFlatDataSource<T, F, K = F> extends DataSource<F> {
_data: BehaviorSubject<T[]>;
_expandedData: BehaviorSubject<F[]>;
_flattenedData: BehaviorSubject<F[]>;
get data(): T[];
set data(value: T[]);
constructor(_treeControl: FlatTreeControl<F>, _treeFlattener: MatTreeFlattener<T, F>, initialData?: T[]);
constructor(_treeControl: FlatTreeControl<F, K>, _treeFlattener: MatTreeFlattener<T, F, K>, initialData?: T[]);
connect(collectionViewer: CollectionViewer): Observable<F[]>;
disconnect(): void;
}

export declare class MatTreeFlattener<T, F> {
export declare class MatTreeFlattener<T, F, K = F> {
getChildren: (node: T) => Observable<T[]> | T[] | undefined | null;
getLevel: (node: F) => number;
isExpandable: (node: F) => boolean;
transformFunction: (node: T, level: number) => F;
constructor(transformFunction: (node: T, level: number) => F, getLevel: (node: F) => number, isExpandable: (node: F) => boolean, getChildren: (node: T) => Observable<T[]> | T[] | undefined | null);
_flattenChildren(children: T[], level: number, resultNodes: F[], parentMap: boolean[]): void;
_flattenNode(node: T, level: number, resultNodes: F[], parentMap: boolean[]): F[];
expandFlattenedNodes(nodes: F[], treeControl: TreeControl<F>): F[];
expandFlattenedNodes(nodes: F[], treeControl: TreeControl<F, K>): F[];
flattenNodes(structuredData: T[]): F[];
}

Expand Down