Skip to content

Commit b482f8f

Browse files
authored
fix: improve duplicated key in tree (#255)
1 parent 7382c03 commit b482f8f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/components/tree/index.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,32 @@ const TreeView = ({
9999
onDropTree?.(treeData);
100100
};
101101

102-
const renderTreeNodes = (data: ITreeNodeItemProps[], indent: number) =>
102+
const renderTreeNodes = (
103+
data: ITreeNodeItemProps[],
104+
indent: number,
105+
parentPath?: string
106+
) =>
103107
data?.map((item, index) => {
104108
const {
105109
id,
106110
disabled = false,
107-
// compute key automatic when data don't has key
108-
// take id as backup
109-
key = id || `${index}_${indent}`,
111+
key: rawKey,
110112
icon,
111113
children = [],
112114
isLeaf: itemIsLeaf,
113115
} = item;
116+
114117
const isLeaf =
115118
typeof itemIsLeaf === 'boolean'
116119
? itemIsLeaf
117120
: item.fileType === FileTypes.File || children.length === 0;
121+
118122
const IconComponent =
119123
typeof icon === 'string' ? <Icon type={icon} /> : icon;
124+
125+
// calculate key automatically via parent path and self id
126+
const key = rawKey || `${parentPath ? parentPath + '_' : ''}${id}`;
127+
120128
return (
121129
/**
122130
* TODO: antd TreeNode 目前强依赖于 Tree,不好抽离,后续还不支持的话,考虑重写..
@@ -135,7 +143,7 @@ const TreeView = ({
135143
key={key}
136144
icon={IconComponent}
137145
>
138-
{children && renderTreeNodes(children, indent + 1)}
146+
{children && renderTreeNodes(children, indent + 1, key)}
139147
</RcTreeNode>
140148
);
141149
});

0 commit comments

Comments
 (0)