|
1 | 1 | <script setup lang="ts">
|
2 |
| -import { ref, PropType, watch, onUnmounted, Prop } from "vue"; |
| 2 | +import { ref, PropType, watch, onUnmounted, computed } from "vue"; |
3 | 3 | import Sortable, { SortableOptions } from "sortablejs";
|
4 | 4 |
|
5 | 5 | const props = defineProps({
|
@@ -33,7 +33,9 @@ const props = defineProps({
|
33 | 33 | },
|
34 | 34 | /** The name of the key present in each item in the list that corresponds to a unique value. */
|
35 | 35 | itemKey: {
|
36 |
| - type: String as PropType<string>, |
| 36 | + type: [String, Function] as PropType< |
| 37 | + string | ((item: any) => string | number | Symbol) |
| 38 | + >, |
37 | 39 | default: "",
|
38 | 40 | required: true,
|
39 | 41 | },
|
@@ -62,6 +64,11 @@ const emit = defineEmits<{
|
62 | 64 |
|
63 | 65 | const containerRef = ref<HTMLElement | null>(null);
|
64 | 66 | const sortable = ref<Sortable | null>(null);
|
| 67 | +const getKey = computed(() => { |
| 68 | + if (typeof props.itemKey === "string") |
| 69 | + return (item: any) => item[props.itemKey as string]; |
| 70 | + return props.itemKey; |
| 71 | +}); |
65 | 72 |
|
66 | 73 | watch(containerRef, (newDraggable) => {
|
67 | 74 | if (newDraggable) {
|
@@ -105,7 +112,7 @@ onUnmounted(() => {
|
105 | 112 | <component ref="containerRef" :is="$props.tag" :class="$props.class">
|
106 | 113 | <slot
|
107 | 114 | v-for="(item, index) of list"
|
108 |
| - :key="item[$props.itemKey!]" |
| 115 | + :key="getKey(item)" |
109 | 116 | :element="item"
|
110 | 117 | :index="index"
|
111 | 118 | name="item"
|
|
0 commit comments