Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vue] Custom Draggable component example #87

Open
LeaderbotX400 opened this issue Jul 21, 2024 · 0 comments
Open

[Vue] Custom Draggable component example #87

LeaderbotX400 opened this issue Jul 21, 2024 · 0 comments

Comments

@LeaderbotX400
Copy link

LeaderbotX400 commented Jul 21, 2024

Potentially look in to either adding something like the following snippet to the docs, or as a renderless component

This allows for use with things like firebase where realtime updates can casue some wacky behavior if you implement auto update

This example includes type infetence and is generic for users to implement their own list rendering method with v-for

<script setup lang="ts" generic="T">
import { vAutoAnimate } from "@formkit/auto-animate/vue";
import { useDragAndDrop } from "@formkit/drag-and-drop/vue";

const props = defineProps<{
  itemKey?: keyof T;
  handle?: string;
  wrapper?: string;
}>();

const model = defineModel<T[]>({
  default: [],
});

const [parent, items] = useDragAndDrop<T>(model.value, {
  dragHandle: props.handle || undefined,
});

watchDebounced(
  items,
  () => {
    model.value = items.value;
  },
  {
    deep: true,
    debounce: 100,
  }
);
</script>

<template>
  <component :is="wrapper || 'div'" v-if="modelValue" ref="parent" v-auto-animate>
    <template
      v-for="item of items"
      :key="typeof item === 'object' ? item[itemKey] : item"
    >
      <slot :item />
    </template>
  </component>
</template>

Usage:

<script setup lang="ts">
const data = ref(Array(10).map((i) => `item ${i}`));
</script>

<template>
  <Draggable v-model="data" v-slot="{ item }">
    <li>
       {{ item }}
    </li>
  </Draggable>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant