Skip to content

fix(drag-drop): error when item enters from the top and list has an intermediate child #19361

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

Merged
merged 1 commit into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4331,6 +4331,29 @@ describe('CdkDrag', () => {
dispatchMouseEvent(document, 'mouseup');
}));

it('should not throw when entering from the top with an intermediate sibling present',
fakeAsync(() => {
const fixture = createComponent(ConnectedDropZonesWithIntermediateSibling);

// Make sure there's only one item in the first list.
fixture.componentInstance.todo = ['things'];
fixture.detectChanges();

const groups = fixture.componentInstance.groupedDragItems;
const dropZones = fixture.componentInstance.dropInstances.map(d => d.element.nativeElement);
const item = groups[0][0];

// Add some initial padding as the target drop zone
dropZones[1].style.paddingTop = '10px';
const targetRect = dropZones[1].getBoundingClientRect();

expect(() => {
dragElementViaMouse(fixture, item.element.nativeElement, targetRect.left, targetRect.top);
flush();
fixture.detectChanges();
}).not.toThrow();
}));

it('should assign a default id on each drop zone', fakeAsync(() => {
const fixture = createComponent(ConnectedDropZones);
fixture.detectChanges();
Expand Down Expand Up @@ -6051,6 +6074,46 @@ class DraggableInHorizontalFlexDropZoneWithMatchSizePreview {
}


@Component({
styles: CONNECTED_DROP_ZONES_STYLES,
template: `
<div
cdkDropList
#todoZone="cdkDropList"
[cdkDropListData]="todo"
[cdkDropListConnectedTo]="[doneZone]"
(cdkDropListDropped)="droppedSpy($event)"
(cdkDropListEntered)="enteredSpy($event)">
<div
[cdkDragData]="item"
(cdkDragEntered)="itemEnteredSpy($event)"
*ngFor="let item of todo"
cdkDrag>{{item}}</div>
</div>

<div
cdkDropList
#doneZone="cdkDropList"
[cdkDropListData]="done"
[cdkDropListConnectedTo]="[todoZone]"
(cdkDropListDropped)="droppedSpy($event)"
(cdkDropListEntered)="enteredSpy($event)">

<div>Hello there</div>
<div>
<div
[cdkDragData]="item"
(cdkDragEntered)="itemEnteredSpy($event)"
*ngFor="let item of done"
cdkDrag>{{item}}</div>
</div>
</div>
`
})
class ConnectedDropZonesWithIntermediateSibling extends ConnectedDropZones {
}


/**
* Drags an element to a position on the page using the mouse.
* @param fixture Fixture on which to run change detection.
Expand Down
14 changes: 6 additions & 8 deletions src/cdk/drag-drop/drop-list-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,13 @@ export class DropListRef<T = any> {
const element = newPositionReference.getRootElement();
element.parentElement!.insertBefore(placeholder, element);
activeDraggables.splice(newIndex, 0, item);
} else if (this._shouldEnterAsFirstChild(pointerX, pointerY)) {
const reference = activeDraggables[0].getRootElement();
reference.parentNode!.insertBefore(placeholder, reference);
activeDraggables.unshift(item);
} else {
const element = coerceElement(this.element);
if (this._shouldEnterAsFirstChild(pointerX, pointerY)) {
element.insertBefore(placeholder, activeDraggables[0].getRootElement());
activeDraggables.unshift(item);
} else {
element.appendChild(placeholder);
activeDraggables.push(item);
}
coerceElement(this.element).appendChild(placeholder);
activeDraggables.push(item);
}

// The transform needs to be cleared so it doesn't throw off the measurements.
Expand Down