diff --git a/src/common/utils.ts b/src/common/utils.ts index f9810453e..f46283577 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -115,6 +115,20 @@ export const getRect = function (context: any, selector: string, needAll: boolea }); }; +interface TreeNode { + children?: TreeNode[]; + [key: string]: any; +} + +export const getTreeDepth = (tree: TreeNode[], key?: string) => { + return tree.reduce((maxDepth: number, node: TreeNode) => { + if (node[key ?? 'children'] && node[key ?? 'children'].length > 0) { + return Math.max(maxDepth, getTreeDepth(node[key ?? 'children'], key) + 1); + } + return Math.max(maxDepth, 1); + }, 0); +}; + export const isIOS = function (): boolean { return !!(deviceInfo?.system?.toLowerCase().search('ios') + 1); }; diff --git a/src/tree-select/tree-select.ts b/src/tree-select/tree-select.ts index 2be38a764..2a9884987 100644 --- a/src/tree-select/tree-select.ts +++ b/src/tree-select/tree-select.ts @@ -1,6 +1,7 @@ import { SuperComponent, wxComponent } from '../common/src/index'; import { isDef } from '../common/validator'; import config from '../common/config'; +import { getTreeDepth } from '../common/utils'; import props from './props'; import type { TreeOptionData } from '../common/common'; @@ -79,8 +80,10 @@ export default class TreeSelect extends SuperComponent { : currentLevelOptions[0]; } - // Ensure at least two levels (even if second is empty) - if (treeOptions.length === 1) { + const depth = getTreeDepth(options, keys?.children); + + // 补齐 treeOptions 长度到 depth + while (treeOptions.length < depth) { treeOptions.push([]); level += 1; }