diff --git a/docs/guide/markdown.md b/docs/guide/markdown.md
index 60327b69d7..144d61e4e5 100644
--- a/docs/guide/markdown.md
+++ b/docs/guide/markdown.md
@@ -172,6 +172,7 @@ features:
src
@@ -187,6 +188,7 @@ features:
src
@@ -203,6 +205,11 @@ features:
src
+ 这是 src 文件夹
+ -
+ directory
++ 没有子项的文件夹
+
+
-
index.md
+ 这是 index.md
@@ -225,6 +232,11 @@ features:
src
这是 src 文件夹
+ -
+ directory
+ 没有子项的文件夹
+
+
-
index.md
这是 index.md
diff --git a/src/client/theme-default/builtins/Tree/index.tsx b/src/client/theme-default/builtins/Tree/index.tsx
index 4c1017db28..8195e50b59 100644
--- a/src/client/theme-default/builtins/Tree/index.tsx
+++ b/src/client/theme-default/builtins/Tree/index.tsx
@@ -32,15 +32,23 @@ function getTreeFromList(nodes: ReactNode, prefix = '') {
}
case 'li': {
- const liLeafs = getTreeFromList(node.props.children, key);
-
+ const hasEmptyUl = node.props.children?.some?.(
+ (child) => child.type === 'ul' && !child.props.children?.length,
+ );
+ const title = ([] as ReactNode[])
+ .concat(node.props.children)
+ .filter((child) => child.type !== 'ul');
+ const children = hasEmptyUl
+ ? []
+ : getTreeFromList(node.props.children, key);
data.push({
- title: ([] as ReactNode[])
- .concat(node.props.children)
- .filter((child) => child.type !== 'ul'),
+ title,
key,
- children: liLeafs,
- isLeaf: !liLeafs.length,
+ children,
+ isLeaf: !hasEmptyUl && !children.length,
+ switcherIcon: hasEmptyUl ? (
+
+ ) : undefined,
});
break;
}
@@ -63,7 +71,7 @@ const useListToTree = (nodes: ReactNode) => {
};
const getIcon = (props: TreeNodeProps) => {
- const { isLeaf, expanded } = props;
+ const { isLeaf, expanded, data } = props;
if (isLeaf) {
return (
@@ -71,13 +79,13 @@ const getIcon = (props: TreeNodeProps) => {
);
}
- return expanded ? (
+ return !expanded || !data?.children?.length ? (
-
+
) : (
-
+
);
};
@@ -85,16 +93,16 @@ const getIcon = (props: TreeNodeProps) => {
const renderSwitcherIcon = (props: TreeNodeProps) => {
const { isLeaf, expanded } = props;
if (isLeaf) {
- return ;
+ return ;
}
return expanded ? (
-
+
) : (
-
+
@@ -142,8 +150,14 @@ export default (props: ComponentProps<'div'>) => {
node: EventDataNode,
) => {
const { isLeaf } = node;
-
- if (isLeaf || event.shiftKey || event.metaKey || event.ctrlKey) {
+ const isEmptyUl = !isLeaf && !node.children?.length;
+ if (
+ isLeaf ||
+ isEmptyUl ||
+ event.shiftKey ||
+ event.metaKey ||
+ event.ctrlKey
+ ) {
return;
}
treeRef.current!.onNodeExpand(event as any, node);