File tree 1 file changed +13
-5
lines changed
1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -99,24 +99,32 @@ const TreeView = ({
99
99
onDropTree ?.( treeData ) ;
100
100
} ;
101
101
102
- const renderTreeNodes = ( data : ITreeNodeItemProps [ ] , indent : number ) =>
102
+ const renderTreeNodes = (
103
+ data : ITreeNodeItemProps [ ] ,
104
+ indent : number ,
105
+ parentPath ?: string
106
+ ) =>
103
107
data ?. map ( ( item , index ) => {
104
108
const {
105
109
id,
106
110
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 ,
110
112
icon,
111
113
children = [ ] ,
112
114
isLeaf : itemIsLeaf ,
113
115
} = item ;
116
+
114
117
const isLeaf =
115
118
typeof itemIsLeaf === 'boolean'
116
119
? itemIsLeaf
117
120
: item . fileType === FileTypes . File || children . length === 0 ;
121
+
118
122
const IconComponent =
119
123
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
+
120
128
return (
121
129
/**
122
130
* TODO: antd TreeNode 目前强依赖于 Tree,不好抽离,后续还不支持的话,考虑重写..
@@ -135,7 +143,7 @@ const TreeView = ({
135
143
key = { key }
136
144
icon = { IconComponent }
137
145
>
138
- { children && renderTreeNodes ( children , indent + 1 ) }
146
+ { children && renderTreeNodes ( children , indent + 1 , key ) }
139
147
</ RcTreeNode >
140
148
) ;
141
149
} ) ;
You can’t perform that action at this time.
0 commit comments