Skip to content

Commit d170037

Browse files
committed
fix(treeSelect): automatically expand the selected nodes on initial render
1 parent b5df611 commit d170037

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

packages/components/tree-select/tree-select.tsx

+34
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,40 @@ export default defineComponent({
7777
},
7878
);
7979

80+
/**
81+
* 递归查找指定节点的所有父节点的 value。
82+
* @param targetValue 要查找的目标节点 value
83+
* @param nodes 树形数据源
84+
* @param ancestors 父节点 value 累加数组
85+
* @returns 则返回父节点 value 数组
86+
*/
87+
function findParentValues(options: TreeOptionData[], targetValue: string | number): (string | number)[] {
88+
function helper(nodes: TreeOptionData[], parentValues: (string | number)[]): (string | number)[] | null {
89+
for (const node of nodes) {
90+
// 找到目标节点,返回当前记录的父节点值数组
91+
if (node.value === targetValue) {
92+
return parentValues;
93+
}
94+
// 递归处理子节点,将当前节点的 value 加入父节点路径
95+
if (Array.isArray(node.children)) {
96+
const found = helper(node.children, [...parentValues, node.value]);
97+
if (found) return found;
98+
}
99+
}
100+
return null;
101+
}
102+
103+
const result = helper(options, []);
104+
return result || [];
105+
}
106+
watch(actived, () => {
107+
if (actived.value.length > 0) {
108+
const activedExpanded = actived.value.map((val) => findParentValues(props.data, val)).flat();
109+
expanded.value = Array.from(new Set([...expanded.value, ...activedExpanded]));
110+
popupVisible.value = true;
111+
}
112+
});
113+
80114
// computed
81115
/** filterByText keep pace with innerInputValue */
82116
const filterByText = computed(() => {

0 commit comments

Comments
 (0)