From a9a5168714f0392e3fb776104500b91007cc53ce Mon Sep 17 00:00:00 2001 From: Wendell Date: Wed, 6 Nov 2019 22:34:44 +0800 Subject: [PATCH 1/2] feat(module:tooltip): support changing trigger --- .../tooltip/base/nz-tooltip-base.directive.ts | 16 ++++++++++++- components/tooltip/nz-tooltip.spec.ts | 24 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/components/tooltip/base/nz-tooltip-base.directive.ts b/components/tooltip/base/nz-tooltip-base.directive.ts index 3f5547b08ac..ae02ca4f189 100644 --- a/components/tooltip/base/nz-tooltip-base.directive.ts +++ b/components/tooltip/base/nz-tooltip-base.directive.ts @@ -114,7 +114,7 @@ export abstract class NzTooltipBaseDirective implements OnChanges, OnInit, OnDes */ protected isDynamicTooltip = false; - protected triggerUnlisteners: Array<() => void> = []; + protected readonly triggerUnlisteners: Array<() => void> = []; protected $destroy = new Subject(); @@ -133,6 +133,13 @@ export abstract class NzTooltipBaseDirective implements OnChanges, OnInit, OnDes ) {} ngOnChanges(changes: SimpleChanges): void { + const { nzTrigger, specificTrigger } = changes; + const trigger = specificTrigger || nzTrigger; + + if (trigger && !trigger.isFirstChange()) { + this.registerTriggers(); + } + if (this.tooltip && this.isDynamicTooltip) { this.updateChangedProperties(changes); } @@ -241,6 +248,8 @@ export abstract class NzTooltipBaseDirective implements OnChanges, OnInit, OnDes const el = this.elementRef.nativeElement; const trigger = this.isDynamicTooltip ? this.trigger : this.tooltip.nzTrigger; + this.removeTriggerListeners(); + if (trigger === 'hover') { let overlayElement: HTMLElement; this.triggerUnlisteners.push( @@ -341,4 +350,9 @@ export abstract class NzTooltipBaseDirective implements OnChanges, OnInit, OnDes isEnter && isOrigin ? this.show() : this.hide(); } } + + private removeTriggerListeners(): void { + this.triggerUnlisteners.forEach(cancel => cancel()); + this.triggerUnlisteners.length = 0; + } } diff --git a/components/tooltip/nz-tooltip.spec.ts b/components/tooltip/nz-tooltip.spec.ts index d51f686a2f1..b3c28c38e45 100644 --- a/components/tooltip/nz-tooltip.spec.ts +++ b/components/tooltip/nz-tooltip.spec.ts @@ -24,7 +24,7 @@ import { NzToolTipModule } from './nz-tooltip.module'; > Show - Show + Show title-template @@ -36,7 +36,6 @@ import { NzToolTipModule } from './nz-tooltip.module'; ` }) export class NzTooltipTestDirectiveComponent { - @ViewChild('titleString', { static: false }) titleString: ElementRef; @ViewChild('titleString', { static: false, read: NzTooltipDirective }) @@ -50,6 +49,7 @@ export class NzTooltipTestDirectiveComponent { @ViewChild('inBtnGroup', { static: false }) inBtnGroup: ElementRef; title = 'title-string'; + trigger: string | null = 'hover'; } @Component({ @@ -282,6 +282,26 @@ describe('NzTooltip', () => { expect(tooltipComponent.nzTitle).toBe('changed!'); expect(overlayContainerElement.textContent).toContain('changed!'); })); + + it('should support changing trigger', fakeAsync(() => { + const featureKey = 'title-template'; + const triggerElement = component.titleTemplate.nativeElement; + + dispatchMouseEvent(triggerElement, 'mouseenter'); + waitingForTooltipToggling(fixture); + expect(overlayContainerElement.textContent).toContain(featureKey); + + dispatchMouseEvent(triggerElement, 'mouseleave'); + waitingForTooltipToggling(fixture); + expect(overlayContainerElement.textContent).not.toContain(featureKey); + + component.trigger = null; + fixture.detectChanges(); + + dispatchMouseEvent(triggerElement, 'mouseenter'); + waitingForTooltipToggling(fixture); + expect(overlayContainerElement.textContent).not.toContain(featureKey); + })); }); }); From 9cc6e426658eb753ced91f1125a47531cea82e47 Mon Sep 17 00:00:00 2001 From: Wendell Date: Thu, 7 Nov 2019 19:09:03 +0800 Subject: [PATCH 2/2] fix: remove listeners when directive destroys --- components/tooltip/base/nz-tooltip-base.directive.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/tooltip/base/nz-tooltip-base.directive.ts b/components/tooltip/base/nz-tooltip-base.directive.ts index ae02ca4f189..60bf759dbe4 100644 --- a/components/tooltip/base/nz-tooltip-base.directive.ts +++ b/components/tooltip/base/nz-tooltip-base.directive.ts @@ -199,6 +199,8 @@ export abstract class NzTooltipBaseDirective implements OnChanges, OnInit, OnDes ngOnDestroy(): void { this.$destroy.next(); this.$destroy.complete(); + this.removeTriggerListeners(); + if (this.tooltipRef) { this.tooltipRef.destroy(); }