Skip to content

Commit bfacbb5

Browse files
crisbetojelbourn
authored andcommitted
fix(drag-drop): handle placeholder and preview templates changing after init (#14541)
Moves some code around to account for the placeholder and preview templates changing after init.
1 parent 364376e commit bfacbb5

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

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

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,23 @@ describe('CdkDrag', () => {
17761776
expect(preview.textContent!.trim()).toContain('Custom preview');
17771777
}));
17781778

1779+
it('should handle the custom preview being removed', fakeAsync(() => {
1780+
const fixture = createComponent(DraggableInDropZoneWithCustomPreview);
1781+
fixture.detectChanges();
1782+
flush();
1783+
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;
1784+
1785+
fixture.componentInstance.renderCustomPreview = false;
1786+
fixture.detectChanges();
1787+
startDraggingViaMouse(fixture, item);
1788+
1789+
const preview = document.querySelector('.cdk-drag-preview')! as HTMLElement;
1790+
1791+
expect(preview).toBeTruthy();
1792+
expect(preview.classList).not.toContain('custom-preview');
1793+
expect(preview.textContent!.trim()).not.toContain('Custom preview');
1794+
}));
1795+
17791796
it('should be able to constrain the position of a custom preview', fakeAsync(() => {
17801797
const fixture = createComponent(DraggableInDropZoneWithCustomPreview);
17811798
fixture.componentInstance.boundarySelector = '.cdk-drop-list';
@@ -1900,6 +1917,25 @@ describe('CdkDrag', () => {
19001917
expect(placeholder.textContent!.trim()).toContain('Custom placeholder');
19011918
}));
19021919

1920+
it('should handle the custom placeholder being removed', fakeAsync(() => {
1921+
const fixture = createComponent(DraggableInDropZoneWithCustomPlaceholder);
1922+
fixture.detectChanges();
1923+
flush();
1924+
1925+
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;
1926+
1927+
fixture.componentInstance.renderPlaceholder = false;
1928+
fixture.detectChanges();
1929+
1930+
startDraggingViaMouse(fixture, item);
1931+
1932+
const placeholder = document.querySelector('.cdk-drag-placeholder')! as HTMLElement;
1933+
1934+
expect(placeholder).toBeTruthy();
1935+
expect(placeholder.classList).not.toContain('custom-placeholder');
1936+
expect(placeholder.textContent!.trim()).not.toContain('Custom placeholder');
1937+
}));
1938+
19031939
it('should clear the `transform` value from siblings when item is dropped`', fakeAsync(() => {
19041940
const fixture = createComponent(DraggableInDropZone);
19051941
fixture.detectChanges();
@@ -2713,10 +2749,13 @@ class DraggableInHorizontalDropZone {
27132749
[cdkDragBoundary]="boundarySelector"
27142750
style="width: 100%; height: ${ITEM_HEIGHT}px; background: red;">
27152751
{{item}}
2716-
<div
2717-
class="custom-preview"
2718-
style="width: 50px; height: 50px; background: purple;"
2719-
*cdkDragPreview>Custom preview</div>
2752+
2753+
<ng-container *ngIf="renderCustomPreview">
2754+
<div
2755+
class="custom-preview"
2756+
style="width: 50px; height: 50px; background: purple;"
2757+
*cdkDragPreview>Custom preview</div>
2758+
</ng-container>
27202759
</div>
27212760
</div>
27222761
`
@@ -2726,6 +2765,7 @@ class DraggableInDropZoneWithCustomPreview {
27262765
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
27272766
items = ['Zero', 'One', 'Two', 'Three'];
27282767
boundarySelector: string;
2768+
renderCustomPreview = true;
27292769
}
27302770

27312771

@@ -2735,14 +2775,17 @@ class DraggableInDropZoneWithCustomPreview {
27352775
<div *ngFor="let item of items" cdkDrag
27362776
style="width: 100%; height: ${ITEM_HEIGHT}px; background: red;">
27372777
{{item}}
2738-
<div class="custom-placeholder" *cdkDragPlaceholder>Custom placeholder</div>
2778+
<ng-container *ngIf="renderPlaceholder">
2779+
<div class="custom-placeholder" *cdkDragPlaceholder>Custom placeholder</div>
2780+
</ng-container>
27392781
</div>
27402782
</div>
27412783
`
27422784
})
27432785
class DraggableInDropZoneWithCustomPlaceholder {
27442786
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
27452787
items = ['Zero', 'One', 'Two', 'Three'];
2788+
renderPlaceholder = true;
27462789
}
27472790

27482791

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
174174
if (!ref.isDragging()) {
175175
ref.disabled = this.disabled;
176176
ref.lockAxis = this.lockAxis;
177-
ref.withBoundaryElement(this._getBoundaryElement());
177+
ref
178+
.withBoundaryElement(this._getBoundaryElement())
179+
.withPlaceholderTemplate(this._placeholderTemplate)
180+
.withPreviewTemplate(this._previewTemplate);
178181
}
179182
});
180183
this._proxyEvents(ref);
@@ -213,11 +216,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
213216
`Currently attached to "${rootElement.nodeName}".`);
214217
}
215218

216-
this._dragRef
217-
.withRootElement(rootElement)
218-
.withPlaceholderTemplate(this._placeholderTemplate)
219-
.withPreviewTemplate(this._previewTemplate);
220-
219+
this._dragRef.withRootElement(rootElement);
221220
this._handles.changes
222221
.pipe(startWith(this._handles))
223222
.subscribe((handleList: QueryList<CdkDragHandle>) => {

0 commit comments

Comments
 (0)