Skip to content

Commit 23a17cb

Browse files
authored
fix(tooltip): handle closeOnEsc via overlay #7638 (#7843)
1 parent 8c5362d commit 23a17cb

File tree

2 files changed

+30
-53
lines changed

2 files changed

+30
-53
lines changed

projects/igniteui-angular/src/lib/directives/tooltip/tooltip.directive.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ describe('IgxTooltip', () => {
272272
flush();
273273

274274
unhoverElement(button);
275+
tick(500);
275276
expect(tooltipTarget.onTooltipHide.emit).toHaveBeenCalled();
276277
flush();
277278
}));
@@ -285,6 +286,7 @@ describe('IgxTooltip', () => {
285286
flush();
286287

287288
tooltipTarget.hideTooltip();
289+
tick(500);
288290
expect(tooltipTarget.onTooltipHide.emit).toHaveBeenCalled();
289291
flush();
290292
}));
@@ -301,6 +303,7 @@ describe('IgxTooltip', () => {
301303
flush();
302304

303305
unhoverElement(button);
306+
tick(500);
304307
expect(tooltipTarget.onTooltipHide.emit).toHaveBeenCalledWith(tooltipHideArgs);
305308
flush();
306309
}));
@@ -317,6 +320,7 @@ describe('IgxTooltip', () => {
317320
flush();
318321

319322
tooltipTarget.hideTooltip();
323+
tick(500);
320324
expect(tooltipTarget.onTooltipHide.emit).toHaveBeenCalledWith(tooltipHideArgs);
321325
flush();
322326
}));

projects/igniteui-angular/src/lib/directives/tooltip/tooltip.directive.ts

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
Directive, ElementRef, HostListener, Input, NgModule, ChangeDetectorRef, OnInit,
3-
Output, EventEmitter, Optional, HostBinding, Inject
3+
Output, EventEmitter, Optional, HostBinding, Inject, OnDestroy
44
} from '@angular/core';
55
import { useAnimation } from '@angular/animations';
66
import { scaleInCenter } from '../../animations/scale/index';
@@ -11,6 +11,8 @@ import { CommonModule } from '@angular/common';
1111
import { IgxNavigationService } from '../../core/navigation';
1212
import { IgxToggleDirective, IgxToggleActionDirective } from '../toggle/toggle.directive';
1313
import { IBaseEventArgs } from '../../core/utils';
14+
import { Subject } from 'rxjs';
15+
import { takeUntil } from 'rxjs/operators';
1416

1517
export interface ITooltipShowEventArgs extends IBaseEventArgs {
1618
target: IgxTooltipTargetDirective;
@@ -41,7 +43,9 @@ export interface ITooltipHideEventArgs extends IBaseEventArgs {
4143
exportAs: 'tooltipTarget',
4244
selector: '[igxTooltipTarget]'
4345
})
44-
export class IgxTooltipTargetDirective extends IgxToggleActionDirective implements OnInit {
46+
export class IgxTooltipTargetDirective extends IgxToggleActionDirective implements OnInit, OnDestroy {
47+
private destroy$ = new Subject();
48+
4549
/**
4650
* Gets/sets the amount of milliseconds that should pass before showing the tooltip.
4751
*
@@ -196,6 +200,24 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
196200

197201
this._overlayDefaults.positionStrategy = new AutoPositionStrategy(positionSettings);
198202
this._overlayDefaults.closeOnOutsideClick = false;
203+
this._overlayDefaults.closeOnEscape = true;
204+
205+
this.target.onClosing.pipe(takeUntil(this.destroy$)).subscribe((event) => {
206+
const hidingArgs = { target: this, tooltip: this.target, cancel: false };
207+
this.onTooltipHide.emit(hidingArgs);
208+
209+
if (hidingArgs.cancel) {
210+
event.cancel = true;
211+
}
212+
});
213+
}
214+
215+
/**
216+
* @hidden
217+
*/
218+
ngOnDestroy() {
219+
this.destroy$.next();
220+
this.destroy$.complete();
199221
}
200222

201223
private checkOutletAndOutsideClick() {
@@ -220,13 +242,6 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
220242
if (!this.target.collapsed || this.target.toBeHidden) {
221243
clearTimeout(this.target.timeoutId);
222244

223-
const hidingArgs = { target: this, tooltip: this.target, cancel: false };
224-
this.onTooltipHide.emit(hidingArgs);
225-
226-
if (hidingArgs.cancel) {
227-
return true;
228-
}
229-
230245
// if close animation has started finish it, or close the tooltip with no animation
231246
this.target.forceClose(this.mergedOverlaySettings);
232247
this.target.toBeHidden = false;
@@ -249,36 +264,12 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
249264
return false;
250265
}
251266

252-
/**
253-
* @hidden
254-
*/
255-
@HostListener('document:keydown.escape', ['$event'])
256-
public onKeydownEscape(event) {
257-
const hidingArgs = { target: this, tooltip: this.target, cancel: false };
258-
this.onTooltipHide.emit(hidingArgs);
259-
260-
if (hidingArgs.cancel) {
261-
return;
262-
}
263-
264-
this.target.toBeHidden = true;
265-
this.target.close();
266-
this.target.toBeHidden = false;
267-
}
268-
269267
/**
270268
* @hidden
271269
*/
272270
@HostListener('click')
273271
public onClick() {
274272
if (!this.target.collapsed) {
275-
const hidingArgs = { target: this, tooltip: this.target, cancel: false };
276-
this.onTooltipHide.emit(hidingArgs);
277-
278-
if (hidingArgs.cancel) {
279-
return;
280-
}
281-
282273
this.target.forceClose(this.mergedOverlaySettings);
283274
}
284275
}
@@ -327,18 +318,13 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
327318
return;
328319
}
329320

330-
const hidingArgs = { target: this, tooltip: this.target, cancel: false };
331-
this.onTooltipHide.emit(hidingArgs);
332-
333-
if (hidingArgs.cancel) {
334-
return;
335-
}
336-
337321
this.target.toBeHidden = true;
338322
this.target.timeoutId = setTimeout(() => {
339323
this.target.close(); // Call close() of IgxTooltipDirective
340324
this.target.toBeHidden = false;
341325
}, this.hideDelay);
326+
327+
342328
}
343329

344330
/**
@@ -380,12 +366,6 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
380366
clearTimeout(this.target.timeoutId);
381367

382368
if (!this.target.collapsed) {
383-
const hidingArgs = { target: this, tooltip: this.target, cancel: false };
384-
this.onTooltipHide.emit(hidingArgs);
385-
386-
if (hidingArgs.cancel) {
387-
return;
388-
}
389369
// if close animation has started finish it, or close the tooltip with no animation
390370
this.target.forceClose(this.mergedOverlaySettings);
391371
this.target.toBeHidden = false;
@@ -421,13 +401,6 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
421401
return;
422402
}
423403

424-
const hidingArgs = { target: this, tooltip: this.target, cancel: false };
425-
this.onTooltipHide.emit(hidingArgs);
426-
427-
if (hidingArgs.cancel) {
428-
return;
429-
}
430-
431404
this.target.toBeHidden = true;
432405
this.target.timeoutId = setTimeout(() => {
433406
this.target.close(); // Call close() of IgxTooltipDirective

0 commit comments

Comments
 (0)