Skip to content

Commit

Permalink
Rewrite findIndexInSelection (Tree and TreeSelect)
Browse files Browse the repository at this point in the history
  • Loading branch information
perceptron8 committed Oct 5, 2024
1 parent f0a27b7 commit 93ce735
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 33 deletions.
19 changes: 3 additions & 16 deletions src/app/components/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1435,24 +1435,11 @@ export class Tree implements OnInit, AfterContentInit, OnChanges, OnDestroy, Blo
}

findIndexInSelection(node: TreeNode) {
let index: number = -1;
if (this.selectionMode && this.selection) {
if (this.isSingleSelectionMode()) {
let areNodesEqual = (this.selection.key !== undefined && this.selection.key === node.key) || this.selection == node;
index = areNodesEqual ? 0 : -1;
} else {
for (let i = 0; i < this.selection.length; i++) {
let selectedNode = this.selection[i];
let areNodesEqual = (selectedNode.key !== undefined && selectedNode.key === node.key) || selectedNode == node;
if (areNodesEqual) {
index = i;
break;
}
}
}
const selection = this.isSingleSelectionMode() ? [this.selection] : this.selection;
return selection.findIndex(selectedNode => selectedNode === node || selectedNode.key === node.key && selectedNode.key !== undefined);
}

return index;
return -1;
}

syncNodeOption(node: TreeNode, parentNodes: TreeNode<any>[], option: any, value?: any) {
Expand Down
20 changes: 3 additions & 17 deletions src/app/components/treeselect/treeselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,25 +907,11 @@ export class TreeSelect implements AfterContentInit {
}

findIndexInSelection(node: TreeNode) {
let index: number = -1;

if (this.value) {
if (this.selectionMode === 'single') {
let areNodesEqual = (this.value.key !== undefined && this.value.key === node.key) || this.value === node;
index = areNodesEqual ? 0 : -1;
} else {
for (let i = 0; i < this.value.length; i++) {
let selectedNode = this.value[i];
let areNodesEqual = (selectedNode.key !== undefined && selectedNode.key === node.key) || selectedNode === node;
if (areNodesEqual) {
index = i;
break;
}
}
}
const value = this.selectionMode === 'single' ? [this.value] : this.value;
return value.findIndex(selectedNode => selectedNode === node || selectedNode.key === node.key && selectedNode.key !== undefined);
}

return index;
return -1;
}

onSelect(event: TreeNodeSelectEvent) {
Expand Down

0 comments on commit 93ce735

Please sign in to comment.