Skip to content

Commit c9db800

Browse files
Fixing range inconsistency when using footer
1 parent b7b5501 commit c9db800

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

example/components/two-list-headerslots.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
role="group"
4343
aria-label="Basic example"
4444
>
45-
<button class="btn btn-secondary" @click="add">Add</button>
46-
<button class="btn btn-secondary" @click="replace">Replace</button>
45+
<button class="btn btn-secondary" @click="add2">Add</button>
46+
<button class="btn btn-secondary" @click="replace2">Replace</button>
4747
</div>
4848
</draggable>
4949
</div>
@@ -58,7 +58,7 @@
5858
import draggable from "@/vuedraggable";
5959
let id = 1;
6060
export default {
61-
name: "headerslots",
61+
name: "two-list-headerslots",
6262
display: "Two list header slot",
6363
order: 13,
6464
components: {
@@ -80,8 +80,15 @@ export default {
8080
},
8181
replace: function() {
8282
this.list = [{ name: "Edgard", id: id++ }];
83+
},
84+
add2: function() {
85+
this.list2.push({ name: "Juan " + id, id: id++ });
86+
},
87+
replace2: function() {
88+
this.list2 = [{ name: "Edgard", id: id++ }];
8389
}
8490
}
8591
};
8692
</script>
87-
<style scoped></style>
93+
<style scoped>
94+
</style>

src/vuedraggable.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ function computeVmIndex(vnodes, element) {
2828
return vnodes.map(elt => elt.elm).indexOf(element);
2929
}
3030

31-
function computeIndexes(slots, children, isTransition) {
31+
function computeIndexes(slots, children, isTransition, footerOffset) {
3232
if (!slots) {
3333
return [];
3434
}
3535

3636
const elmFromNodes = slots.map(elt => elt.elm);
37-
const rawIndexes = [...children].map(elt => elmFromNodes.indexOf(elt));
37+
const footerIndex = children.length - footerOffset;
38+
const rawIndexes = [...children].map((elt, idx) =>
39+
idx >= footerIndex ? elmFromNodes.length : elmFromNodes.indexOf(elt)
40+
);
3841
return isTransition ? rawIndexes.filter(ind => ind !== -1) : rawIndexes;
3942
}
4043

@@ -140,16 +143,19 @@ const draggableComponent = {
140143
}
141144
}
142145
let headerOffset = 0;
146+
let footerOffset = 0;
143147
let children = slots;
144148
const { header, footer } = this.$slots;
145149
if (header) {
146150
headerOffset = header.length;
147151
children = children ? [...header, ...children] : [...header];
148152
}
149153
if (footer) {
154+
footerOffset = footer.length;
150155
children = children ? [...children, ...footer] : [...footer];
151156
}
152157
this.headerOffset = headerOffset;
158+
this.footerOffset = footerOffset;
153159
var attributes = null;
154160
const update = (name, value) => {
155161
attributes = buildAttribute(attributes, name, value);
@@ -295,7 +301,8 @@ const draggableComponent = {
295301
this.visibleIndexes = computeIndexes(
296302
this.getChildrenNodes(),
297303
this.rootContainer.children,
298-
this.transitionMode
304+
this.transitionMode,
305+
this.footerOffset
299306
);
300307
});
301308
},

tests/unit/vuedraggable.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ describe("draggable.vue when initialized with list", () => {
207207

208208
it("computes indexes", async () => {
209209
await Vue.nextTick();
210-
//To be altered
211-
expect(vm.visibleIndexes).toEqual([-1, 0, 1, 2, -1]);
210+
expect(vm.visibleIndexes).toEqual([-1, 0, 1, 2, 3]);
212211
});
213212

214213
it("update indexes", async () => {

0 commit comments

Comments
 (0)