Skip to content

Commit 94d3fda

Browse files
crisbetojelbourn
authored andcommitted
fix(drag-drop): placeholder element not available in started event (#19493)
Fixes calling `CdkDrag.getPlaceholderElement` returning undefined if it's called in the `cdkDragStarted` event. Fixes #19457.
1 parent c7eaec9 commit 94d3fda

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {_supportsShadowDom} from '@angular/cdk/platform';
2828
import {of as observableOf} from 'rxjs';
2929

3030
import {DragDropModule} from '../drag-drop-module';
31-
import {CdkDragDrop, CdkDragEnter} from '../drag-events';
31+
import {CdkDragDrop, CdkDragEnter, CdkDragStart} from '../drag-events';
3232
import {Point, DragRef} from '../drag-ref';
3333
import {extendStyles} from '../drag-styling';
3434
import {moveItemInArray} from '../drag-utils';
@@ -4022,6 +4022,20 @@ describe('CdkDrag', () => {
40224022
expect(fixture.componentInstance.droppedSpy).toHaveBeenCalledTimes(1);
40234023
}));
40244024

4025+
it('should make the placeholder available in the start event', fakeAsync(() => {
4026+
const fixture = createComponent(DraggableInDropZone);
4027+
fixture.detectChanges();
4028+
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;
4029+
let placeholder: HTMLElement | undefined;
4030+
4031+
fixture.componentInstance.startedSpy.and.callFake((event: CdkDragStart) => {
4032+
placeholder = event.source.getPlaceholderElement();
4033+
});
4034+
4035+
startDraggingViaMouse(fixture, item);
4036+
expect(placeholder).toBeTruthy();
4037+
}));
4038+
40254039
});
40264040

40274041
describe('in a connected drop container', () => {
@@ -5409,6 +5423,7 @@ const DROP_ZONE_FIXTURE_TEMPLATE = `
54095423
[cdkDragPreviewClass]="previewClass"
54105424
[style.height.px]="item.height"
54115425
[style.margin-bottom.px]="item.margin"
5426+
(cdkDragStarted)="startedSpy($event)"
54125427
style="width: 100%; background: red;">{{item.value}}</div>
54135428
</div>
54145429
`;
@@ -5430,6 +5445,7 @@ class DraggableInDropZone {
54305445
droppedSpy = jasmine.createSpy('dropped spy').and.callFake((event: CdkDragDrop<string[]>) => {
54315446
moveItemInArray(this.items, event.previousIndex, event.currentIndex);
54325447
});
5448+
startedSpy = jasmine.createSpy('started spy');
54335449
}
54345450

54355451
@Component({

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,12 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
226226
/**
227227
* Returns the element that is being used as a placeholder
228228
* while the current element is being dragged.
229-
* @deprecated No longer being used to be removed.
230-
* @breaking-change 11.0.0
231229
*/
232230
getPlaceholderElement(): HTMLElement {
233231
return this._dragRef.getPlaceholderElement();
234232
}
235233

236-
/**
237-
* Returns the root draggable element.
238-
* @deprecated No longer being used to be removed.
239-
* @breaking-change 11.0.0
240-
*/
234+
/** Returns the root draggable element. */
241235
getRootElement(): HTMLElement {
242236
return this._dragRef.getRootElement();
243237
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,6 @@ export class DragRef<T = any> {
695695

696696
/** Starts the dragging sequence. */
697697
private _startDragSequence(event: MouseEvent | TouchEvent) {
698-
// Emit the event on the item before the one on the container.
699-
this.started.next({source: this});
700-
701698
if (isTouchEvent(event)) {
702699
this._lastTouchEventTime = Date.now();
703700
}
@@ -722,10 +719,12 @@ export class DragRef<T = any> {
722719
element.style.display = 'none';
723720
this._document.body.appendChild(parent.replaceChild(placeholder, element));
724721
getPreviewInsertionPoint(this._document).appendChild(preview);
722+
this.started.next({source: this}); // Emit before notifying the container.
725723
dropContainer.start();
726724
this._initialContainer = dropContainer;
727725
this._initialIndex = dropContainer.getItemIndex(this);
728726
} else {
727+
this.started.next({source: this});
729728
this._initialContainer = this._initialIndex = undefined!;
730729
}
731730

0 commit comments

Comments
 (0)