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(tree): checkbox partialChecked not correct when filtering #6964

Closed
wants to merge 2 commits into from
Closed
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
48 changes: 48 additions & 0 deletions packages/primevue/src/tree/Tree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down