Skip to content

Commit 6e4fa41

Browse files
authored
Merge pull request #5739 from IgniteUI/SKrastev/fix-5730-master
fix(IgxDrag): Fix no being able to detect click event correctly when here is no ghost.
2 parents c8a8d05 + da7dbd8 commit 6e4fa41

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

projects/igniteui-angular/src/lib/directives/drag-drop/drag-drop.directive.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,23 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
372372
@Output()
373373
public dragEnd = new EventEmitter<IDragBaseEventArgs>();
374374

375+
/**
376+
* Event triggered when the draggable element is clicked.
377+
* ```html
378+
* <div igxDrag (dragClick)="onDragClick()">
379+
* <span>Drag Me!</span>
380+
* </div>
381+
* ```
382+
* ```typescript
383+
* public onDragClick(){
384+
* alert("The element has been clicked!");
385+
* }
386+
* ```
387+
* @memberof IgxDragDirective
388+
*/
389+
@Output()
390+
public dragClick = new EventEmitter<IDragBaseEventArgs>();
391+
375392
/**
376393
* Event triggered when the drag ghost element is created.
377394
* ```html
@@ -423,24 +440,6 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
423440
@Output()
424441
public transitioned = new EventEmitter<IDragBaseEventArgs>();
425442

426-
/**
427-
* @deprecated
428-
* Event triggered when the draggable element is clicked.
429-
* ```html
430-
* <div igxDrag (dragClicked)="dragClicked()">
431-
* <span>Drag Me!</span>
432-
* </div>
433-
* ```
434-
* ```typescript
435-
* public dragClicked(){
436-
* alert("The element has been clicked!");
437-
* }
438-
* ```
439-
* @memberof IgxDragDirective
440-
*/
441-
@Output()
442-
public dragClicked = new EventEmitter<IDragBaseEventArgs>();
443-
444443
/**
445444
* @hidden
446445
*/
@@ -1046,6 +1045,9 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
10461045
if (!this.animInProgress) {
10471046
this.onTransitionEnd(null);
10481047
}
1048+
} else {
1049+
// Trigger our own click event because when there is no ghost, native click cannot be prevented when dragging.
1050+
this.dragClick.emit(eventArgs);
10491051
}
10501052
}
10511053

