Skip to content

Commit

Permalink
Support empty TreeNode.key in findIndexInSelection (#16495)
Browse files Browse the repository at this point in the history
* Support empty `TreeNode.key` in `findIndexInSelection`

* Rewrite `findIndexInSelection` (`Tree` and `TreeSelect`)

* Add `Tree.selection` tests
  • Loading branch information
perceptron8 authored Nov 18, 2024
1 parent 6a6606f commit 7e7de62
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
32 changes: 32 additions & 0 deletions src/app/components/tree/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,36 @@ describe('Tree', () => {
expect(tree.filteredNodes).toBeTruthy();
expect(tree.filteredNodes.length).toEqual(2);
});

it('should select nodes by reference', () => {
// <p-tree selectionMode="single" [value]="[root]" [selection]="root" />

const root = {key: undefined, label: '(root)'};
tree.selectionMode = 'single';
tree.value = [root];

tree.selection = root;
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('.p-highlight'))).toBeTruthy();

tree.selection = undefined;
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('.p-highlight'))).toBeFalsy();
});

it('should select nodes by key (even if key is empty!)', () => {
// <p-tree selectionMode="single" [value]="[root]" [selection]="{key: root.key, label: root.label}" />

const root = {key: '', label: '(root)'};
tree.selectionMode = 'single';
tree.value = [root];

tree.selection = {key: root.key, label: root.label};
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('.p-highlight'))).toBeTruthy();

tree.selection = {key: 'non-' + root.key, label: root.label};
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('.p-highlight'))).toBeFalsy();
});
});
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 && 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 && 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 && 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 && 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 7e7de62

Please sign in to comment.