Skip to content

Commit 07cadcf

Browse files
[feat]: transition-group compatibility
1 parent faafc3f commit 07cadcf

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

src/components/Sortable.vue

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<script setup lang="ts" generic="GItem">
22
import {
33
ref,
4-
PropType,
54
watch,
65
onUnmounted,
76
computed,
87
useAttrs,
9-
Ref,
108
} from "vue";
11-
import Sortable, { SortableOptions } from "sortablejs";
9+
import type { PropType, Ref, ComponentPublicInstance } from "vue";
10+
import Sortable from "sortablejs";
11+
import type { SortableOptions } from "sortablejs";
1212
import type { AutoScrollOptions } from "sortablejs/plugins";
1313
1414
type SortableOptionsProp = Omit<
@@ -28,7 +28,7 @@ type SortableOptionsProp = Omit<
2828
>;
2929
3030
type ExposedProps = {
31-
containerRef: Ref<HTMLDivElement | null>;
31+
containerRef: Ref<HTMLDivElement | ComponentPublicInstance | null>;
3232
sortable: Ref<Sortable | null>;
3333
isDragging: Ref<boolean>;
3434
};
@@ -60,6 +60,11 @@ const props = defineProps({
6060
default: "div",
6161
required: false,
6262
},
63+
groupTag: {
64+
type: String,
65+
default: "div",
66+
required: false,
67+
},
6368
});
6469
6570
const emit = defineEmits<{
@@ -80,7 +85,7 @@ const emit = defineEmits<{
8085
const attrs = useAttrs();
8186
8287
const isDragging = ref(false);
83-
const containerRef = ref<HTMLElement | null>(null);
88+
const containerRef = ref<HTMLElement | ComponentPublicInstance | null>(null);
8489
const sortable = ref<Sortable | null>(null);
8590
const getKey = computed(() => {
8691
if (typeof props.itemKey === "string")
@@ -95,8 +100,16 @@ defineExpose({
95100
} as ExposedProps);
96101
97102
watch(containerRef, (newDraggable) => {
103+
let el: HTMLElement | null = null;
98104
if (newDraggable) {
99-
sortable.value = new Sortable(newDraggable, {
105+
if ("$el" in newDraggable && newDraggable.$el instanceof HTMLElement) {
106+
el = newDraggable.$el;
107+
} else if (newDraggable instanceof HTMLElement) {
108+
el = newDraggable;
109+
}
110+
}
111+
if (el) {
112+
sortable.value = new Sortable(el, {
100113
...props.options,
101114
onChoose: (event) => emit("choose", event),
102115
onUnchoose: (event) => emit("unchoose", event),
@@ -155,7 +168,12 @@ onUnmounted(() => {
155168
</script>
156169

157170
<template>
158-
<component ref="containerRef" :is="$props.tag" :class="$props.class">
171+
<component
172+
v-if="!['transition-group', 'TransitionGroup'].includes(tag)"
173+
ref="containerRef"
174+
:is="tag"
175+
:class="$props.class"
176+
>
159177
<slot name="header"></slot>
160178
<slot
161179
v-for="(item, index) of list"
@@ -166,4 +184,20 @@ onUnmounted(() => {
166184
></slot>
167185
<slot name="footer"></slot>
168186
</component>
187+
<transition-group
188+
v-else
189+
:tag="groupTag"
190+
:class="$props.class"
191+
ref="containerRef"
192+
>
193+
<slot name="header"></slot>
194+
<slot
195+
v-for="(item, index) of list"
196+
:key="getKey(item)"
197+
:element="item"
198+
:index="index"
199+
name="item"
200+
></slot>
201+
<slot name="footer"></slot>
202+
</transition-group>
169203
</template>

0 commit comments

Comments
 (0)