Skip to content

Commit 86df606

Browse files
authored
Hide the tooltip on destroying its target. (#15785)
* fix(Tooltip): Hide the tooltip on destroying the target.
1 parent 4625046 commit 86df606

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
314314
* @hidden
315315
*/
316316
public ngOnDestroy() {
317+
this.hideTooltip();
317318
this.destroy$.next();
318319
this.destroy$.complete();
319320
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ describe('IgxTooltip', () => {
268268
verifyTooltipVisibility(tooltipNativeElement, tooltipTarget, false);
269269
}));
270270

271+
it('IgxTooltip is hidden when its target is destroyed', fakeAsync(() => {
272+
hoverElement(button);
273+
flush();
274+
275+
verifyTooltipVisibility(tooltipNativeElement, tooltipTarget, true);
276+
277+
fix.componentInstance.showButton = false;
278+
fix.detectChanges();
279+
flush();
280+
281+
verifyTooltipVisibility(tooltipNativeElement, tooltipTarget, false);
282+
}));
283+
271284
describe('Tooltip events', () => {
272285
it('should emit the proper events when hovering/unhovering target', fakeAsync(() => {
273286
spyOn(tooltipTarget.tooltipShow, 'emit');
@@ -589,7 +602,7 @@ const touchElement = (element) => element.nativeElement.dispatchEvent(new TouchE
589602
const verifyTooltipVisibility = (tooltipNativeElement, tooltipTarget, shouldBeVisible: boolean) => {
590603
expect(tooltipNativeElement.classList.contains(TOOLTIP_CLASS)).toBe(shouldBeVisible);
591604
expect(tooltipNativeElement.classList.contains(HIDDEN_TOOLTIP_CLASS)).not.toBe(shouldBeVisible);
592-
expect(tooltipTarget.tooltipHidden).not.toBe(shouldBeVisible);
605+
expect(tooltipTarget?.tooltipHidden).not.toBe(shouldBeVisible);
593606
};
594607

595608
const verifyTooltipPosition = (tooltipNativeElement, actualTarget, shouldBeAligned: boolean) => {

projects/igniteui-angular/src/lib/test-utils/tooltip-components.spec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import { IgxToggleActionDirective, IgxToggleDirective } from '../directives/togg
66
@Component({
77
template: `
88
<div class="dummyDiv">dummy div for touch tests</div>
9-
<button [igxTooltipTarget]="tooltipRef" [tooltip]="'Infragistics Inc. HQ'"
10-
(tooltipShow)="showing($event)" (tooltipHide)="hiding($event)"
11-
style="margin: 200px">
12-
Hover me
13-
</button>
9+
10+
@if (showButton) {
11+
<button [igxTooltipTarget]="tooltipRef" [tooltip]="'Infragistics Inc. HQ'"
12+
(tooltipShow)="showing($event)" (tooltipHide)="hiding($event)"
13+
style="margin: 200px">
14+
Hover me
15+
</button>
16+
}
1417
<div igxTooltip #tooltipRef="tooltip">
1518
Hello, I am a tooltip!
1619
</div>
@@ -19,9 +22,10 @@ import { IgxToggleActionDirective, IgxToggleDirective } from '../directives/togg
1922
})
2023
export class IgxTooltipSingleTargetComponent {
2124
@ViewChild(IgxTooltipDirective, { static: true }) public tooltip: IgxTooltipDirective;
22-
@ViewChild(IgxTooltipTargetDirective, { static: true }) public tooltipTarget: IgxTooltipTargetDirective;
25+
@ViewChild(IgxTooltipTargetDirective, { static: false }) public tooltipTarget: IgxTooltipTargetDirective;
2326
public cancelShowing = false;
2427
public cancelHiding = false;
28+
public showButton = true;
2529

2630
public showing(args: ITooltipShowEventArgs) {
2731
if (this.cancelShowing) {

0 commit comments

Comments
 (0)