@@ -1776,6 +1776,23 @@ describe('CdkDrag', () => {
1776
1776
expect ( preview . textContent ! . trim ( ) ) . toContain ( 'Custom preview' ) ;
1777
1777
} ) ) ;
1778
1778
1779
+ it ( 'should handle the custom preview being removed' , fakeAsync ( ( ) => {
1780
+ const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
1781
+ fixture . detectChanges ( ) ;
1782
+ flush ( ) ;
1783
+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
1784
+
1785
+ fixture . componentInstance . renderCustomPreview = false ;
1786
+ fixture . detectChanges ( ) ;
1787
+ startDraggingViaMouse ( fixture , item ) ;
1788
+
1789
+ const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
1790
+
1791
+ expect ( preview ) . toBeTruthy ( ) ;
1792
+ expect ( preview . classList ) . not . toContain ( 'custom-preview' ) ;
1793
+ expect ( preview . textContent ! . trim ( ) ) . not . toContain ( 'Custom preview' ) ;
1794
+ } ) ) ;
1795
+
1779
1796
it ( 'should be able to constrain the position of a custom preview' , fakeAsync ( ( ) => {
1780
1797
const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
1781
1798
fixture . componentInstance . boundarySelector = '.cdk-drop-list' ;
@@ -1900,6 +1917,25 @@ describe('CdkDrag', () => {
1900
1917
expect ( placeholder . textContent ! . trim ( ) ) . toContain ( 'Custom placeholder' ) ;
1901
1918
} ) ) ;
1902
1919
1920
+ it ( 'should handle the custom placeholder being removed' , fakeAsync ( ( ) => {
1921
+ const fixture = createComponent ( DraggableInDropZoneWithCustomPlaceholder ) ;
1922
+ fixture . detectChanges ( ) ;
1923
+ flush ( ) ;
1924
+
1925
+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
1926
+
1927
+ fixture . componentInstance . renderPlaceholder = false ;
1928
+ fixture . detectChanges ( ) ;
1929
+
1930
+ startDraggingViaMouse ( fixture , item ) ;
1931
+
1932
+ const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) ! as HTMLElement ;
1933
+
1934
+ expect ( placeholder ) . toBeTruthy ( ) ;
1935
+ expect ( placeholder . classList ) . not . toContain ( 'custom-placeholder' ) ;
1936
+ expect ( placeholder . textContent ! . trim ( ) ) . not . toContain ( 'Custom placeholder' ) ;
1937
+ } ) ) ;
1938
+
1903
1939
it ( 'should clear the `transform` value from siblings when item is dropped`' , fakeAsync ( ( ) => {
1904
1940
const fixture = createComponent ( DraggableInDropZone ) ;
1905
1941
fixture . detectChanges ( ) ;
@@ -2713,10 +2749,13 @@ class DraggableInHorizontalDropZone {
2713
2749
[cdkDragBoundary]="boundarySelector"
2714
2750
style="width: 100%; height: ${ ITEM_HEIGHT } px; background: red;">
2715
2751
{{item}}
2716
- <div
2717
- class="custom-preview"
2718
- style="width: 50px; height: 50px; background: purple;"
2719
- *cdkDragPreview>Custom preview</div>
2752
+
2753
+ <ng-container *ngIf="renderCustomPreview">
2754
+ <div
2755
+ class="custom-preview"
2756
+ style="width: 50px; height: 50px; background: purple;"
2757
+ *cdkDragPreview>Custom preview</div>
2758
+ </ng-container>
2720
2759
</div>
2721
2760
</div>
2722
2761
`
@@ -2726,6 +2765,7 @@ class DraggableInDropZoneWithCustomPreview {
2726
2765
@ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
2727
2766
items = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
2728
2767
boundarySelector : string ;
2768
+ renderCustomPreview = true ;
2729
2769
}
2730
2770
2731
2771
@@ -2735,14 +2775,17 @@ class DraggableInDropZoneWithCustomPreview {
2735
2775
<div *ngFor="let item of items" cdkDrag
2736
2776
style="width: 100%; height: ${ ITEM_HEIGHT } px; background: red;">
2737
2777
{{item}}
2738
- <div class="custom-placeholder" *cdkDragPlaceholder>Custom placeholder</div>
2778
+ <ng-container *ngIf="renderPlaceholder">
2779
+ <div class="custom-placeholder" *cdkDragPlaceholder>Custom placeholder</div>
2780
+ </ng-container>
2739
2781
</div>
2740
2782
</div>
2741
2783
`
2742
2784
} )
2743
2785
class DraggableInDropZoneWithCustomPlaceholder {
2744
2786
@ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
2745
2787
items = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
2788
+ renderPlaceholder = true ;
2746
2789
}
2747
2790
2748
2791
0 commit comments