Skip to content

Commit a327e46

Browse files
Fix dragging bug
1 parent 4a1ec99 commit a327e46

3 files changed

Lines changed: 65 additions & 11 deletions

File tree

addon/components/drag-sort-item.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{{if this._isDragged '-isDragged'}}
88
{{if this.shouldShowPlaceholderBefore '-placeholderBefore'}}
99
{{if this.shouldShowPlaceholderAfter '-placeholderAfter'}}"
10-
draggable="{{this.draggable}}"
10+
draggable={{this.draggable}}
1111
{{did-insert this.elementInserted}}
1212
{{on "dragstart" this.dragStart}}
1313
{{on "dragend" this.dragEnd}}

addon/services/drag-sort.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,10 @@ export default class DragSort extends Service.extend(EventedMixin) {
3737
sourceList: items,
3838

3939
targetArgs: additionalArgs,
40-
targetIndex: index,
40+
targetIndex: null,
4141
targetList: items,
4242
});
4343

44-
if (items.length > 1) {
45-
if (index === 0) {
46-
this.targetIndex = index + 1;
47-
this.isDraggingUp = true;
48-
} else {
49-
this.targetIndex = index - 1;
50-
}
51-
}
52-
5344
next(() => {
5445
this.trigger('start', {
5546
group,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { module, test } from 'qunit';
2+
import { setupRenderingTest } from 'ember-qunit';
3+
import { findAll, render, triggerEvent } from '@ember/test-helpers';
4+
import hbs from 'htmlbars-inline-precompile';
5+
import { A } from '@ember/array';
6+
7+
module(
8+
'Integration | Component | drag-sort targetIndex regression test',
9+
function (hooks) {
10+
setupRenderingTest(hooks);
11+
12+
test('service targetIndex should not be set immediately on dragstart', async function (assert) {
13+
// This test prevents regression of the bug where targetIndex was being set
14+
// immediately on dragstart, which interfered with dragover events in modern Ember
15+
16+
const items = A([
17+
{ name: 'Item 1' },
18+
{ name: 'Item 2' },
19+
{ name: 'Item 3' },
20+
]);
21+
22+
this.setProperties({ items });
23+
24+
await render(hbs`
25+
<DragSortList
26+
@items={{this.items}}
27+
@group="test"
28+
as |item|
29+
>
30+
<div class="the-item">{{item.name}}</div>
31+
</DragSortList>
32+
`);
33+
34+
const dragSort = this.owner.lookup('service:drag-sort');
35+
const itemElements = findAll('.dragSortItem');
36+
37+
// Initial state - targetIndex should be null
38+
assert.strictEqual(
39+
dragSort.targetIndex,
40+
null,
41+
'targetIndex should initially be null',
42+
);
43+
44+
// Start dragging the second item (index 1)
45+
await triggerEvent(itemElements[1], 'dragstart', {
46+
dataTransfer: new DataTransfer(),
47+
});
48+
49+
// CRITICAL: targetIndex should remain null after dragstart
50+
// It should only be set later during actual dragover events
51+
// The bug was setting targetIndex = index - 1 immediately, which caused
52+
// CSS class conflicts and prevented dragover events from firing properly
53+
assert.strictEqual(
54+
dragSort.targetIndex,
55+
null,
56+
'targetIndex should remain null after dragstart - it should only be set during dragover',
57+
);
58+
59+
// Clean up
60+
await triggerEvent(itemElements[1], 'dragend');
61+
});
62+
},
63+
);

0 commit comments

Comments
 (0)