diff --git a/packages/primevue/src/tree/Tree.vue b/packages/primevue/src/tree/Tree.vue index db04745410..6cb8b19c2e 100755 --- a/packages/primevue/src/tree/Tree.vue +++ b/packages/primevue/src/tree/Tree.vue @@ -221,6 +221,54 @@ export default { return matched; } }, + watch: { + valueToRender(newValueToRender) { + const _selectionKeys = { ...this.selectionKeys }; + + const getChildrenSelection = (node) => { + const selection = { allChecked: true, someChecked: false }; + + for (const childNode of node.children) { + const keyState = _selectionKeys[childNode.key]; + + if (keyState?.checked) { + selection.someChecked = true; + } else if (keyState?.partialChecked) { + selection.someChecked = true; + selection.allChecked = false; + } else { + selection.allChecked = false; + } + } + + return selection; + }; + + const recalculateSelection = (nodes) => { + for (const node of nodes) { + if (!node.children || !node.children.length) { + continue; + } + + recalculateSelection(node.children); + + const { allChecked, someChecked } = getChildrenSelection(node); + + if (allChecked) { + _selectionKeys[node.key] = { checked: true, partialChecked: false }; + } else if (someChecked) { + _selectionKeys[node.key] = { checked: false, partialChecked: true }; + } else { + delete _selectionKeys[node.key]; + } + } + }; + + recalculateSelection(newValueToRender); + + this.$emit('update:selectionKeys', _selectionKeys); + } + }, computed: { filteredValue() { let filteredNodes = [];