fix(#19132): fix component tree defaultExpandAll does not work when using treeData

This commit is contained in:
kavin 2019-10-10 10:06:12 +08:00 committed by 偏右
parent 311a6c0917
commit 5fff041e8a
2 changed files with 24 additions and 2 deletions

View File

@ -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';
@ -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,

View File

@ -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];
}
}