From 664ef4c9d30deb8b814a9f25b378073dbda2d772 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 12 Sep 2018 00:12:47 +0200 Subject: [PATCH] fix(drag-drop): incorrectly calculating index when sorting horizontally (#13082) Fixes the list index not being calculated correctly, because the items are being sorted in memory based on their `top` position, even though the dragging is horizontal. Fixes #13072. --- src/cdk/drag-drop/drop.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cdk/drag-drop/drop.ts b/src/cdk/drag-drop/drop.ts index a62be073777..b86073a3f3d 100644 --- a/src/cdk/drag-drop/drop.ts +++ b/src/cdk/drag-drop/drop.ts @@ -314,6 +314,7 @@ export class CdkDrop implements OnInit, OnDestroy { /** Refreshes the position cache of the items and sibling containers. */ private _cachePositions() { + const isHorizontal = this.orientation === 'horizontal'; this._positionCache.items = this._activeDraggables .map(drag => { const elementToMeasure = this._dragDropRegistry.isDragging(drag) ? @@ -340,7 +341,10 @@ export class CdkDrop implements OnInit, OnDestroy { } }; }) - .sort((a, b) => a.clientRect.top - b.clientRect.top); + .sort((a, b) => { + return isHorizontal ? a.clientRect.left - b.clientRect.left : + a.clientRect.top - b.clientRect.top; + }); this._positionCache.siblings = coerceArray(this.connectedTo) .map(drop => typeof drop === 'string' ? this._dragDropRegistry.getDropContainer(drop)! : drop)