Skip to content

refactor(drag-drop): allow single instance/id to be passed to connectedTo #12811

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
Aug 23, 2018
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
1 change: 1 addition & 0 deletions src/cdk/drag-drop/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ng_module(
"//src/cdk/platform",
"//src/cdk/overlay",
"//src/cdk/bidi",
"//src/cdk/coercion",
],
tsconfig = "//src/cdk:tsconfig-build.json",
)
Expand Down
30 changes: 30 additions & 0 deletions src/cdk/drag-drop/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,36 @@ describe('CdkDrag', () => {
});
}));

it('should be able to pass a single id to `connectedTo`', fakeAsync(() => {
const fixture = createComponent(ConnectedDropZones);
fixture.detectChanges();

const dropInstances = fixture.componentInstance.dropInstances.toArray();

dropInstances[1].id = 'done';
dropInstances[0].connectedTo = ['done'];
fixture.detectChanges();

const groups = fixture.componentInstance.groupedDragItems;
const element = groups[0][1].element.nativeElement;
const targetRect = groups[1][2].element.nativeElement.getBoundingClientRect();

dragElementViaMouse(fixture, element, targetRect.left + 1, targetRect.top + 1);
flush();
fixture.detectChanges();

const event = fixture.componentInstance.droppedSpy.calls.mostRecent().args[0];

expect(event).toBeTruthy();
expect(event).toEqual({
previousIndex: 1,
currentIndex: 3,
item: groups[0][1],
container: dropInstances[1],
previousContainer: dropInstances[0]
});
}));

});

});
Expand Down
5 changes: 3 additions & 2 deletions src/cdk/drag-drop/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
QueryList,
ViewEncapsulation,
} from '@angular/core';
import {coerceArray} from '@angular/cdk/coercion';
import {CdkDrag} from './drag';
import {CdkDragExit, CdkDragEnter, CdkDragDrop} from './drag-events';
import {CDK_DROP_CONTAINER} from './drop-container';
Expand Down Expand Up @@ -56,7 +57,7 @@ export class CdkDrop<T = any> implements OnInit, OnDestroy {
* container's items can be transferred. Can either be references to other drop containers,
* or their unique IDs.
*/
@Input() connectedTo: (CdkDrop | string)[] = [];
@Input() connectedTo: (CdkDrop | string)[] | CdkDrop | string = [];

/** Arbitrary data to attach to this container. */
@Input() data: T;
Expand Down Expand Up @@ -316,7 +317,7 @@ export class CdkDrop<T = any> implements OnInit, OnDestroy {
})
.sort((a, b) => a.clientRect.top - b.clientRect.top);

this._positionCache.siblings = this.connectedTo
this._positionCache.siblings = coerceArray(this.connectedTo)
.map(drop => typeof drop === 'string' ? this._dragDropRegistry.getDropContainer(drop)! : drop)
.filter(drop => drop && drop !== this)
.map(drop => ({drop, clientRect: drop.element.nativeElement.getBoundingClientRect()}));
Expand Down