-
Notifications
You must be signed in to change notification settings - Fork 156
onMove receives inconsistent parentId and index #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Same here, moving a node from a nested node to another one get an incorrect index, then if you move the node inside the same level the index is correct. |
Yes. Same here. onMove is giving incorrect position. at root level, if there are two nodes, it am getting position as 2 whereas it should have been 1. |
Possible solution? instead of get the index from the args in onMove, yo can get the proper one from the tree state like this, where tree is the ref to the component. tree.state.dnd.index; |
Will try this |
@surajair the index in the state has the same problem, dragging the node upward the index is increased by 1, you can calculate the direction getting the old rowIndex of the node from the tree instance |
I may need to see more of your code to uncover the issue. I do see that when you drag the first child node "under itself" the index is 1. This might be a bug, might not. You may need to change your "onMove" implementation. Here is how I have written my onMove function in the past. const onMove = (args: {
dragIds: string[];
parentId: null | string;
index: number;
}) => {
for (const id of args.dragIds) {
tree.move({ id, parentId: args.parentId, index: args.index });
}
setData(tree.data);
}; And here is the tree.move function. move(args: { id: string; parentId: string | null; index: number }) {
const src = this.find(args.id);
const parent = args.parentId ? this.find(args.parentId) : this.root;
if (!src || !parent) return;
parent.addChild(src.data, args.index);
src.drop();
} And here is the addChild(data: T, index: number) {
const node = createNode(data, this);
this.children = this.children ?? [];
this.children.splice(index, 0, node);
this.data.children = this.data.children ?? [];
this.data.children.splice(index, 0, data);
} And here is the drop function. drop() {
if (this.hasParent()) this.parent.removeChild(this.childIndex);
} All this code is here in the repo
The algorithm works like this.... |
@davidnaas What if you move your cursor further left before dropping? |
Inconsistent index

Cursor would imply new index should be 1 but is reported as 2
Inconsistent parentId
Cursor and
node.willReceiveDrop
indicates that a drop will NOT happen, yetparentId
is that of theBox
rather thanContainer
This behaviour only happens after first moving

Slider
into aBox
, then out again. Somehow thatSlider
continues to receive theBox
as the parent when being moved. It should also be noted that those boxes are internal nodes in all cases, i.e.children
is always an arrayThe text was updated successfully, but these errors were encountered: