forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module:tree-view): add tree-view component
close NG-ZORRO#5976, close NG-ZORRO#5809, close NG-ZORRO#5739, close NG-ZORRO#5736, close NG-ZORRO#5519, close NG-ZORRO#5446, close NG-ZORRO#5152, close NG-ZORRO#4694, close NG-ZORRO#4472, close NG-ZORRO#3832, close NG-ZORRO#2785, close NG-ZORRO#2744
- Loading branch information
Showing
48 changed files
with
2,430 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
*/ | ||
|
||
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core'; | ||
|
||
import { BooleanInput } from 'ng-zorro-antd/core/types'; | ||
import { InputBoolean } from 'ng-zorro-antd/core/util'; | ||
|
||
@Component({ | ||
selector: 'nz-tree-node-checkbox', | ||
template: ` | ||
<span class="ant-tree-checkbox-inner"></span> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
preserveWhitespaces: false, | ||
host: { | ||
class: 'ant-tree-checkbox', | ||
'[class.ant-tree-checkbox-checked]': `nzChecked`, | ||
'[class.ant-tree-checkbox-indeterminate]': `nzIndeterminate`, | ||
'[class.ant-tree-checkbox-disabled]': `nzDisabled`, | ||
'(click)': 'onClick($event)' | ||
} | ||
}) | ||
export class NzTreeNodeCheckboxComponent { | ||
static ngAcceptInputType_nzDisabled: BooleanInput; | ||
|
||
@Input() nzChecked?: boolean; | ||
@Input() nzIndeterminate?: boolean; | ||
@Input() @InputBoolean() nzDisabled?: boolean; | ||
@Output() readonly nzClick = new EventEmitter<MouseEvent>(); | ||
|
||
onClick(e: MouseEvent): void { | ||
if (!this.nzDisabled) { | ||
this.nzClick.emit(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
*/ | ||
|
||
import { CollectionViewer, DataSource } from '@angular/cdk/collections'; | ||
import { FlatTreeControl, TreeControl } from '@angular/cdk/tree'; | ||
import { BehaviorSubject, merge, Observable } from 'rxjs'; | ||
import { map, take } from 'rxjs/operators'; | ||
|
||
export class NzTreeFlattener<T, F, K = F> { | ||
constructor( | ||
public transformFunction: (node: T, level: number) => F, | ||
public getLevel: (node: F) => number, | ||
public isExpandable: (node: F) => boolean, | ||
public getChildren: (node: T) => Observable<T[]> | T[] | undefined | null | ||
) {} | ||
|
||
private flattenNode(node: T, level: number, resultNodes: F[], parentMap: boolean[]): F[] { | ||
const flatNode = this.transformFunction(node, level); | ||
resultNodes.push(flatNode); | ||
|
||
if (this.isExpandable(flatNode)) { | ||
const childrenNodes = this.getChildren(node); | ||
if (childrenNodes) { | ||
if (Array.isArray(childrenNodes)) { | ||
this.flattenChildren(childrenNodes, level, resultNodes, parentMap); | ||
} else { | ||
childrenNodes.pipe(take(1)).subscribe(children => { | ||
this.flattenChildren(children, level, resultNodes, parentMap); | ||
}); | ||
} | ||
} | ||
} | ||
return resultNodes; | ||
} | ||
|
||
private flattenChildren(children: T[], level: number, resultNodes: F[], parentMap: boolean[]): void { | ||
children.forEach((child, index) => { | ||
const childParentMap: boolean[] = parentMap.slice(); | ||
childParentMap.push(index !== children.length - 1); | ||
this.flattenNode(child, level + 1, resultNodes, childParentMap); | ||
}); | ||
} | ||
|
||
/** | ||
* Flatten a list of node type T to flattened version of node F. | ||
* Please note that type T may be nested, and the length of `structuredData` may be different | ||
* from that of returned list `F[]`. | ||
*/ | ||
flattenNodes(structuredData: T[]): F[] { | ||
const resultNodes: F[] = []; | ||
structuredData.forEach(node => this.flattenNode(node, 0, resultNodes, [])); | ||
return resultNodes; | ||
} | ||
|
||
/** | ||
* Expand flattened node with current expansion status. | ||
* The returned list may have different length. | ||
*/ | ||
expandFlattenedNodes(nodes: F[], treeControl: TreeControl<F, K>): F[] { | ||
const results: F[] = []; | ||
const currentExpand: boolean[] = []; | ||
currentExpand[0] = true; | ||
|
||
nodes.forEach(node => { | ||
let expand = true; | ||
for (let i = 0; i <= this.getLevel(node); i++) { | ||
expand = expand && currentExpand[i]; | ||
} | ||
if (expand) { | ||
results.push(node); | ||
} | ||
if (this.isExpandable(node)) { | ||
currentExpand[this.getLevel(node) + 1] = treeControl.isExpanded(node); | ||
} | ||
}); | ||
return results; | ||
} | ||
} | ||
|
||
export class NzTreeFlatDataSource<T, F, K = F> extends DataSource<F> { | ||
_flattenedData = new BehaviorSubject<F[]>([]); | ||
|
||
_expandedData = new BehaviorSubject<F[]>([]); | ||
|
||
_data: BehaviorSubject<T[]>; | ||
|
||
constructor(private _treeControl: FlatTreeControl<F, K>, private _treeFlattener: NzTreeFlattener<T, F, K>, initialData: T[] = []) { | ||
super(); | ||
this._data = new BehaviorSubject<T[]>(initialData); | ||
} | ||
|
||
setData(value: T[]): void { | ||
this._data.next(value); | ||
this._flattenedData.next(this._treeFlattener.flattenNodes(this.getData())); | ||
this._treeControl.dataNodes = this._flattenedData.value; | ||
} | ||
|
||
getData(): T[] { | ||
return this._data.getValue(); | ||
} | ||
|
||
connect(collectionViewer: CollectionViewer): Observable<F[]> { | ||
const changes = [collectionViewer.viewChange, this._treeControl.expansionModel.changed, this._flattenedData]; | ||
return merge(...changes).pipe( | ||
map(() => { | ||
this._expandedData.next(this._treeFlattener.expandFlattenedNodes(this._flattenedData.value, this._treeControl)); | ||
return this._expandedData.value; | ||
}) | ||
); | ||
} | ||
|
||
disconnect(): void { | ||
// no op | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
order: 0 | ||
title: | ||
zh-CN: 基本 | ||
en-US: basic | ||
--- | ||
|
||
## zh-CN | ||
|
||
最简单的用法,选中,禁用,展开等功能。 | ||
|
||
## en-US | ||
|
||
The most basic usage including select, disable and expand features. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { SelectionModel } from '@angular/cdk/collections'; | ||
import { FlatTreeControl } from '@angular/cdk/tree'; | ||
import { Component } from '@angular/core'; | ||
|
||
import { NzTreeFlatDataSource, NzTreeFlattener } from 'ng-zorro-antd/tree-view'; | ||
|
||
interface TreeNode { | ||
name: string; | ||
disabled?: boolean; | ||
children?: TreeNode[]; | ||
} | ||
|
||
const TREE_DATA: TreeNode[] = [ | ||
{ | ||
name: 'parent 1', | ||
children: [ | ||
{ | ||
name: 'parent 1-0', | ||
disabled: true, | ||
children: [{ name: 'leaf' }, { name: 'leaf' }] | ||
}, | ||
{ | ||
name: 'parent 1-1', | ||
children: [{ name: 'leaf' }] | ||
} | ||
] | ||
} | ||
]; | ||
|
||
interface FlatNode { | ||
expandable: boolean; | ||
name: string; | ||
level: number; | ||
disabled: boolean; | ||
} | ||
|
||
@Component({ | ||
selector: 'nz-demo-tree-view-basic', | ||
template: ` | ||
<nz-tree-view [nzTreeControl]="treeControl" [nzDataSource]="dataSource"> | ||
<nz-tree-node *nzTreeNodeDef="let node" nzTreeNodePadding> | ||
<nz-tree-node-toggle nzTreeNodeNoopToggle></nz-tree-node-toggle> | ||
<nz-tree-node-option | ||
[nzDisabled]="node.disabled" | ||
[nzSelected]="selectListSelection.isSelected(node)" | ||
(nzClick)="selectListSelection.toggle(node)" | ||
> | ||
{{ node.name }} | ||
</nz-tree-node-option> | ||
</nz-tree-node> | ||
<nz-tree-node *nzTreeNodeDef="let node; when: hasChild" nzTreeNodePadding> | ||
<nz-tree-node-toggle> | ||
<i nz-icon nzType="caret-down" nzTreeNodeToggleRotateIcon></i> | ||
</nz-tree-node-toggle> | ||
<nz-tree-node-option | ||
[nzDisabled]="node.disabled" | ||
[nzSelected]="selectListSelection.isSelected(node)" | ||
(nzClick)="selectListSelection.toggle(node)" | ||
> | ||
{{ node.name }} | ||
</nz-tree-node-option> | ||
</nz-tree-node> | ||
</nz-tree-view> | ||
` | ||
}) | ||
export class NzDemoTreeViewBasicComponent { | ||
private transformer = (node: TreeNode, level: number) => { | ||
return { | ||
expandable: !!node.children && node.children.length > 0, | ||
name: node.name, | ||
level: level, | ||
disabled: !!node.disabled | ||
}; | ||
}; | ||
selectListSelection = new SelectionModel<FlatNode>(true); | ||
|
||
treeControl = new FlatTreeControl<FlatNode>( | ||
node => node.level, | ||
node => node.expandable | ||
); | ||
|
||
treeFlattener = new NzTreeFlattener( | ||
this.transformer, | ||
node => node.level, | ||
node => node.expandable, | ||
node => node.children | ||
); | ||
|
||
dataSource = new NzTreeFlatDataSource(this.treeControl, this.treeFlattener); | ||
|
||
constructor() { | ||
this.dataSource.setData(TREE_DATA); | ||
this.treeControl.expandAll(); | ||
} | ||
|
||
hasChild = (_: number, node: FlatNode) => node.expandable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
order: 1 | ||
title: | ||
zh-CN: 选择框 | ||
en-US: checkbox | ||
--- | ||
|
||
## zh-CN | ||
|
||
带选择框的树。 | ||
|
||
## en-US | ||
|
||
Tree with checkboxes. |
Oops, something went wrong.