Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
7 changes: 5 additions & 2 deletions src/tree-select/tree-select.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}
Expand Down