Skip to content

Commit c079805

Browse files
committed
fix(tooltip): allow tooltip text to be updated while shown
Fixes #1002
1 parent 4d1026c commit c079805

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

projects/angular-calendar/src/modules/common/calendar-tooltip.directive.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
ComponentFactory,
1313
Inject,
1414
Renderer2,
15-
TemplateRef
15+
TemplateRef,
16+
OnChanges,
17+
SimpleChanges
1618
} from '@angular/core';
1719
import { DOCUMENT } from '@angular/common';
1820
import { PlacementArray, positionElements } from 'positioning';
@@ -58,7 +60,7 @@ export class CalendarTooltipWindowComponent {
5860
@Directive({
5961
selector: '[mwlCalendarTooltip]'
6062
})
61-
export class CalendarTooltipDirective implements OnDestroy {
63+
export class CalendarTooltipDirective implements OnDestroy, OnChanges {
6264
@Input('mwlCalendarTooltip') contents: string; // tslint:disable-line no-input-rename
6365

6466
@Input('tooltipPlacement') placement: PlacementArray = 'auto'; // tslint:disable-line no-input-rename
@@ -88,6 +90,18 @@ export class CalendarTooltipDirective implements OnDestroy {
8890
);
8991
}
9092

93+
ngOnChanges(changes: SimpleChanges): void {
94+
if (
95+
this.tooltipRef &&
96+
(changes.contents || changes.customTemplate || changes.event)
97+
) {
98+
this.tooltipRef.instance.contents = this.contents;
99+
this.tooltipRef.instance.customTemplate = this.customTemplate;
100+
this.tooltipRef.instance.event = this.event;
101+
this.tooltipRef.changeDetectorRef.markForCheck();
102+
}
103+
}
104+
91105
ngOnDestroy(): void {
92106
this.hide();
93107
}

projects/angular-calendar/test/calendar-month-view.component.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -1012,4 +1012,38 @@ describe('calendarMonthView component', () => {
10121012
);
10131013
document.head.removeChild(style);
10141014
});
1015+
1016+
it('should allow the tooltip text to be updated dynamically', fakeAsync(() => {
1017+
const fixture: ComponentFixture<
1018+
CalendarMonthViewComponent
1019+
> = TestBed.createComponent(CalendarMonthViewComponent);
1020+
fixture.componentInstance.viewDate = new Date('2016-06-27');
1021+
fixture.componentInstance.events = [
1022+
{
1023+
start: new Date('2016-05-30'),
1024+
end: new Date('2016-06-02'),
1025+
title: 'foo'
1026+
}
1027+
];
1028+
fixture.componentInstance.ngOnChanges({ viewDate: {}, events: {} });
1029+
fixture.detectChanges();
1030+
const event: HTMLElement = fixture.nativeElement.querySelector(
1031+
'.cal-days .cal-cell-row .cal-cell:nth-child(4) .cal-events .cal-event'
1032+
);
1033+
triggerDomEvent('mouseenter', event);
1034+
fixture.detectChanges();
1035+
flush();
1036+
const tooltip = document.body.querySelector('.cal-tooltip');
1037+
expect(tooltip.querySelector('.cal-tooltip-inner').innerHTML).to.equal(
1038+
'foo'
1039+
);
1040+
fixture.componentInstance.events[0].title = 'bar';
1041+
fixture.detectChanges();
1042+
expect(tooltip.querySelector('.cal-tooltip-inner').innerHTML).to.equal(
1043+
'bar'
1044+
);
1045+
triggerDomEvent('mouseleave', event);
1046+
fixture.detectChanges();
1047+
fixture.destroy();
1048+
}));
10151049
});

0 commit comments

Comments
 (0)