@@ -397,23 +397,7 @@ export default class TreeStore extends TreeEventTarget {
397397 } )
398398 . then ( children => {
399399 if ( Array . isArray ( children ) ) {
400- const parentIndex : number = this . findIndex ( node )
401- if ( parentIndex === - 1 ) return
402- node . _loaded = true
403- node . expand = value
404- node . setChildren ( children )
405- // 如果单选选中的值为空,则允许后续数据覆盖单选 value
406- const currentCheckedKeys = this . getCheckedKeys ( )
407- const flattenChildren = this . flattenData (
408- node . children ,
409- this . getSelectedKey === null
410- )
411- this . insertIntoFlatData ( parentIndex + 1 , flattenChildren )
412- // 如果有未加载的选中节点,判断其是否已加载
413- this . setUnloadCheckedKeys ( currentCheckedKeys )
414- if ( this . unloadSelectedKey !== null ) {
415- this . setUnloadSelectedKey ( this . unloadSelectedKey )
416- }
400+ this . loadChildren ( node , children , value )
417401 this . emit ( 'set-data' )
418402 }
419403 } )
@@ -531,18 +515,11 @@ export default class TreeStore extends TreeEventTarget {
531515
532516 if ( 'children' in newNodeCopy ) {
533517 // remove all children
534- const childKeys = this . mapData [ key ] . children . map ( ( child ) => child [ this . options . keyField ] )
535- childKeys . forEach ( ( childKey ) => {
536- // TODO: 重新写一个批量移除 children 的函数
537- this . remove ( childKey , false , false )
538- } )
518+ this . removeChildren ( key , false , false )
539519
540520 // add new children
541521 if ( Array . isArray ( newNodeCopy . children ) ) {
542- newNodeCopy . children . forEach ( ( child ) => {
543- // TODO: 批量添加抽离 setExpand 中 load children 的逻辑
544- this . append ( child , key , false , false )
545- } )
522+ this . loadChildren ( this . mapData [ key ] , newNodeCopy . children , this . mapData [ key ] . expand )
546523 }
547524
548525 delete newNodeCopy . children
@@ -908,6 +885,66 @@ export default class TreeStore extends TreeEventTarget {
908885 return node
909886 }
910887
888+ private removeChildren (
889+ parentKey : TreeNodeKeyType ,
890+ triggerEvent : boolean = true ,
891+ triggerDataChange : boolean = true ,
892+ ) {
893+ const node = this . mapData [ parentKey ]
894+ if ( ! node || ! node . children . length ) return null
895+ 896+ const firstChild = node . children [ 0 ]
897+ 898+ // 从 flatData 中移除
899+ const index = this . findIndex ( node )
900+ if ( index === - 1 ) return null
901+ let deleteCount = 0
902+ const length = this . flatData . length
903+ for ( let i = index + 1 ; i < length ; i ++ ) {
904+ if ( this . flatData [ i ] . _level > node . _level ) {
905+ // 从 mapData 中移除
906+ delete this . mapData [ this . flatData [ i ] [ this . options . keyField ] ]
907+ deleteCount ++
908+ } else break
909+ }
910+ this . flatData . splice ( index + 1 , deleteCount )
911+ 912+ // 从父节点 children 中移除
913+ node . children . splice ( 0 , node . children . length )
914+ node . isLeaf = true
915+ node . indeterminate = false
916+ 917+ // 更新被移除处父节点状态
918+ this . updateMovingNodeStatus ( firstChild , triggerEvent , triggerDataChange )
919+ 920+ if ( triggerDataChange ) {
921+ this . emit ( 'visible-data-change' )
922+ }
923+ 924+ return node
925+ }
926+ 927+ private loadChildren ( node : TreeNode , children : any [ ] , expand : boolean ) {
928+ const parentIndex : number = this . findIndex ( node )
929+ if ( parentIndex === - 1 ) return
930+ node . _loaded = true
931+ node . expand = expand
932+ node . setChildren ( children )
933+ node . isLeaf = ! node . children . length
934+ // 如果单选选中的值为空,则允许后续数据覆盖单选 value
935+ const currentCheckedKeys = this . getCheckedKeys ( )
936+ const flattenChildren = this . flattenData (
937+ node . children ,
938+ this . getSelectedKey === null
939+ )
940+ this . insertIntoFlatData ( parentIndex + 1 , flattenChildren )
941+ // 如果有未加载的选中节点,判断其是否已加载
942+ this . setUnloadCheckedKeys ( currentCheckedKeys )
943+ if ( this . unloadSelectedKey !== null ) {
944+ this . setUnloadSelectedKey ( this . unloadSelectedKey )
945+ }
946+ }
947+ 911948 private getInsertedNode (
912949 insertedNode : TreeNodeKeyType | ITreeNodeOptions ,
913950 referenceKey : TreeNodeKeyType ,
0 commit comments