Skip to content

Commit

Permalink
feat(tree): support default selected keys prop
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerkay committed Dec 24, 2019
1 parent 2bd093e commit 02a0c03
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/tree/test/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ describe('ThyTreeComponent', () => {
expect(treeElement.querySelector('.thy-tree-expand-icon').classList).toContain('thy-icon-plus-square');
});

it('test set selectedKeys correctly', () => {
expect(treeComponent.getSelectedNodes().length).toEqual(1);
expect(treeComponent.getSelectedNode().title).toEqual('未分配部门');
});

it(`test public function 'getRootNodes()`, () => {
expect(treeComponent.getRootNodes().length).toEqual(2);
});
Expand All @@ -66,7 +71,6 @@ describe('ThyTreeComponent', () => {
});

it(`test public function 'getSelectedNodes()`, () => {
expect(treeComponent.getSelectedNodes().length).toEqual(0);
treeComponent.selectTreeNode(treeComponent.getRootNodes()[1]);
fixture.detectChanges();
expect(treeComponent.getSelectedNodes().length).toEqual(1);
Expand Down Expand Up @@ -158,6 +162,7 @@ describe('ThyTreeComponent', () => {
[thyDraggable]="options.draggable"
[thyCheckable]="options.checkable"
[thyMultiple]="options.multiple"
[thySelectedKeys]="['000000000000000000000000']"
[thyShowExpand]="true"
[thyBeforeDragDrop]="beforeDragDrop"
(thyOnDragDrop)="onEvent()"
Expand Down
6 changes: 5 additions & 1 deletion src/tree/tree-node.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ export class ThyTreeNodeComponent implements OnDestroy {
}

public clickNode(event: Event) {
this.root.toggleTreeNode(this.node);
if (!this.root.thyMultiple) {
this.root.selectTreeNode(this.node);
} else {
this.root.toggleTreeNode(this.node);
}
this.thyOnClick.emit({
eventName: 'click',
event: event,
Expand Down
9 changes: 9 additions & 0 deletions src/tree/tree.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export class ThyTreeComponent implements ControlValueAccessor, OnInit, OnChanges

@Input() thyTitleTruncate = true;

@Input() thySelectedKeys: string[];

@Input() thyBeforeDragStart: (e: ThyDragStartEvent) => boolean;

@Input() thyBeforeDragDrop: (e: ThyDragDropEvent) => boolean;
Expand Down Expand Up @@ -169,6 +171,7 @@ export class ThyTreeComponent implements ControlValueAccessor, OnInit, OnChanges
this._setTreeType();
this._setTreeSize();
this._instanceSelectionModel();
this._setDefaultSelectedKeys();
}

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

private _setDefaultSelectedKeys() {
(this.thySelectedKeys || []).forEach(key => {
this.selectTreeNode(this.thyTreeService.getTreeNode(key));
});
}

public isSelected(node: ThyTreeNode) {
return this._selectionModel.isSelected(node);
}
Expand Down

0 comments on commit 02a0c03

Please sign in to comment.