angular / angular/components

feature request: Simplify MatTree setup

Open
#11,446 2 comments 7 reactions 0 assignees View on GitHub
area: material/tree feature P3
Dominant language
TypeScript
Stars
25k
Forks
6.8k
Avg merge
1d 8h
Merged PRs (30d)
91

Description

#### Bug, feature request, or proposal:
feature request

#### What is the expected behavior?
To simlify matTree setup

#### What is the current behavior?
we use lots of codes to setup a flatten tree
```javascript
this.treeFlattener = new MatTreeFlattener(this.transformer, this.getLevel,
this.isExpandable, this.getChildren);
this.treeControl = new FlatTreeControl(this.getLevel, this.isExpandable);
this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);

this.database.dataChange.subscribe(data => {
this.dataSource.data = data;
});

getLevel = (node: TodoItemFlatNode) => node.level;

isExpandable = (node: TodoItemFlatNode) => node.expandable;

getChildren = (node: TodoItemNode): Observable => {
return ofObservable(node.children);
}

hasChild = (_: number, _nodeData: TodoItemFlatNode) => _nodeData.expandable;

hasNoContent = (_: number, _nodeData: TodoItemFlatNode) => _nodeData.item === '';
```

#### What are the steps to reproduce?
[official demo](https://run.stackblitz.com/api/angular/v1?file=app%2Ftree-checklist-example.ts)

#### What is the use-case or motivation for changing an existing behavior?
1. Convention over configuration
We should follow a convention rather than do too many configuration.
All tree datasource has the same property such as `children`, `level`, `expandable`
2. Proposal
```javascript
// to define a material tree interface
export interface MatTreeNode {
level: number;
expandable: boolean,
children: Array
}

// all tree nodes must implements tree data interface
export class MyTreeNode implements MatTreeNode {
// ... business logic
}

// only need to pass Array to setup tree component
this.treeDataSource = new MatTreeDataSource(Array);
```

#### Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular 6.0.0
Material 6.0.0
Typescript 2.7.2

#### Is there anything else we should know?

Contributor guide

Open the contributing guide

Research direction

Start with the official tree-checklist demo and its app/tree-checklist-example.ts entry point, then review the MatTreeFlattener, FlatTreeControl, and MatTreeFlatDataSource setup described in the issue. Done means agreeing on and implementing a simpler MatTree data-source API that removes the repeated callbacks while preserving the demonstrated tree behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.