Skip to content

Commit 89b5fa8

Browse files
authored
fix(drag-drop): warn if connected container ID doesn't exist (#20057)
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 316141f commit 89b5fa8

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
@@ -5280,6 +5280,24 @@ describe('CdkDrag', () => {
52805280
}).not.toThrow();
52815281
}));
52825282

5283+
it('should warn when the connected container ID does not exist', fakeAsync(() => {
5284+
const fixture = createComponent(ConnectedDropZones);
5285+
fixture.detectChanges();
5286+
5287+
fixture.componentInstance.dropInstances.first.connectedTo = 'does-not-exist';
5288+
fixture.detectChanges();
5289+
5290+
const groups = fixture.componentInstance.groupedDragItems;
5291+
const element = groups[0][1].element.nativeElement;
5292+
5293+
spyOn(console, 'warn');
5294+
dragElementViaMouse(fixture, element, 0, 0);
5295+
flush();
5296+
fixture.detectChanges();
5297+
5298+
expect(console.warn).toHaveBeenCalledWith(`CdkDropList could not find connected drop ` +
5299+
`list with id "does-not-exist"`);
5300+
}));
52835301
});
52845302

52855303
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)