From 3782131facf8e67d75d594203ac5e92558217a1f Mon Sep 17 00:00:00 2001 From: maxim Date: Thu, 12 Dec 2024 02:08:05 +0400 Subject: [PATCH] #6950 Fixes performance issue for TreeSelect component --- .../primevue/src/treeselect/TreeSelect.vue | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/primevue/src/treeselect/TreeSelect.vue b/packages/primevue/src/treeselect/TreeSelect.vue index 0be9818d3..07a9cd191 100644 --- a/packages/primevue/src/treeselect/TreeSelect.vue +++ b/packages/primevue/src/treeselect/TreeSelect.vue @@ -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) { @@ -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;