Skip to content

fix(drag-drop): handle placeholder and preview templates changing after init #14541

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
Dec 18, 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
53 changes: 48 additions & 5 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,23 @@ describe('CdkDrag', () => {
expect(preview.textContent!.trim()).toContain('Custom preview');
}));

it('should handle the custom preview being removed', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZoneWithCustomPreview);
fixture.detectChanges();
flush();
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;

fixture.componentInstance.renderCustomPreview = false;
fixture.detectChanges();
startDraggingViaMouse(fixture, item);

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

expect(preview).toBeTruthy();
expect(preview.classList).not.toContain('custom-preview');
expect(preview.textContent!.trim()).not.toContain('Custom preview');
}));

it('should be able to constrain the position of a custom preview', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZoneWithCustomPreview);
fixture.componentInstance.boundarySelector = '.cdk-drop-list';
Expand Down Expand Up @@ -1848,6 +1865,25 @@ describe('CdkDrag', () => {
expect(placeholder.textContent!.trim()).toContain('Custom placeholder');
}));

it('should handle the custom placeholder being removed', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZoneWithCustomPlaceholder);
fixture.detectChanges();
flush();

const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;

fixture.componentInstance.renderPlaceholder = false;
fixture.detectChanges();

startDraggingViaMouse(fixture, item);

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

expect(placeholder).toBeTruthy();
expect(placeholder.classList).not.toContain('custom-placeholder');
expect(placeholder.textContent!.trim()).not.toContain('Custom placeholder');
}));

it('should clear the `transform` value from siblings when item is dropped`', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
Expand Down Expand Up @@ -2661,10 +2697,13 @@ class DraggableInHorizontalDropZone {
[cdkDragBoundary]="boundarySelector"
style="width: 100%; height: ${ITEM_HEIGHT}px; background: red;">
{{item}}
<div
class="custom-preview"
style="width: 50px; height: 50px; background: purple;"
*cdkDragPreview>Custom preview</div>

<ng-container *ngIf="renderCustomPreview">
<div
class="custom-preview"
style="width: 50px; height: 50px; background: purple;"
*cdkDragPreview>Custom preview</div>
</ng-container>
</div>
</div>
`
Expand All @@ -2674,6 +2713,7 @@ class DraggableInDropZoneWithCustomPreview {
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
items = ['Zero', 'One', 'Two', 'Three'];
boundarySelector: string;
renderCustomPreview = true;
}


Expand All @@ -2683,14 +2723,17 @@ class DraggableInDropZoneWithCustomPreview {
<div *ngFor="let item of items" cdkDrag
style="width: 100%; height: ${ITEM_HEIGHT}px; background: red;">
{{item}}
<div class="custom-placeholder" *cdkDragPlaceholder>Custom placeholder</div>
<ng-container *ngIf="renderPlaceholder">
<div class="custom-placeholder" *cdkDragPlaceholder>Custom placeholder</div>
</ng-container>
</div>
</div>
`
})
class DraggableInDropZoneWithCustomPlaceholder {
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
items = ['Zero', 'One', 'Two', 'Three'];
renderPlaceholder = true;
}


Expand Down
11 changes: 5 additions & 6 deletions src/cdk/drag-drop/directives/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
if (!ref.isDragging()) {
ref.disabled = this.disabled;
ref.lockAxis = this.lockAxis;
ref.withBoundaryElement(this._getBoundaryElement());
ref
.withBoundaryElement(this._getBoundaryElement())
.withPlaceholderTemplate(this._placeholderTemplate)
.withPreviewTemplate(this._previewTemplate);
}
});
this._proxyEvents(ref);
Expand Down Expand Up @@ -213,11 +216,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
`Currently attached to "${rootElement.nodeName}".`);
}

this._dragRef
.withRootElement(rootElement)
.withPlaceholderTemplate(this._placeholderTemplate)
.withPreviewTemplate(this._previewTemplate);

this._dragRef.withRootElement(rootElement);
this._handles.changes
.pipe(startWith(this._handles))
.subscribe((handleList: QueryList<CdkDragHandle>) => {
Expand Down