Skip to content
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

fix(cdk/drag-drop): sorted event emitted multiple times for single-item list #23589

Merged
merged 1 commit into from
Sep 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,29 @@ describe('CdkDrag', () => {
flush();
}));

it('should not dispatch the `sorted` event when an item is dragged inside ' +
'a single-item list', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.componentInstance.items = [fixture.componentInstance.items[0]];
fixture.detectChanges();

const draggedItem = fixture.componentInstance.dragItems.first.element.nativeElement;
const {top, left} = draggedItem.getBoundingClientRect();

startDraggingViaMouse(fixture, draggedItem, left, top);

for (let i = 0; i < 5; i++) {
dispatchMouseEvent(document, 'mousemove', left, top + 1);
fixture.detectChanges();

expect(fixture.componentInstance.sortedSpy).not.toHaveBeenCalled();
}

dispatchMouseEvent(document, 'mouseup');
fixture.detectChanges();
flush();
}));

it('should not move items in a vertical list if the pointer is too far away', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
Expand Down
7 changes: 3 additions & 4 deletions src/cdk/drag-drop/drop-list-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,11 +754,10 @@ export class DropListRef<T = any> {
private _getItemIndexFromPointerPosition(item: DragRef, pointerX: number, pointerY: number,
delta?: {x: number, y: number}): number {
const isHorizontal = this._orientation === 'horizontal';
const index = this._itemPositions.findIndex(({drag, clientRect}, _, array) => {
const index = this._itemPositions.findIndex(({drag, clientRect}) => {
// Skip the item itself.
if (drag === item) {
// If there's only one item left in the container, it must be
// the dragged item itself so we use it as a reference.
return array.length < 2;
return false;
}

if (delta) {
Expand Down