Skip to content

Commit d4f2356

Browse files
crisbetojelbourn
authored andcommitted
refactor(drag-drop): expose drag ref in constrainPosition function (#16465)
Exposes the `DragRef` of the instance being dragged when calling the `constrainPosition` function. The `DragRef` has some more info and enable more complex use cases. Fixes #16425.
1 parent 49e8c59 commit d4f2356

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {of as observableOf} from 'rxjs';
2828

2929
import {DragDropModule} from '../drag-drop-module';
3030
import {CdkDragDrop, CdkDragEnter} from '../drag-events';
31-
import {DragRefConfig, Point} from '../drag-ref';
31+
import {DragRefConfig, Point, DragRef} from '../drag-ref';
3232
import {extendStyles} from '../drag-styling';
3333
import {moveItemInArray} from '../drag-utils';
3434

@@ -753,7 +753,8 @@ describe('CdkDrag', () => {
753753
expect(dragElement.style.transform).toBeFalsy();
754754
dragElementViaMouse(fixture, dragElement, 300, 300);
755755

756-
expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({x: 300, y: 300}));
756+
expect(spy)
757+
.toHaveBeenCalledWith(jasmine.objectContaining({x: 300, y: 300}), jasmine.any(DragRef));
757758
expect(dragElement.style.transform).toBe('translate3d(50px, 50px, 0px)');
758759
}));
759760

@@ -2430,7 +2431,8 @@ describe('CdkDrag', () => {
24302431

24312432
const previewRect = preview.getBoundingClientRect();
24322433

2433-
expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({x: 200, y: 200}));
2434+
expect(spy)
2435+
.toHaveBeenCalledWith(jasmine.objectContaining({x: 200, y: 200}), jasmine.any(DragRef));
24342436
expect(Math.floor(previewRect.top)).toBe(50);
24352437
expect(Math.floor(previewRect.left)).toBe(50);
24362438
}));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
152152
* of the user's pointer on the page and should return a point describing where the item should
153153
* be rendered.
154154
*/
155-
@Input('cdkDragConstrainPosition') constrainPosition?: (point: Point) => Point;
155+
@Input('cdkDragConstrainPosition') constrainPosition?: (point: Point, dragRef: DragRef) => Point;
156156

157157
/** Emits when the user starts dragging the item. */
158158
@Output('cdkDragStarted') started: EventEmitter<CdkDragStart> = new EventEmitter<CdkDragStart>();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export class DragRef<T = any> {
271271
* of the user's pointer on the page and should return a point describing where the item should
272272
* be rendered.
273273
*/
274-
constrainPosition?: (point: Point) => Point;
274+
constrainPosition?: (point: Point, dragRef: DragRef) => Point;
275275

276276
constructor(
277277
element: ElementRef<HTMLElement> | HTMLElement,
@@ -956,7 +956,7 @@ export class DragRef<T = any> {
956956
/** Gets the pointer position on the page, accounting for any position constraints. */
957957
private _getConstrainedPointerPosition(event: MouseEvent | TouchEvent): Point {
958958
const point = this._getPointerPositionOnPage(event);
959-
const constrainedPoint = this.constrainPosition ? this.constrainPosition(point) : point;
959+
const constrainedPoint = this.constrainPosition ? this.constrainPosition(point, this) : point;
960960
const dropContainerLock = this._dropContainer ? this._dropContainer.lockAxis : null;
961961

962962
if (this.lockAxis === 'x' || dropContainerLock === 'x') {

tools/public_api_guard/cdk/drag-drop.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export declare class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDes
1313
_previewTemplate: CdkDragPreview;
1414
boundaryElement: string | ElementRef<HTMLElement> | HTMLElement;
1515
boundaryElementSelector: string;
16-
constrainPosition?: (point: Point) => Point;
16+
constrainPosition?: (point: Point, dragRef: DragRef) => Point;
1717
data: T;
1818
disabled: boolean;
1919
dragStartDelay: number;
@@ -227,7 +227,7 @@ export declare class DragDropRegistry<I, C extends {
227227

228228
export declare class DragRef<T = any> {
229229
beforeStarted: Subject<void>;
230-
constrainPosition?: (point: Point) => Point;
230+
constrainPosition?: (point: Point, dragRef: DragRef) => Point;
231231
data: T;
232232
disabled: boolean;
233233
dragStartDelay: number;

0 commit comments

Comments
 (0)