Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: builtin Tree component can recognizes empty ul as a folder #2115

Merged
merged 5 commits into from
Jun 11, 2024
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
12 changes: 12 additions & 0 deletions docs/guide/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ features:
<li>
src
<ul>
<li>directory <ul></ul></li>
<li>index.md</li>
</ul>
</li>
Expand All @@ -187,6 +188,7 @@ features:
<li>
src
<ul>
<li>directory <ul></ul></li>
<li>index.md</li>
</ul>
</li>
Expand All @@ -203,6 +205,11 @@ features:
src
+ <small>这是 src 文件夹</small>
<ul>
<li>
directory
+ <small>没有子项的文件夹</small>
<ul></ul>
</li>
<li>
index.md
+ <small>这是 index.md</small>
Expand All @@ -225,6 +232,11 @@ features:
src
<small>这是 src 文件夹</small>
<ul>
<li>
directory
<small>没有子项的文件夹</small>
<ul></ul>
</li>
<li>
index.md
<small>这是 index.md</small>
Expand Down
46 changes: 30 additions & 16 deletions src/client/theme-default/builtins/Tree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? (
<span className="tree-switcher-leaf-line" />
) : undefined,
});
break;
}
Expand All @@ -63,38 +71,38 @@ const useListToTree = (nodes: ReactNode) => {
};

const getIcon = (props: TreeNodeProps<DataNode>) => {
const { isLeaf, expanded } = props;
const { isLeaf, expanded, data } = props;
if (isLeaf) {
return (
<span className="dumi-default-tree-icon">
<FileOutlined fill="currentColor" />
</span>
);
}
return expanded ? (
return !expanded || !data?.children?.length ? (
<span className="dumi-default-tree-icon">
<FolderOpenOutlined fill="currentColor" />
<FolderOutlined fill="currentColor" />
</span>
) : (
<span className="dumi-default-tree-icon">
<FolderOutlined fill="currentColor" />
<FolderOpenOutlined fill="currentColor" />
</span>
);
};

const renderSwitcherIcon = (props: TreeNodeProps<DataNode>) => {
const { isLeaf, expanded } = props;
if (isLeaf) {
return <span className={`tree-switcher-leaf-line`} />;
return <span className="tree-switcher-leaf-line" />;
}
return expanded ? (
<span className={`tree-switcher-line-icon`}>
<span className="tree-switcher-line-icon">
<span className="dumi-default-tree-icon">
<MinusSquareOutlined fill="currentColor" />
</span>
</span>
) : (
<span className={`tree-switcher-line-icon`}>
<span className="tree-switcher-line-icon">
<span className="dumi-default-tree-icon">
<PlusSquareOutlined fill="currentColor" />
</span>
Expand Down Expand Up @@ -142,8 +150,14 @@ export default (props: ComponentProps<'div'>) => {
node: EventDataNode<any>,
) => {
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);
Expand Down
Loading