fix(components): [tree] reconcile parent states in setCheckedKeys (#23697)

closed #22032
This commit is contained in:
Noblet Ouways
2026-03-03 09:49:47 +01:00
committed by GitHub
parent fea1ae7a86
commit d1a60f599d
2 changed files with 19 additions and 4 deletions

View File

@@ -805,8 +805,22 @@ describe('Tree.vue', () => {
const tree = treeWrapper.vm as InstanceType<typeof Tree>
tree.setCheckedKeys([2], true)
expect(tree.getCheckedNodes().length).toEqual(2)
expect(tree.getCheckedKeys().length).toEqual(2)
expect(tree.getCheckedNodes().length).toEqual(3)
expect(tree.getCheckedKeys().length).toEqual(3)
})
test('setCheckedKeys with leafOnly=true produces consistent parent states', async () => {
const { wrapper } = getTreeVm(
`:props="defaultProps" show-checkbox node-key="id"`
)
const treeWrapper = wrapper.findComponent(Tree)
const tree = treeWrapper.vm as InstanceType<typeof Tree>
tree.setCheckedKeys([1], true)
expect(tree.getCheckedKeys().sort()).toEqual([1, 11, 111])
expect(tree.getHalfCheckedNodes()).toEqual([])
expect(tree.getHalfCheckedKeys()).toEqual([])
})
test('setCurrentKey', async () => {

View File

@@ -340,15 +340,16 @@ export default class TreeStore {
node.setChecked(true, true)
if (leafOnly) {
node.setChecked(false, false)
node.setChecked(false, false, true)
const traverse = function (node: Node): void {
const childNodes = node.childNodes
childNodes.forEach((child) => {
if (!child.isLeaf) {
child.setChecked(false, false)
child.setChecked(false, false, true)
}
traverse(child)
})
node.reInitChecked()
}
traverse(node)
}