fix(components): [tree-v2] update expanded state when defaultExpandedKeys change (#23557)

* fix(components): [tree-v2] sync expanded state with defaultExpandedKeys

- guard setExpandedKeys against stale keys
- remove isExpanded, use node.expanded in template
- move defaultExpandedKeys apply logic to data watch
- remove redundant comments in tests

fix #23546

Made-with: Cursor

* fix: update

* fix: move the position of the watch

* fix: revert

---------

Co-authored-by: cye2 <cye2@leqee.com>
Co-authored-by: keeplearning66 <1256734885@qq.com>
This commit is contained in:
Mrbigshot
2026-03-12 21:24:24 +08:00
committed by GitHub
parent 341a08c353
commit dfe0f58351
2 changed files with 81 additions and 8 deletions

View File

@@ -864,6 +864,82 @@ describe('Virtual Tree', () => {
expect(nodes.length).toBe(5)
})
test('defaultExpandedKeys change should update expand icon state', async () => {
const defaultExpandedKeys = ref<string[]>([])
const { wrapper } = createTree({
data() {
return {
height: 400,
data: [
{
id: '1',
label: 'node-1',
children: [
{
id: '1-1',
label: 'node-1-1',
},
],
},
{
id: '2',
label: 'node-2',
},
],
defaultExpandedKeys,
}
},
})
await nextTick()
let expandIcons = wrapper.findAll(TREE_NODE_EXPAND_ICON_CLASS_NAME)
expect(expandIcons.length).toBe(2)
expect(expandIcons[0].classes()).not.toContain('expanded')
defaultExpandedKeys.value = ['1']
await nextTick()
expandIcons = wrapper.findAll(TREE_NODE_EXPAND_ICON_CLASS_NAME)
expect(expandIcons[0].classes()).toContain('expanded')
defaultExpandedKeys.value = []
await nextTick()
expandIcons = wrapper.findAll(TREE_NODE_EXPAND_ICON_CLASS_NAME)
expect(expandIcons[0].classes()).not.toContain('expanded')
})
test('defaultExpandedKeys with child key should expand parent nodes', async () => {
const { wrapper } = createTree({
data() {
return {
height: 400,
data: [
{
id: '1',
label: 'node-1',
children: [
{
id: '1-1',
label: 'node-1-1',
children: [
{ id: '1-1-1', label: 'node-1-1-1' },
{ id: '1-1-2', label: 'node-1-1-2' },
],
},
{ id: '1-2', label: 'node-1-2' },
{ id: '1-3', label: 'node-1-3' },
],
},
{ id: '2', label: 'node-2' },
],
defaultExpandedKeys: ['1-1-1'],
}
},
})
await nextTick()
const nodes = wrapper.findAll(TREE_NODE_CLASS_NAME)
expect(nodes.length).toBe(7)
const expandIcons = wrapper.findAll(TREE_NODE_EXPAND_ICON_CLASS_NAME)
expect(expandIcons[0].classes()).toContain('expanded')
expect(expandIcons[1].classes()).toContain('expanded')
})
test('setExpandedKeys', async () => {
const { treeRef, wrapper } = createTree({
data() {

View File

@@ -182,9 +182,8 @@ export function useTree(
const nodeMap = tree.value!.treeNodeMap
expandedKeySet.value.forEach((key) => {
const node = nodeMap.get(key)!
expandedKeySet.value.delete(node.key)
node.expanded = false
const node = nodeMap.get(key)
if (node) node.expanded = false
})
keys.forEach((k) => {
@@ -313,11 +312,8 @@ export function useTree(
watch(
() => props.defaultExpandedKeys,
(key) => {
expandedKeySet.value = new Set<TreeKey>(key)
},
{
immediate: true,
(keys) => {
setExpandedKeys(keys || [])
}
)
@@ -325,6 +321,7 @@ export function useTree(
() => props.data!,
(data: TreeData) => {
setData(data)
setExpandedKeys(props.defaultExpandedKeys || [])
},
{
immediate: true,