Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/DraggableTreeviewNode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
:group="group"
:value="value.children"
ghost-class="ghost"
@input="updateValue"
@change="updateValue"
>
<treeview-node
v-for="child in value.children"
Expand Down Expand Up @@ -109,7 +109,10 @@ export default Vue.extend({
data() {
return {
open: false,
localValue: { ...this.value } as TreeItem,
localValue: {
...this.value,
children: [...this.value.children],
} as TreeItem,
};
},
computed: {
Expand All @@ -125,12 +128,30 @@ export default Vue.extend({
},
watch: {
value(value): void {
this.localValue = { ...value };
this.localValue = {
...value,
children: [...this.value.children],
};
},
},
methods: {
updateValue(value: TreeItem[]): void {
this.localValue.children = [...value];
updateValue($event: any): void {
// removing must be done before adding
// in order for the event `moved` to work properly
const dataForRemoving = $event.removed || $event.moved;
if (dataForRemoving) {
this.localValue.children.splice(dataForRemoving.oldIndex, 1);
}

const dataForAdding = $event.added || $event.moved;
if (dataForAdding) {
this.localValue.children.splice(
dataForAdding.newIndex,
0,
dataForAdding.element
);
}

Comment on lines +138 to +154

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the mutating error is fixed by the 2 other changes that create a shallow copy of children for localValue

but without this change, a strange bug appears :

Screen.Recording.2023-11-02.at.15.22.15.mov

on drag/drop, multiple updates occur :

  1. the child "test 2" gets updated with its now 4 children
  2. the parent "Plats cuisinés" gets updated with its now only 1 child "test 2" (but the value passed of "test 2" has only the 3 original children, not the now 4)

this.$emit("input", this.localValue);
},
updateChildValue(value: TreeItem): void {
Expand Down