Skip to content

Commit 63f18a7

Browse files
committed
Watch options for changes; update demo with example
1 parent dc1738f commit 63f18a7

File tree

2 files changed

+73
-56
lines changed

2 files changed

+73
-56
lines changed

src/components/HelloWorld.vue

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,67 @@
11
<script setup lang="ts">
22
import Sortable from "./Sortable.vue";
3-
import { computed } from 'vue'
3+
import { computed, ref } from "vue";
44
import type { SortableOptions } from "sortablejs";
55
66
const elements = computed(() => {
7-
return [
7+
return [
8+
{
9+
id: "1",
10+
text: "One",
11+
children: [
812
{
9-
id: '1',
10-
text: 'One',
11-
children: [
12-
{
13-
id: '1-1',
14-
text: 'One-One',
15-
children: [
16-
{
17-
id: '1-1-1',
18-
text: 'One-One-One',
19-
},
20-
{
21-
id: '1-1-2',
22-
text: 'One-One-Two',
23-
},
24-
],
25-
},
26-
{
27-
id: '1-2',
28-
text: 'One-Two',
29-
},
30-
],
13+
id: "1-1",
14+
text: "One-One",
15+
children: [
16+
{
17+
id: "1-1-1",
18+
text: "One-One-One",
19+
},
20+
{
21+
id: "1-1-2",
22+
text: "One-One-Two",
23+
},
24+
],
3125
},
3226
{
33-
id: '2',
34-
text: 'Two'
27+
id: "1-2",
28+
text: "One-Two",
3529
},
36-
{
37-
id: '3',
38-
text: 'Three'
39-
}
40-
]
41-
})
30+
],
31+
},
32+
{
33+
id: "2",
34+
text: "Two",
35+
},
36+
{
37+
id: "3",
38+
text: "Three",
39+
},
40+
];
41+
});
4242
4343
const logEvent = (evt: Event, evt2?: Event) => {
44-
if (evt2) {
45-
console.log(evt, evt2);
46-
} else {
47-
console.log(evt);
48-
}
49-
}
44+
if (evt2) {
45+
console.log(evt, evt2);
46+
} else {
47+
console.log(evt);
48+
}
49+
};
50+
51+
const animating = ref(true);
5052
51-
const options = computed(() => {
53+
const options = computed<SortableOptions>(() => {
5254
return {
53-
draggable: '.draggable',
54-
animation: 150,
55-
ghostClass: 'ghost',
56-
dragClass: 'drag',
57-
} as SortableOptions
58-
})
55+
draggable: ".draggable",
56+
animation: animating.value ? 150 : 0,
57+
ghostClass: "ghost",
58+
dragClass: "drag",
59+
};
60+
});
61+
62+
const onPress = (evt: Event) => {
63+
animating.value = !animating.value;
64+
};
5965
</script>
6066

6167
<style lang="css" scoped>
@@ -85,6 +91,7 @@ main {
8591

8692
<template>
8793
<main>
94+
<button @click="onPress">Toggle animations</button>
8895
<Sortable
8996
:list="elements"
9097
item-key="id"
@@ -102,7 +109,7 @@ main {
102109
@move="logEvent"
103110
@clone="logEvent"
104111
>
105-
<template #item="{element, index}">
112+
<template #item="{ element, index }">
106113
<div class="draggable" :key="element.id">
107114
{{ element.text }}
108115
<Sortable
@@ -123,7 +130,7 @@ main {
123130
@move="logEvent"
124131
@clone="logEvent"
125132
>
126-
<template #item="{element, index}">
133+
<template #item="{ element, index }">
127134
<div class="draggable" :key="element.id">
128135
{{ element.text }}
129136
</div>
@@ -135,6 +142,4 @@ main {
135142
</main>
136143
</template>
137144

138-
<style scoped>
139-
140-
</style>
145+
<style scoped></style>

src/components/Sortable.vue

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ref, PropType, watch, onUnmounted, Prop } from "vue";
33
import Sortable, { SortableOptions } from "sortablejs";
44
55
const props = defineProps({
6+
/** All SortableJS options are supported; events are handled by the `defineEmits` below and should be used with v-on */
67
options: {
78
type: Object as PropType<
89
Omit<
@@ -24,21 +25,24 @@ const props = defineProps({
2425
default: null,
2526
required: false,
2627
},
28+
/** Your list of items **/
2729
list: {
2830
type: Array as PropType<any[]>,
2931
default: [],
3032
required: true,
3133
},
34+
/** The name of the key present in each item in the list that corresponds to a unique value. */
3235
itemKey: {
3336
type: String as PropType<string>,
3437
default: "",
3538
required: true,
3639
},
40+
/** The element type to render as (string or function). */
3741
tag: {
3842
type: String as PropType<string>,
3943
default: "div",
4044
required: false,
41-
}
45+
},
4246
});
4347
4448
const emit = defineEmits<{
@@ -79,6 +83,18 @@ watch(containerRef, (newDraggable) => {
7983
}
8084
});
8185
86+
watch(
87+
() => props.options,
88+
(options) => {
89+
if (options && sortable?.value) {
90+
sortable.value.options = { ...sortable.value.options, ...options };
91+
}
92+
},
93+
{
94+
deep: true,
95+
}
96+
);
97+
8298
onUnmounted(() => {
8399
if (sortable.value) {
84100
sortable.value.destroy();
@@ -89,11 +105,7 @@ onUnmounted(() => {
89105
</script>
90106

91107
<template>
92-
<component
93-
ref="containerRef"
94-
:is="$props.tag"
95-
:class="$props.class"
96-
>
108+
<component ref="containerRef" :is="$props.tag" :class="$props.class">
97109
<slot
98110
v-for="(item, index) of list"
99111
:key="item[$props.itemKey!]"

0 commit comments

Comments
 (0)