Skip to content

Commit 7f1bedb

Browse files
authored
Hide the tooltip on destroying its target. (#15786)
* fix(Tooltip): Hide the tooltip on destroying the target.
1 parent 33d7052 commit 7f1bedb

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
@@ -271,6 +271,19 @@ describe('IgxTooltip', () => {
271271
verifyTooltipVisibility(tooltipNativeElement, tooltipTarget, false);
272272
}));
273273

274+
it('IgxTooltip is hidden when its target is destroyed', fakeAsync(() => {
275+
hoverElement(button);
276+
flush();
277+
278+
verifyTooltipVisibility(tooltipNativeElement, tooltipTarget, true);
279+
280+
fix.componentInstance.showButton = false;
281+
fix.detectChanges();
282+
flush();
283+
284+
verifyTooltipVisibility(tooltipNativeElement, tooltipTarget, false);
285+
}));
286+
274287
describe('Tooltip events', () => {
275288
// configureTestSuite();
276289
it('should emit the proper events when hovering/unhovering target', fakeAsync(() => {
@@ -596,7 +609,7 @@ const touchElement = (element) => element.nativeElement.dispatchEvent(new TouchE
596609
const verifyTooltipVisibility = (tooltipNativeElement, tooltipTarget, shouldBeVisible: boolean) => {
597610
expect(tooltipNativeElement.classList.contains(TOOLTIP_CLASS)).toBe(shouldBeVisible);
598611
expect(tooltipNativeElement.classList.contains(HIDDEN_TOOLTIP_CLASS)).not.toBe(shouldBeVisible);
599-
expect(tooltipTarget.tooltipHidden).not.toBe(shouldBeVisible);
612+
expect(tooltipTarget?.tooltipHidden).not.toBe(shouldBeVisible);
600613
};
601614

602615
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>
@@ -20,9 +23,10 @@ import { IgxToggleActionDirective, IgxToggleDirective } from '../directives/togg
2023
})
2124
export class IgxTooltipSingleTargetComponent {
2225
@ViewChild(IgxTooltipDirective, { static: true }) public tooltip: IgxTooltipDirective;
23-
@ViewChild(IgxTooltipTargetDirective, { static: true }) public tooltipTarget: IgxTooltipTargetDirective;
26+
@ViewChild(IgxTooltipTargetDirective, { static: false }) public tooltipTarget: IgxTooltipTargetDirective;
2427
public cancelShowing = false;
2528
public cancelHiding = false;
29+
public showButton = true;
2630

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

0 commit comments

Comments
 (0)