Skip to content

Commit

Permalink
Fix for issue #633
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Desmaisons committed Jun 23, 2019
1 parent 8a0015a commit d4da343
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
109 changes: 109 additions & 0 deletions example/debug-components/slot-example.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<template>
<div class="row">
<div class="col-2">
<div class="form-group">
<div
class="btn-group-vertical buttons"
role="group"
aria-label="Basic example"
>
<button
class="btn btn-secondary"
@click="add"
>Add</button>
<button
class="btn btn-secondary"
@click="remove"
>Remove</button>
<button
class="btn btn-secondary"
@click="clear"
>Clear</button>
</div>
</div>
</div>

<div class="col-6">
<h3>Draggable</h3>

<draggable
:list="list"
:disabled="!enabled"
class="list-group"
ghost-class="ghost"
>
<div
class="list-group-item"
v-for="element in list"
:key="element.name"
>
{{ element.name }}
</div>

<template v-slot:header>
<div>
header slot
</div>
</template>

<template v-slot:footer>
<div>
footer slot
</div>
</template>
</draggable>
</div>

<rawDisplayer
class="col-3"
:value="list"
title="List"
/>
</div>
</template>

<script>
import draggable from "@/vuedraggable";
let id = 1;
export default {
name: "slot-example",
display: "Slot example",
order: 1,
debug: true,
components: {
draggable
},
data() {
return {
enabled: true,
list: [
{ name: "John", id: 0 },
{ name: "Joao", id: 1 },
{ name: "Jean", id: 2 }
]
};
},
methods: {
clear: function() {
this.list = [];
},
add: function() {
this.list.push({ name: "Juan " + id, id: id++ });
},
remove: function() {
this.list.pop();
}
}
};
</script>
<style scoped>
.buttons {
margin-top: 35px;
}
.ghost {
opacity: 0.5;
background: #c8ebfb;
}
</style>
11 changes: 9 additions & 2 deletions src/vuedraggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,19 @@ function isTransition(slots) {
return isTransitionName(componentOptions.tag);
}

function computeChildrenAndOffsets(children, { header, footer }) {
function getSlot(slot, scopedSlot, key) {
return slot[key] || (scopedSlot[key] ? scopedSlot[key]() : undefined);
}

function computeChildrenAndOffsets(children, slot, scopedSlot) {
let headerOffset = 0;
let footerOffset = 0;
const header = getSlot(slot, scopedSlot, "header");
if (header) {
headerOffset = header.length;
children = children ? [...header, ...children] : [...header];
}
const footer = getSlot(slot, scopedSlot, "footer");
if (footer) {
footerOffset = footer.length;
children = children ? [...children, ...footer] : [...footer];
Expand Down Expand Up @@ -159,7 +165,8 @@ const draggableComponent = {
this.transitionMode = isTransition(slots);
const { children, headerOffset, footerOffset } = computeChildrenAndOffsets(
slots,
this.$slots
this.$slots,
this.$scopedSlots
);
this.headerOffset = headerOffset;
this.footerOffset = footerOffset;
Expand Down

0 comments on commit d4da343

Please sign in to comment.