Skip to content

Commit 8f6fc7a

Browse files
committed
fix(tooltip): consolidate tooltip show checks into a single method
1 parent 1a7d51a commit 8f6fc7a

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -359,18 +359,7 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
359359
*/
360360
@HostListener('mouseenter')
361361
public onMouseEnter() {
362-
if (this.tooltipDisabled) {
363-
return;
364-
}
365-
366-
if (!this.target.collapsed && this.target?.tooltipTarget?.sticky) {
367-
return;
368-
}
369-
370-
this._checkOutletAndOutsideClick();
371-
this._checkTooltipForMultipleTargets();
372-
this._evaluateStickyState();
373-
this._showOnInteraction();
362+
this._checksBeforeShowing(() => this._showOnInteraction());
374363
}
375364

376365
/**
@@ -391,19 +380,15 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
391380
*/
392381
@HostListener('touchstart')
393382
public onTouchStart() {
394-
if (this.tooltipDisabled) {
395-
return;
396-
}
397-
398-
this._showOnInteraction();
383+
this._checksBeforeShowing(() => this._showOnInteraction());
399384
}
400385

401386
/**
402387
* @hidden
403388
*/
404389
@HostListener('document:touchstart', ['$event'])
405390
public onDocumentTouchStart(event) {
406-
if (this.tooltipDisabled) {
391+
if (this.tooltipDisabled || this?.target?.tooltipTarget !== this) {
407392
return;
408393
}
409394

@@ -465,7 +450,7 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
465450
* ```
466451
*/
467452
public showTooltip() {
468-
this._showTooltip(false, true);
453+
this._checksBeforeShowing(() => this._showTooltip(false, true));
469454
}
470455

471456
/**
@@ -498,6 +483,20 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
498483
}
499484
}
500485

486+
/**
487+
* A guard method that performs precondition checks before showing the tooltip.
488+
* It ensures that the tooltip is not disabled and not already shown in sticky mode.
489+
* If all conditions pass, it executes the provided `action` callback.
490+
*/
491+
private _checksBeforeShowing(action: () => void): void {
492+
if (this.tooltipDisabled) return;
493+
if (!this.target.collapsed && this.target?.tooltipTarget?.sticky) return;
494+
495+
this._checkOutletAndOutsideClick();
496+
this._checkTooltipForMultipleTargets();
497+
action();
498+
}
499+
501500
private _hideTooltip(withDelay: boolean): void {
502501
if (this.target.collapsed) {
503502
return;
@@ -525,6 +524,8 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
525524
if (showingArgs.cancel) return;
526525
}
527526

527+
this._evaluateStickyState();
528+
528529
this.target.timeoutId = setTimeout(() => {
529530
// Call open() of IgxTooltipDirective
530531
this.target.open(this._mergedOverlaySettings);

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,33 @@ describe('IgxTooltip', () => {
699699
expect(tooltip.querySelector('igx-tooltip-close-button')).toBeNull();
700700
}));
701701

702+
it('should correctly update tooltip when showing programmatically for sticky and non-sticky targets', fakeAsync(() => {
703+
const tooltip = tooltipNativeElement;
704+
705+
targetOne.sticky = true;
706+
fix.detectChanges();
707+
targetOne.showTooltip();
708+
flush();
709+
710+
verifyTooltipVisibility(tooltip, targetOne, true);
711+
expect(tooltip.role).toBe('status');
712+
713+
// Programmatically show tooltip for targetTwo (non-sticky) without closing sticky tooltip
714+
targetTwo.sticky = false;
715+
targetTwo.showTooltip();
716+
flush();
717+
verifyTooltipPosition(tooltip, targetTwo, false);
718+
expect(tooltip.role).toBe('status');
719+
720+
targetOne.hideTooltip();
721+
flush();
722+
723+
targetTwo.showTooltip();
724+
flush();
725+
verifyTooltipPosition(tooltip, targetTwo, true);
726+
expect(tooltip.role).toBe('tooltip');
727+
}));
728+
702729
it('should correctly manage arrow state between different targets', fakeAsync(() => {
703730
targetOne.hasArrow = true;
704731
fix.detectChanges();
@@ -843,6 +870,18 @@ describe('IgxTooltip', () => {
843870

844871
verifyTooltipVisibility(tooltipNativeElement, tooltipTarget, false)
845872
}));
873+
874+
it('should correctly display a sticky tooltip on touchstart', fakeAsync(() => {
875+
tooltipTarget.sticky = true;
876+
fix.detectChanges();
877+
touchElement(button);
878+
flush();
879+
880+
verifyTooltipVisibility(tooltipNativeElement, tooltipTarget, true);
881+
const closeBtn = document.querySelector('.my-close-btn');
882+
expect(closeBtn).toBeTruthy();
883+
expect(tooltipNativeElement.getAttribute('role')).toBe('status');
884+
}));
846885
});
847886

848887
describe('IgxTooltip placement and offset', () => {

src/app/tooltip/tooltip.sample.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h4 class="sample-title">Simple tooltip</h4>
4646
<h4 class="sample-title">Tooltip input</h4>
4747
<div class="sample-tooltip-input">
4848

49-
<igx-avatar class="bottomMargin" src="assets/images/avatar/10.jpg" size="medium" igxTooltipTarget [tooltip]="'Her name is Toola Tipa'" [disableArrow]="true">
49+
<igx-avatar class="bottomMargin" src="assets/images/avatar/10.jpg" size="medium" igxTooltipTarget [tooltip]="'Her name is Toola Tipa'">
5050
</igx-avatar>
5151

5252
<igx-icon igxTooltipTarget [tooltip]="'Infragistics Inc. HQ'">

0 commit comments

Comments
 (0)