Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Performance issue for TreeSelect component #6951

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions packages/primevue/src/treeselect/TreeSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -452,22 +452,11 @@ export default {
onOverlayKeydown(event) {
if (event.code === 'Escape') this.hide();
},
findSelectedNodes(node, keys, selectedNodes) {
if (node) {
if (this.isSelected(node, keys)) {
selectedNodes.push(node);
delete keys[node.key];
}
fillNodeMap(node, nodeMap) {
nodeMap[node.key] = node;

if (Object.keys(keys).length && node.children) {
for (let childNode of node.children) {
this.findSelectedNodes(childNode, keys, selectedNodes);
}
}
} else {
for (let childNode of this.options) {
this.findSelectedNodes(childNode, keys, selectedNodes);
}
if (node.children?.length) {
node.children.forEach(children => this.fillNodeMap(children, nodeMap))
}
},
isSelected(node, keys) {
Expand Down Expand Up @@ -520,13 +509,24 @@ export default {
}
},
computed: {
nodeMap() {
const nodeMap = {};

this.options?.forEach(node => this.fillNodeMap(node, nodeMap))

return nodeMap;
},
selectedNodes() {
let selectedNodes = [];

if (this.d_value && this.options) {
let keys = { ...this.d_value };
Object.keys(this.d_value).forEach(key => {
const node = this.nodeMap[key];

this.findSelectedNodes(null, keys, selectedNodes);
if (this.isSelected(node, this.d_value)) {
selectedNodes.push(node)
}
})
}

return selectedNodes;
Expand Down