@@ -46,6 +46,26 @@ describe('Standalone CdkDrag', () => {
4646 expect ( dragElement . style . transform ) . toBe ( 'translate3d(50px, 100px, 0px)' ) ;
4747 } ) ) ;
4848
49+ it ( 'should reset drag item to boundary' , fakeAsync ( ( ) => {
50+ const fixture = createComponent ( DragWithResizeableBoundary ) ;
51+ fixture . detectChanges ( ) ;
52+ let dragElement = fixture . componentInstance . dragElement . nativeElement ;
53+
54+ expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
55+ dragElementViaMouse ( fixture , dragElement , 50 , 100 ) ;
56+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(50px, 100px, 0px)' ) ;
57+
58+ dragElementViaMouse ( fixture , dragElement , 300 , 300 ) ;
59+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(300px, 300px, 0px)' ) ;
60+
61+ fixture . componentInstance . resizeBoundary ( ) ;
62+ fixture . detectChanges ( ) ;
63+
64+ const position = fixture . componentInstance . dragInstance . getFreeDragPosition ( ) ;
65+ expect ( position ) . toEqual ( { x : 100 , y : 0 } ) ;
66+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(100px, 0px, 0px)' ) ;
67+ } ) ) ;
68+
4969 it ( 'should drag an element freely to a particular position when the page is scrolled' , fakeAsync ( ( ) => {
5070 const fixture = createComponent ( StandaloneDraggable ) ;
5171 fixture . detectChanges ( ) ;
@@ -2047,3 +2067,26 @@ class PlainStandaloneDraggable {
20472067class StandaloneDraggableWithExternalTemplateHandle {
20482068 @ViewChild ( 'dragElement' ) dragElement : ElementRef < HTMLElement > ;
20492069}
2070+
2071+ @Component ( {
2072+ template : `
2073+ <div #boundaryElement class="example-boundary" style="width: 400px; height: 400px">
2074+ <div #dragElement class="example-box" cdkDragBoundary=".example-boundary" cdkDrag style="width: 100px; height: 100px">
2075+ I can only be dragged within the dotted container
2076+ </div>
2077+ </div>
2078+ ` ,
2079+ imports : [ CdkDrag ] ,
2080+ } )
2081+ class DragWithResizeableBoundary {
2082+ @ViewChild ( 'boundaryElement' ) boundaryElement : ElementRef < HTMLElement > ;
2083+
2084+ @ViewChild ( 'dragElement' ) dragElement : ElementRef < HTMLElement > ;
2085+ @ViewChild ( CdkDrag ) dragInstance : CdkDrag ;
2086+
2087+ resizeBoundary ( ) {
2088+ this . boundaryElement . nativeElement . style . height = '200px' ;
2089+ this . boundaryElement . nativeElement . style . width = '200px' ;
2090+ this . dragInstance . resetToBoundary ( ) ;
2091+ }
2092+ }
0 commit comments