Skip to content

Commit b92268b

Browse files
committed
fix: onDrop for treeData
1 parent 6e54ae5 commit b92268b

File tree

1 file changed

+24
-39
lines changed

1 file changed

+24
-39
lines changed

lib/HeTree.tsx

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -452,53 +452,38 @@ export function useHeTree<T extends Record<string, any>>(
452452
customized = true
453453
}
454454
if (!customized && !isExternal) {
455-
// move node
456-
// const draft = getDraft()
457-
// let draggedSiblings: T[], targetSiblings: T[]
458-
// if (!placeholder.parentStat) {
459-
// targetSiblings = draft
460-
// }
461-
// let draggedNodeDraft: T
462-
// for (const [node, { siblings }] of walkTreeDataGenerator(draft, CHILDREN)) {
463-
// if (node[ID] === draggedStat.id) {
464-
// draggedSiblings = siblings
465-
// draggedNodeDraft = node
466-
// }
467-
// if (!targetSiblings! && node[ID] === placeholder.parentStat!.id) {
468-
// targetSiblings = node[CHILDREN]
469-
// }
470-
// if (draggedSiblings! && targetSiblings!) {
471-
// break
472-
// }
473-
// }
474-
// const ds = draggedSiblings!
475-
// const ts = targetSiblings!
476-
// const startIndex = ds.findIndex(v => v[ID] === draggedStat.id)
477-
// let targetIndex = placeholder.index
478-
// // check index
479-
// if (ds === ts && placeholder.index > startIndex) {
480-
// targetIndex -= 1
481-
// }
482-
// // remove from start position
483-
// ds.splice(startIndex, 1)
484-
// // move to target position
485-
// ts.splice(targetIndex, 0, draggedNodeDraft!)
486-
// props.onChange(nextData())
455+
let targetIndexInSiblings = placeholder.index
456+
if (placeholder.parentStat === draggedStat.parentStat && draggedStat.index < targetIndexInSiblings) {
457+
targetIndexInSiblings--
458+
}
459+
const newData = [...props.data];
487460
if (props.dataType === 'flat') {
488-
const startIndex = props.data.findIndex(v => v[ID] === draggedStat.id)
489-
let targetIndexInSiblings = placeholder.index
490-
if (placeholder.parentStat === draggedStat.parentStat && draggedStat.index < targetIndexInSiblings) {
491-
targetIndexInSiblings--
492-
}
493461
const targetParentId = placeholder.parentStat?.id ?? null
494-
const newData = [...props.data];
495462
const removed = removeByIdInFlatData(newData, draggedStat.id, flatOpt)
496463
const newNode = { ...draggedStat.node, [PID]: targetParentId }
497464
removed[0] = newNode
498465
const targetTreeIndex = convertIndexToTreeIndexInFlatData(newData, targetParentId, targetIndexInSiblings, flatOpt)
499466
newData.splice(targetTreeIndex, 0, ...removed)
500-
props.onChange!(newData)
467+
} else {
468+
// copy data
469+
const copyNode = (stat: Stat<T> | null) => {
470+
if (!stat) {
471+
return newData
472+
}
473+
const siblings = copyNode(stat.parentStat)
474+
const children = [...stat.children];
475+
const newNode = { ...stat.node, [CHILDREN]: children }
476+
siblings[stat.index] = newNode;
477+
return children
478+
}
479+
const newSiblingsOfDragged = copyNode(draggedStat.parentStat)
480+
const newSiblingsOfTarget = placeholder.parentStat === draggedStat.parentStat ? newSiblingsOfDragged : copyNode(placeholder.parentStat)
481+
// remove
482+
newSiblingsOfDragged.splice(draggedStat.index, 1)
483+
// add
484+
newSiblingsOfTarget.splice(targetIndexInSiblings, 0, draggedStat.node)
501485
}
486+
props.onChange!(newData)
502487
}
503488
}
504489
if (!customized) {

0 commit comments

Comments
 (0)