Skip to content

Commit 4867ebf

Browse files
xiejay97wangyaju
andauthored
fix(tooltip): refresh when input change (#107)
close #106 Co-authored-by: wangyaju <wyj19881215@163.com>
1 parent 3a31c9e commit 4867ebf

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

devui/tooltip/tooltip.directive.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
ElementRef,
77
HostListener,
88
Input,
9-
OnDestroy
9+
OnChanges,
10+
OnDestroy,
11+
SimpleChanges,
1012
} from '@angular/core';
1113
import { OverlayContainerRef } from 'ng-devui/overlay-container';
1214
import { DevConfigService, WithConfig } from 'ng-devui/utils/globalConfig';
@@ -19,7 +21,7 @@ import { PositionType } from './tooltip.types';
1921
selector: '[dTooltip]',
2022
exportAs: 'dTooltip',
2123
})
22-
export class TooltipDirective implements AfterViewInit, OnDestroy {
24+
export class TooltipDirective implements OnChanges, AfterViewInit, OnDestroy {
2325
@Input() content: string;
2426
@Input() position: PositionType | PositionType[] = 'top';
2527
@Input() @WithConfig() showAnimation = true;
@@ -41,7 +43,7 @@ export class TooltipDirective implements AfterViewInit, OnDestroy {
4143
private triggerElementRef: ElementRef,
4244
private overlayContainerRef: OverlayContainerRef,
4345
private componentFactoryResolver: ComponentFactoryResolver,
44-
private devConfigService: DevConfigService,
46+
private devConfigService: DevConfigService
4547
) {}
4648

4749
@HostListener('focus') onFocus() {
@@ -57,18 +59,14 @@ export class TooltipDirective implements AfterViewInit, OnDestroy {
5759
this.componentFactoryResolver.resolveComponentFactory(TooltipComponent)
5860
);
5961

60-
Object.assign(this.tooltipComponentRef.instance, {
61-
content: this.content,
62-
position: this.position,
63-
showAnimation: this.showAnimation,
64-
triggerElementRef: this.triggerElementRef,
65-
});
62+
this.instanceAssignValue(['content', 'position', 'showAnimation', 'triggerElementRef']);
6663

6764
// 对创建的ToolTip组件添加鼠标移入和移出的监听事件
6865
if (this.tooltipComponentRef.instance['tooltip'].nativeElement) {
6966
this.bindMouseEvent();
7067
}
7168
}
69+
7270
bindMouseEvent() {
7371
fromEvent(this.tooltipComponentRef.instance['tooltip'].nativeElement, 'mouseenter')
7472
.pipe(
@@ -111,7 +109,6 @@ export class TooltipDirective implements AfterViewInit, OnDestroy {
111109

112110
this.createTooltip();
113111
this.tooltipComponentRef.instance.onShow();
114-
115112
}
116113

117114
destroy() {
@@ -141,6 +138,29 @@ export class TooltipDirective implements AfterViewInit, OnDestroy {
141138
this.unsubscribeT$.complete();
142139
}
143140
}
141+
142+
instanceAssignValue(key: string | string[]): void {
143+
const keyArr = typeof key === 'string' ? [key] : key;
144+
const obj: any = {};
145+
keyArr.forEach((item) => (obj[item] = this[item]));
146+
Object.assign(this.tooltipComponentRef.instance, obj);
147+
}
148+
149+
ngOnChanges(changes: SimpleChanges): void {
150+
if (this.tooltipComponentRef) {
151+
const { content, position, showAnimation } = changes;
152+
if (content) {
153+
this.instanceAssignValue('content');
154+
}
155+
if (position) {
156+
this.instanceAssignValue('position');
157+
}
158+
if (showAnimation) {
159+
this.instanceAssignValue('showAnimation');
160+
}
161+
}
162+
}
163+
144164
ngAfterViewInit() {
145165
if (this.triggerElementRef.nativeElement) {
146166
fromEvent(this.triggerElementRef.nativeElement, 'mouseenter')
@@ -173,6 +193,7 @@ export class TooltipDirective implements AfterViewInit, OnDestroy {
173193
});
174194
}
175195
}
196+
176197
ngOnDestroy() {
177198
if (this.unsubscribeT$) {
178199
this.unsubscribeT$.next();

0 commit comments

Comments
 (0)