diff --git a/packages/components/tree/__tests__/tree.test.ts b/packages/components/tree/__tests__/tree.test.ts index bcb95a209c..d02b9e7c7a 100644 --- a/packages/components/tree/__tests__/tree.test.ts +++ b/packages/components/tree/__tests__/tree.test.ts @@ -805,8 +805,22 @@ describe('Tree.vue', () => { const tree = treeWrapper.vm as InstanceType 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 + + tree.setCheckedKeys([1], true) + + expect(tree.getCheckedKeys().sort()).toEqual([1, 11, 111]) + expect(tree.getHalfCheckedNodes()).toEqual([]) + expect(tree.getHalfCheckedKeys()).toEqual([]) }) test('setCurrentKey', async () => { diff --git a/packages/components/tree/src/model/tree-store.ts b/packages/components/tree/src/model/tree-store.ts index c1c6250e47..ae33aae3be 100644 --- a/packages/components/tree/src/model/tree-store.ts +++ b/packages/components/tree/src/model/tree-store.ts @@ -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) }