Skip to content

Commit 58efca0

Browse files
crisbetojelbourn
authored andcommitted
refactor(drag-drop): allow single instance/id to be passed to connectedTo (#12811)
Something that I ran into while writing docs. Currently if a non-array is passed to `connectedTo`, a vague error will be thrown. These changes coerce single values into an array to make it more convenient.
1 parent 1840008 commit 58efca0

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/cdk/drag-drop/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ng_module(
1414
"//src/cdk/platform",
1515
"//src/cdk/overlay",
1616
"//src/cdk/bidi",
17+
"//src/cdk/coercion",
1718
],
1819
tsconfig = "//src/cdk:tsconfig-build.json",
1920
)

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,36 @@ describe('CdkDrag', () => {
11511151
});
11521152
}));
11531153

1154+
it('should be able to pass a single id to `connectedTo`', fakeAsync(() => {
1155+
const fixture = createComponent(ConnectedDropZones);
1156+
fixture.detectChanges();
1157+
1158+
const dropInstances = fixture.componentInstance.dropInstances.toArray();
1159+
1160+
dropInstances[1].id = 'done';
1161+
dropInstances[0].connectedTo = ['done'];
1162+
fixture.detectChanges();
1163+
1164+
const groups = fixture.componentInstance.groupedDragItems;
1165+
const element = groups[0][1].element.nativeElement;
1166+
const targetRect = groups[1][2].element.nativeElement.getBoundingClientRect();
1167+
1168+
dragElementViaMouse(fixture, element, targetRect.left + 1, targetRect.top + 1);
1169+
flush();
1170+
fixture.detectChanges();
1171+
1172+
const event = fixture.componentInstance.droppedSpy.calls.mostRecent().args[0];
1173+
1174+
expect(event).toBeTruthy();
1175+
expect(event).toEqual({
1176+
previousIndex: 1,
1177+
currentIndex: 3,
1178+
item: groups[0][1],
1179+
container: dropInstances[1],
1180+
previousContainer: dropInstances[0]
1181+
});
1182+
}));
1183+
11541184
});
11551185

11561186
});

src/cdk/drag-drop/drop.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
QueryList,
2121
ViewEncapsulation,
2222
} from '@angular/core';
23+
import {coerceArray} from '@angular/cdk/coercion';
2324
import {CdkDrag} from './drag';
2425
import {CdkDragExit, CdkDragEnter, CdkDragDrop} from './drag-events';
2526
import {CDK_DROP_CONTAINER} from './drop-container';
@@ -56,7 +57,7 @@ export class CdkDrop<T = any> implements OnInit, OnDestroy {
5657
* container's items can be transferred. Can either be references to other drop containers,
5758
* or their unique IDs.
5859
*/
59-
@Input() connectedTo: (CdkDrop | string)[] = [];
60+
@Input() connectedTo: (CdkDrop | string)[] | CdkDrop | string = [];
6061

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

319-
this._positionCache.siblings = this.connectedTo
320+
this._positionCache.siblings = coerceArray(this.connectedTo)
320321
.map(drop => typeof drop === 'string' ? this._dragDropRegistry.getDropContainer(drop)! : drop)
321322
.filter(drop => drop && drop !== this)
322323
.map(drop => ({drop, clientRect: drop.element.nativeElement.getBoundingClientRect()}));

0 commit comments

Comments
 (0)