From 7b25c654e4e640dfb9e438fc0beaef952e6453a2 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 20 Mar 2024 09:26:51 +0100 Subject: [PATCH] fix(cdk/drag-drop): optionally inject parent drag in preview and placeholder Fixes a regression from #28633 where using `cdkDragPlaceholder` or `cdkDragPreview` without a `cdkDrag` would throw. Technically this is a no-op, but it appears that folks started depending on the old behavior so these changes bring it back. Fixes #28744. --- src/cdk/drag-drop/directives/drag-placeholder.ts | 6 +++--- src/cdk/drag-drop/directives/drag-preview.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cdk/drag-drop/directives/drag-placeholder.ts b/src/cdk/drag-drop/directives/drag-placeholder.ts index e6df55b485e8..5afbfe04bf8c 100644 --- a/src/cdk/drag-drop/directives/drag-placeholder.ts +++ b/src/cdk/drag-drop/directives/drag-placeholder.ts @@ -26,16 +26,16 @@ export const CDK_DRAG_PLACEHOLDER = new InjectionToken('CdkD providers: [{provide: CDK_DRAG_PLACEHOLDER, useExisting: CdkDragPlaceholder}], }) export class CdkDragPlaceholder implements OnDestroy { - private _drag = inject(CDK_DRAG_PARENT); + private _drag = inject(CDK_DRAG_PARENT, {optional: true}); /** Context data to be added to the placeholder template instance. */ @Input() data: T; constructor(public templateRef: TemplateRef) { - this._drag._setPlaceholderTemplate(this); + this._drag?._setPlaceholderTemplate(this); } ngOnDestroy(): void { - this._drag._resetPlaceholderTemplate(this); + this._drag?._resetPlaceholderTemplate(this); } } diff --git a/src/cdk/drag-drop/directives/drag-preview.ts b/src/cdk/drag-drop/directives/drag-preview.ts index 9c23d8a03bf2..73a8c17219f5 100644 --- a/src/cdk/drag-drop/directives/drag-preview.ts +++ b/src/cdk/drag-drop/directives/drag-preview.ts @@ -34,7 +34,7 @@ export const CDK_DRAG_PREVIEW = new InjectionToken('CdkDragPrevi providers: [{provide: CDK_DRAG_PREVIEW, useExisting: CdkDragPreview}], }) export class CdkDragPreview implements OnDestroy { - private _drag = inject(CDK_DRAG_PARENT); + private _drag = inject(CDK_DRAG_PARENT, {optional: true}); /** Context data to be added to the preview template instance. */ @Input() data: T; @@ -43,10 +43,10 @@ export class CdkDragPreview implements OnDestroy { @Input({transform: booleanAttribute}) matchSize: boolean = false; constructor(public templateRef: TemplateRef) { - this._drag._setPreviewTemplate(this); + this._drag?._setPreviewTemplate(this); } ngOnDestroy(): void { - this._drag._resetPreviewTemplate(this); + this._drag?._resetPreviewTemplate(this); } }