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

bug fix : expand all leaf nodes in root level #47567

Merged
merged 13 commits into from
Feb 24, 2024
16 changes: 13 additions & 3 deletions components/tree/DirectoryTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import FolderOutlined from '@ant-design/icons/FolderOutlined';
import classNames from 'classnames';
import type RcTree from 'rc-tree';
import type { BasicDataNode } from 'rc-tree';
import type { DataNode, EventDataNode, Key } from 'rc-tree/lib/interface';
import type { DataNode, EventDataNode, Key, KeyEntities } from 'rc-tree/lib/interface';
import { conductExpandParent } from 'rc-tree/lib/util';
import { convertDataToEntities, convertTreeToData } from 'rc-tree/lib/utils/treeUtil';

Expand Down Expand Up @@ -52,7 +52,7 @@ const DirectoryTree: React.ForwardRefRenderFunction<RcTree, DirectoryTreeProps>
const cachedSelectedKeys = React.useRef<Key[]>();

const getInitExpandedKeys = () => {
const { keyEntities } = convertDataToEntities(getTreeData(props));
const { keyEntities }: { keyEntities: KeyEntities } = convertDataToEntities(getTreeData(props));
wkmyws marked this conversation as resolved.
Show resolved Hide resolved

let initExpandedKeys: Key[];

Expand All @@ -65,7 +65,17 @@ const DirectoryTree: React.ForwardRefRenderFunction<RcTree, DirectoryTreeProps>
keyEntities,
);
} else {
initExpandedKeys = (props.expandedKeys || defaultExpandedKeys)!;
initExpandedKeys = props.expandedKeys || defaultExpandedKeys || [];
// If not specified, expand all leaf nodes in the first level.
const firstLayerLeafs = Object.values(keyEntities)
.filter((item) => {
if (item.parent === undefined && item?.node.isLeaf === true) {
wkmyws marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
})
.map((item) => item.key);
initExpandedKeys = Array.from(new Set([...initExpandedKeys, ...firstLayerLeafs]));
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved
}
return initExpandedKeys;
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved
};
Expand Down