8000 feat(tree): support default selected keys prop · atinc/ngx-tethys@02a0c03 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 02a0c03

Browse files
committed
feat(tree): support default selected keys prop
1 parent 2bd093e commit 02a0c03

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/tree/test/tree.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ describe('ThyTreeComponent', () => {
5454
expect(treeElement.querySelector('.thy-tree-expand-icon').classList).toContain('thy-icon-plus-square');
5555
});
5656

57+
it('test set selectedKeys correctly', () => {
58+
expect(treeComponent.getSelectedNodes().length).toEqual(1);
59+
expect(treeComponent.getSelectedNode().title).toEqual('未分配部门');
60+
});
61+
5762
it(`test public function 'getRootNodes()`, () => {
5863
expect(treeComponent.getRootNodes().length).toEqual(2);
5964
});
@@ -66,7 +71,6 @@ describe('ThyTreeComponent', () => {
6671
});
6772

6873
it(`test public function 'getSelectedNodes()`, () => {
69-
expect(treeComponent.getSelectedNodes().length).toEqual(0);
7074
treeComponent.selectTreeNode(treeComponent.getRootNodes()[1]);
7175
fixture.detectChanges();
7276
expect(treeComponent.getSelectedNodes().length).toEqual(1);
@@ -158,6 +162,7 @@ describe('ThyTreeComponent', () => {
158162
[thyDraggable]="options.draggable"
159163
8000 [thyCheckable]="options.checkable"
160164
[thyMultiple]="options.multiple"
165+
[thySelectedKeys]="['000000000000000000000000']"
161166
[thyShowExpand]="true"
162167
[thyBeforeDragDrop]="beforeDragDrop"
163168
(thyOnDragDrop)="onEvent()"

src/tree/tree-node.component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ export class ThyTreeNodeComponent implements OnDestroy {
9999
}
100100

101101
public clickNode(event: Event) {
102-
this.root.toggleTreeNode(this.node);
102+
if (!this.root.thyMultiple) {
103+
this.root.selectTreeNode(this.node);
104+
} else {
105+
this.root.toggleTreeNode(this.node);
106+
}
103107
this.thyOnClick.emit({
104108
eventName: 'click',
105109
event: event,

src/tree/tree.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ export class ThyTreeComponent implements ControlValueAccessor, OnInit, OnChanges
102102

103103
@Input() thyTitleTruncate = true;
104104

105+
@Input() thySelectedKeys: string[];
106+
105107
@Input() thyBeforeDragStart: (e: ThyDragStartEvent) => boolean;
106108

107109
@Input() thyBeforeDragDrop: (e: ThyDragDropEvent) => boolean;
@@ -169,6 +171,7 @@ export class ThyTreeComponent implements ControlValueAccessor, OnInit, OnChanges
169171
this._setTreeType();
170172
this._setTreeSize();
171173
this._instanceSelectionModel();
174+
this._setDefaultSelectedKeys();
172175
}
173176

174177
private _setTreeType() {
@@ -187,6 +190,12 @@ export class ThyTreeComponent implements ControlValueAccessor, OnInit, OnChanges
187190
this._selectionModel = new SelectionModel<any>(this.thyMultiple);
188191
}
189192

193+
private _setDefaultSelectedKeys() {
194+
(this.thySelectedKeys || []).forEach(key => {
195+
this.selectTreeNode(this.thyTreeService.getTreeNode(key));
196+
});
197+
}
198+
190199
public isSelected(node: ThyTreeNode) {
191200
return this._selectionModel.isSelected(node);
192201
}

0 commit comments

Comments
 (0)
0