Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module:tooltip): support changing trigger #4397

Merged
merged 2 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion components/tooltip/base/nz-tooltip-base.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>();

Expand All @@ -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);
}
Expand Down Expand Up @@ -192,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();
}
Expand Down Expand Up @@ -241,6 +250,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(
Expand Down Expand Up @@ -341,4 +352,9 @@ export abstract class NzTooltipBaseDirective implements OnChanges, OnInit, OnDes
isEnter && isOrigin ? this.show() : this.hide();
}
}

private removeTriggerListeners(): void {
wzhudev marked this conversation as resolved.
Show resolved Hide resolved
this.triggerUnlisteners.forEach(cancel => cancel());
this.triggerUnlisteners.length = 0;
}
}
24 changes: 22 additions & 2 deletions components/tooltip/nz-tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { NzToolTipModule } from './nz-tooltip.module';
>
Show
</a>
<a #titleTemplate nz-tooltip [nzTitle]="template">Show</a>
<a #titleTemplate nz-tooltip [nzTitle]="template" [nzTrigger]="trigger">Show</a>
<ng-template #template>
title-template
</ng-template>
Expand All @@ -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 })
Expand All @@ -50,6 +49,7 @@ export class NzTooltipTestDirectiveComponent {
@ViewChild('inBtnGroup', { static: false }) inBtnGroup: ElementRef;
title = 'title-string';

trigger: string | null = 'hover';
}

@Component({
Expand Down Expand Up @@ -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);
}));
});
});

Expand Down