Browse Source

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

pull/19192/head
kavin 5 years ago
committed by 偏右
parent
commit
5fff041e8a
  1. 17
      components/tree/DirectoryTree.tsx
  2. 9
      components/tree/util.ts

17
components/tree/DirectoryTree.tsx

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

9
components/tree/util.ts

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

Loading…
Cancel
Save