Skip to content

Instantly share code, notes, and snippets.

@athif23
Created June 13, 2020 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athif23/1b856259df4219374e5ac0df4ece4f0f to your computer and use it in GitHub Desktop.
Save athif23/1b856259df4219374e5ac0df4ece4f0f to your computer and use it in GitHub Desktop.
This is wrapper component for vuedraggable library.
<script>
import draggable from 'vuedraggable'
const vuedraggableProps = {
options: Object,
// list: { type: Array, required: false, default: null }, We wouldn't really need this,
// value: { type: Array, required: false, default: null }, also this.
noTransitionOnDrag: { type: Boolean, default: false },
clone: { type: Function, default: original => (original) },
element: { type: String, default: 'div' },
tag: { type: String, default: null },
move: { type: Function, default: null },
componentData: { type: Object, required: false, default: null }
}
export default {
props: vuedraggableProps
components: { draggable }
data: () => ({
list: [] // You can name this whatever you want
}),
mounted () {
// You can change this `key` variable to whatever you want,
// but it must be unique.
let key = 0
const filtered = this.$slots.default.filter(
vnode => vnode.tag !== undefined
)
this.list = filtered.map(vnode => ({ id: key++, vnode }))
},
methods: {
getProps () {
// Remember to add these to `props` property.
const { options, noTransitionOnDrag, clone, element, tag, move, componentData } = this
return { options, noTransitionOnDrag, clone, element, tag, move, componentData }
}
},
render (h) {
return h('draggable', {
props: { ...this.getProps(), value: this.list },
attrs: { ...this.$attrs },
on: { input: ($event) => { this.list = $event } }
}, this.list.map(el => {
el.vnode.key = el.id
return el.vnode
}))
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment