Skip to content

Commit 3b70de9

Browse files
committed
fix(drag-drop): warn if connected container ID doesn't exist
We support connecting two drop lists by their ID, but if the ID is incorrect, we silently ignore it. These changes add a warning so it's easier to debug issues where the ID might be misspelled. Fixes #20056.
1 parent 4a1b24c commit 3b70de9

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/cdk/drag-drop/directives/drag.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5255,6 +5255,24 @@ describe('CdkDrag', () => {
52555255
}).not.toThrow();
52565256
}));
52575257

5258+
it('should warn when the connected container ID does not exist', fakeAsync(() => {
5259+
const fixture = createComponent(ConnectedDropZones);
5260+
fixture.detectChanges();
5261+
5262+
fixture.componentInstance.dropInstances.first.connectedTo = 'does-not-exist';
5263+
fixture.detectChanges();
5264+
5265+
const groups = fixture.componentInstance.groupedDragItems;
5266+
const element = groups[0][1].element.nativeElement;
5267+
5268+
spyOn(console, 'warn');
5269+
dragElementViaMouse(fixture, element, 0, 0);
5270+
flush();
5271+
fixture.detectChanges();
5272+
5273+
expect(console.warn).toHaveBeenCalledWith(`CdkDropList could not find connected drop ` +
5274+
`list with id "does-not-exist"`);
5275+
}));
52585276
});
52595277

52605278
describe('with nested drags', () => {

src/cdk/drag-drop/directives/drop-list.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
SkipSelf,
2020
Inject,
2121
InjectionToken,
22+
isDevMode,
2223
} from '@angular/core';
2324
import {Directionality} from '@angular/cdk/bidi';
2425
import {ScrollDispatcher} from '@angular/cdk/scrolling';
@@ -252,8 +253,17 @@ export class CdkDropList<T = any> implements OnDestroy {
252253

253254
ref.beforeStarted.subscribe(() => {
254255
const siblings = coerceArray(this.connectedTo).map(drop => {
255-
return typeof drop === 'string' ?
256-
CdkDropList._dropLists.find(list => list.id === drop)! : drop;
256+
if (typeof drop === 'string') {
257+
const correspondingDropList = CdkDropList._dropLists.find(list => list.id === drop);
258+
259+
if (!correspondingDropList && isDevMode()) {
260+
console.warn(`CdkDropList could not find connected drop list with id "${drop}"`);
261+
}
262+
263+
return correspondingDropList!;
264+
}
265+
266+
return drop;
257267
});
258268

259269
if (this._group) {

0 commit comments

Comments
 (0)