projects/igniteui-angular/src/lib/directives/drag-drop/drag-drop.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ describe('General igxDrag/igxDrop', () => {
190190

191191
spyOn(firstDrag.ghostCreate, 'emit');
192192
spyOn(firstDrag.ghostDestroy, 'emit');
193+
spyOn(firstDrag.dragClick, 'emit');
193194
expect(document.getElementsByClassName('dragElem').length).toEqual(3);
194195

195196
// Step 1.
@@ -199,6 +200,7 @@ describe('General igxDrag/igxDrop', () => {
199200

200201
expect(firstDrag.ghostCreate.emit).not.toHaveBeenCalled();
201202
expect(firstDrag.ghostDestroy.emit).not.toHaveBeenCalled();
203+
expect(firstDrag.dragClick.emit).not.toHaveBeenCalled();
202204

203205
// Step 2.
204206
UIInteractions.simulatePointerEvent('pointermove', firstElement, startingX + 10, startingY + 10);
@@ -209,6 +211,7 @@ describe('General igxDrag/igxDrop', () => {
209211
expect(document.getElementsByClassName('dragElem').length).toEqual(3);
210212
expect(firstDrag.ghostCreate.emit).not.toHaveBeenCalled();
211213
expect(firstDrag.ghostDestroy.emit).not.toHaveBeenCalled();
214+
expect(firstDrag.dragClick.emit).not.toHaveBeenCalled();
212215

213216
// Step 3.
214217
// We need to trigger the pointerup on the ghostElement because this is the element we move and is under the mouse
@@ -220,6 +223,7 @@ describe('General igxDrag/igxDrop', () => {
220223
expect(document.getElementsByClassName('dragElem').length).toEqual(3);
221224
expect(firstDrag.ghostCreate.emit).not.toHaveBeenCalled();
222225
expect(firstDrag.ghostDestroy.emit).not.toHaveBeenCalled();
226+
expect(firstDrag.dragClick.emit).toHaveBeenCalled();
223227
}));
224228

225229
it('should position ghost at the same position relative to the mouse when drag started.', (async() => {
@@ -454,6 +458,7 @@ describe('General igxDrag/igxDrop', () => {
454458
firstDrag.dragTolerance = 25;
455459

456460
spyOn(firstDrag.dragStart, 'emit');
461+
spyOn(firstDrag.dragClick, 'emit');
457462

458463
// Step 1.
459464
UIInteractions.simulatePointerEvent('pointerdown', firstElement, startingX, startingY);
@@ -471,6 +476,7 @@ describe('General igxDrag/igxDrop', () => {
471476
expect(firstElement.getBoundingClientRect().left).toEqual(dragDirsRects[0].left);
472477
expect(firstElement.getBoundingClientRect().top).toEqual(dragDirsRects[0].top);
473478
expect(firstDrag.dragStart.emit).not.toHaveBeenCalled();
479+
expect(firstDrag.dragClick.emit).not.toHaveBeenCalled();
474480

475481
// Step 3.
476482
UIInteractions.simulatePointerEvent('pointermove', firstElement, startingX + 20, startingY + 20);
@@ -481,6 +487,7 @@ describe('General igxDrag/igxDrop', () => {
481487
expect(firstElement.getBoundingClientRect().left).toEqual(dragDirsRects[0].left);
482488
expect(firstElement.getBoundingClientRect().top).toEqual(dragDirsRects[0].top);
483489
expect(firstDrag.dragStart.emit).not.toHaveBeenCalled();
490+
expect(firstDrag.dragClick.emit).not.toHaveBeenCalled();
484491

485492
// Step 4.
486493
UIInteractions.simulatePointerEvent('pointerup', firstElement, startingX + 20, startingY + 20);
@@ -490,6 +497,7 @@ describe('General igxDrag/igxDrop', () => {
490497
expect(firstElement.getBoundingClientRect().left).toEqual(dragDirsRects[0].left);
491498
expect(firstElement.getBoundingClientRect().top).toEqual(dragDirsRects[0].top);
492499
expect(firstDrag.dragStart.emit).not.toHaveBeenCalled();
500+
expect(firstDrag.dragClick.emit).toHaveBeenCalled();
493501
}));
494502

495503
it('should correctly apply dragTolerance of 0 when it is set to 0 and ghost is disabled.', (async() => {

src/app/drag-drop/drag-drop.sample.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<h4 class="sample-title">No ghost dragging:</h4>
66
<div [style.height.px]="100">
77
<div [igxDrag]="{ dragged: false }" [ghost]="false" class="dialog" [dragChannel]="['Suspect']"
8-
(dragStart)="draggedElem = true" (dragEnd)="draggedElem = false">
8+
(dragStart)="draggedElem = true" (dragEnd)="draggedElem = false" (dragClick)="dragClick($event)">
99
<span *ngIf="!draggedElem">Drag me</span>
1010
<span *ngIf="draggedElem">Weeeee</span>
1111
</div>
@@ -15,7 +15,7 @@ <h4 class="sample-title">Ghost and base templating:</h4>
1515
<div #ghostHost [style.height.px]="100">
1616
<div [igxDrag]="{ id: 'customGhost'}" [dragChannel]="'Suspect'" [ghostTemplate]="dragGhost"
1717
[ngClass]="customDragged ? 'dragWithGhostPlaceholder' : 'dragWithGhostDefault'"
18-
(dragStart)="customDragged = true" (dragEnd)="customDragged = false">
18+
(dragStart)="customDragged = true" (dragEnd)="customDragged = false" (dragClick)="dragClick($event)">
1919
<span *ngIf="!customDragged">Drag me</span>
2020
<ng-template #dragGhost>
2121
<div class="customGhost">

src/app/drag-drop/drag-drop.sample.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,8 @@ export class DragDropSampleComponent {
365365
}
366366
});
367367
}
368+
369+
public dragClick() {
370+
console.log('click');
371+
}
368372
}

0 commit comments

Comments
 (0)