Skip to content

Commit 2802825

Browse files
committed
Async onDropValidators, disabled drag/drop
Fixes #22 and #24
1 parent f888c4a commit 2802825

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

dev/serve.vue

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import json from './tree.json';
33
import Vue3TreeVue from '@/tree-component.vue';
44
import { ref } from '@vue/reactivity';
55
import { defineComponent } from '@vue/runtime-core';
6-
import { TreeViewItem } from '@/types';
6+
import { TreeViewItem, IsValidDropCallback } from '@/types';
77
88
export default defineComponent({
99
name: 'ServeDev',
@@ -16,13 +16,19 @@ export default defineComponent({
1616
1717
const onItemChecked = (checkedItems: TreeViewItem[]) => console.table(checkedItems);
1818
const onItemSelected = (item: TreeViewItem) => console.log(item);
19-
19+
20+
const makeApiCallToSeeIfDropIsValid: IsValidDropCallback = async (_source, _destination) => {
21+
await fetch('www.wikipedia.com').then(() => true);
22+
return true;
23+
}
24+
2025
return {
2126
isCheckable: ref(true),
2227
hideGuidelines: ref(false),
2328
items,
2429
onItemChecked,
25-
onItemSelected
30+
onItemSelected,
31+
makeApiCallToSeeIfDropIsValid
2632
}
2733
}
2834
});
@@ -48,7 +54,7 @@ export default defineComponent({
4854
:isCheckable="isCheckable"
4955
:hideGuideLines="hideGuidelines"
5056
:lazy-load="false"
51-
@dropValidator="(_, __) => true"
57+
@dropValidator="makeApiCallToSeeIfDropIsValid"
5258
@onSelect="onItemSelected"
5359
@onCheck="onItemChecked"
5460
style="width: 800px; display: block; border-right: 1px solid gray;">

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue3-tree-vue",
3-
"version": "2.0.10",
3+
"version": "2.0.11",
44
"description": "",
55
"license": "MIT",
66
"repository": {

src/composables/use-tree-mouse-actions.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,26 @@ export function useTreeViewItemMouseActions() {
4343
if (event.dataTransfer) {
4444
removeHoverClass(event)
4545
const droppedNode = JSON.parse(event.dataTransfer.getData('text/plain')) as _InternalItem;
46-
if (!isDropValid || !isDropValid(droppedNode, dropHost)) return;
4746

47+
if (!isDropValid) return;
4848

49-
5049
if (dropHost && droppedNode.id === dropHost.id) {
5150
return
5251
}
53-
54-
state!.detach(droppedNode.id);
5552

56-
if (dropHost && !dropHost.children)
57-
dropHost.children = [];
53+
isDropValid(droppedNode, dropHost).then((canDrop) => {
54+
if (canDrop) {
55+
state!.detach(droppedNode.id);
5856

59-
if(dropHost)
60-
dropHost!.children!.push(droppedNode);
61-
else
62-
state!.attach(droppedNode);// Dropping into root
57+
if (dropHost && !dropHost.children)
58+
dropHost.children = [];
59+
60+
if(dropHost)
61+
dropHost!.children!.push(droppedNode);
62+
else
63+
state!.attach(droppedNode);// Dropping into root
64+
}
65+
});
6366
}
6467
}
6568

src/tree-component.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<div class="vue3-tree-vue">
33
<ul id="explorer" class="explorer tree-item-node-parent"
44
:class="{'no-guide': hideGuideLines}"
5+
:draggable="onDropValidator != undefined"
56
@dragover.stop.prevent
67
@dragenter.stop.prevent
78
@dragover.stop="addRootHoverClass($event, parent == null)"

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ export interface TreeEvents {
3333
updateSingleSelectedItem(): void;
3434
}
3535

36-
export type IsValidDropCallback = (droppedItem: TreeViewItem, dropHost: TreeViewItem | undefined) => boolean;
36+
export type IsValidDropCallback = (droppedItem: TreeViewItem, dropHost: TreeViewItem | undefined) => Promise<boolean>;

0 commit comments

Comments
 (0)