Skip to content

fix(drag-drop): warn if connected container ID doesn't exist #20057

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
Jul 24, 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
18 changes: 18 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5255,6 +5255,24 @@ describe('CdkDrag', () => {
}).not.toThrow();
}));

it('should warn when the connected container ID does not exist', fakeAsync(() => {
const fixture = createComponent(ConnectedDropZones);
fixture.detectChanges();

fixture.componentInstance.dropInstances.first.connectedTo = 'does-not-exist';
fixture.detectChanges();

const groups = fixture.componentInstance.groupedDragItems;
const element = groups[0][1].element.nativeElement;

spyOn(console, 'warn');
dragElementViaMouse(fixture, element, 0, 0);
flush();
fixture.detectChanges();

expect(console.warn).toHaveBeenCalledWith(`CdkDropList could not find connected drop ` +
`list with id "does-not-exist"`);
}));
});

describe('with nested drags', () => {
Expand Down
14 changes: 12 additions & 2 deletions src/cdk/drag-drop/directives/drop-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SkipSelf,
Inject,
InjectionToken,
isDevMode,
} from '@angular/core';
import {Directionality} from '@angular/cdk/bidi';
import {ScrollDispatcher} from '@angular/cdk/scrolling';
Expand Down Expand Up @@ -252,8 +253,17 @@ export class CdkDropList<T = any> implements OnDestroy {

ref.beforeStarted.subscribe(() => {
const siblings = coerceArray(this.connectedTo).map(drop => {
return typeof drop === 'string' ?
CdkDropList._dropLists.find(list => list.id === drop)! : drop;
if (typeof drop === 'string') {
const correspondingDropList = CdkDropList._dropLists.find(list => list.id === drop);

if (!correspondingDropList && isDevMode()) {
console.warn(`CdkDropList could not find connected drop list with id "${drop}"`);
}

return correspondingDropList!;
}

return drop;
});

if (this._group) {
Expand Down