Skip to content

Commit

Permalink
fix(ant-design#19132): fix component tree defaultExpandAll does not w…
Browse files Browse the repository at this point in the history
…ork when using treeData
  • Loading branch information
kavin committed Oct 10, 2019
1 parent d8526f4 commit bcffcc3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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);
}
} 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];
}
}

0 comments on commit bcffcc3

Please sign in to comment.