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

fix(#19132): fix component tree defaultExpandAll does not work when u… #19148

Merged
merged 9 commits into from Oct 12, 2019
17 changes: 15 additions & 2 deletions components/tree/DirectoryTree.tsx
Expand Up @@ -13,7 +13,12 @@ import Tree, {
AntTreeNodeSelectedEvent,
AntTreeNode,
} from './Tree';
import { calcRangeKeys, getFullKeyList, convertDirectoryKeysToNodes } from './util';
import {
calcRangeKeys,
getFullKeyList,
convertDirectoryKeysToNodes,
getFullKeyListByTreeData,
} from './util';
import Icon from '../icon';

export type ExpandAction = false | 'click' | 'doubleClick';
Expand Down Expand Up @@ -82,7 +87,15 @@ class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeSta

// Expanded keys
if (defaultExpandAll) {
this.state.expandedKeys = getFullKeyList(props.children);
const expandedKeysByChildren = getFullKeyList(props.children);
this.state.expandedKeys = expandedKeysByChildren;
if (
!!props.treeData &&
Array.isArray(expandedKeysByChildren) &&
expandedKeysByChildren.length === 0
) {
this.state.expandedKeys = getFullKeyListByTreeData(props.treeData);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to:

if (treeData) {
  this.state.expandedKeys = ...
} else {
  this.state.expandedKeys = ...
}

treeData priority should be higher than children.

} else if (defaultExpandParent) {
this.state.expandedKeys = conductExpandParent(
expandedKeys || defaultExpandedKeys,
Expand Down
9 changes: 9 additions & 0 deletions components/tree/util.ts
Expand Up @@ -101,3 +101,12 @@ export function convertDirectoryKeysToNodes(
});
return nodes;
}

export function getFullKeyListByTreeData(treeData: any[]): any {
for (const item of treeData) {
if (item.children) {
return getFullKeyListByTreeData(item.children).concat([item.key]);
}
return [item.key];
}
}