Skip to content

fix(drag-drop): handle custom preview/placeholder with multiple root nodes #18829

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
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
71 changes: 71 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2978,6 +2978,21 @@ describe('CdkDrag', () => {
expect(preview.textContent!.trim()).toContain('Hello One');
}));

it('should handle custom preview with multiple root nodes', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZoneWithCustomMultiNodePreview);
fixture.detectChanges();
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;

expect(() => {
startDraggingViaMouse(fixture, item);
}).not.toThrow();

const preview = document.querySelector('.cdk-drag-preview')! as HTMLElement;

expect(preview).toBeTruthy();
expect(preview.textContent!.trim()).toContain('HelloOne');
}));

it('should be able to customize the placeholder', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZoneWithCustomPlaceholder);
fixture.detectChanges();
Expand Down Expand Up @@ -3050,6 +3065,21 @@ describe('CdkDrag', () => {
expect(placeholder.textContent!.trim()).toContain('Hello One');
}));

it('should handle custom placeholder with multiple root nodes', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZoneWithCustomMultiNodePlaceholder);
fixture.detectChanges();
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;

expect(() => {
startDraggingViaMouse(fixture, item);
}).not.toThrow();

const placeholder = document.querySelector('.cdk-drag-placeholder')! as HTMLElement;

expect(placeholder).toBeTruthy();
expect(placeholder.textContent!.trim()).toContain('HelloOne');
}));

it('should clear the `transform` value from siblings when item is dropped`', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
Expand Down Expand Up @@ -5192,6 +5222,28 @@ class DraggableInDropZoneWithCustomTextOnlyPreview {
}


@Component({
template: `
<div cdkDropList style="width: 100px; background: pink;">
<div
*ngFor="let item of items"
cdkDrag
style="width: 100%; height: ${ITEM_HEIGHT}px; background: red;">
{{item}}
<ng-template cdkDragPreview>
<span>Hello</span>
<span>{{item}}</span>
</ng-template>
</div>
</div>
`
})
class DraggableInDropZoneWithCustomMultiNodePreview {
@ViewChild(CdkDropList) dropInstance: CdkDropList;
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
items = ['Zero', 'One', 'Two', 'Three'];
}

@Component({
template: `
<div
Expand Down Expand Up @@ -5240,6 +5292,25 @@ class DraggableInDropZoneWithCustomTextOnlyPlaceholder {
items = ['Zero', 'One', 'Two', 'Three'];
}

@Component({
template: `
<div cdkDropList style="width: 100px; background: pink;">
<div *ngFor="let item of items" cdkDrag
style="width: 100%; height: ${ITEM_HEIGHT}px; background: red;">
{{item}}
<ng-template cdkDragPlaceholder>
<span>Hello</span>
<span>{{item}}</span>
</ng-template>
</div>
</div>
`
})
class DraggableInDropZoneWithCustomMultiNodePlaceholder {
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
items = ['Zero', 'One', 'Two', 'Three'];
}

const CONNECTED_DROP_ZONES_STYLES = [`
.cdk-drop-list {
display: block;
Expand Down
12 changes: 6 additions & 6 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1297,15 +1297,15 @@ function getPreviewInsertionPoint(documentRef: any): HTMLElement {
* If the root is not an HTML element it gets wrapped in one.
*/
function getRootNode(viewRef: EmbeddedViewRef<any>, _document: Document): HTMLElement {
const rootNode: Node = viewRef.rootNodes[0];
const rootNodes: Node[] = viewRef.rootNodes;

if (rootNode.nodeType !== _document.ELEMENT_NODE) {
const wrapper = _document.createElement('div');
wrapper.appendChild(rootNode);
return wrapper;
if (rootNodes.length === 1 && rootNodes[0].nodeType === _document.ELEMENT_NODE) {
return rootNodes[0] as HTMLElement;
}

return rootNode as HTMLElement;
const wrapper = _document.createElement('div');
rootNodes.forEach(node => wrapper.appendChild(node));
return wrapper;
}

/**
Expand Down