Skip to content

Commit c89c369

Browse files
committed
fix(calendar): Memory leaks due to wrong event listener pattern
1 parent 2cadfa8 commit c89c369

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

projects/igniteui-angular/src/lib/calendar/calendar.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ <h2 id="igx-aria-calendar-title-month" class="igx-calendar__header-date">
189189
[attr.aria-multiselectable]="selection !== 'single'"
190190
aria-labelledby="calendar-desc"
191191
role="grid"
192+
(focus)="this.onWrapperFocus($event)"
193+
(blur)="this.onWrapperBlur($event)"
192194
>
193195
<caption id="calendar-desc" tabindex="-1" class="igx-calendar__aria-off-screen">
194196
@if (selection === 'multi') {

projects/igniteui-angular/src/lib/calendar/calendar.component.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe("IgxCalendar - ", () => {
128128
});
129129

130130
describe("Basic -", () => {
131-
configureTestSuite();
131+
configureTestSuite({ checkLeaks: true });
132132

133133
beforeAll(waitForAsync(() => {
134134
TestBed.configureTestingModule({
@@ -154,6 +154,11 @@ describe("IgxCalendar - ", () => {
154154
calendar = fixture.componentInstance.calendar;
155155
dom = fixture.debugElement;
156156
}));
157+
afterEach(() => {
158+
fixture = undefined;
159+
calendar = undefined;
160+
dom = undefined;
161+
});
157162

158163
it("Should initialize a calendar component", () => {
159164
expect(fixture.componentInstance).toBeDefined();

projects/igniteui-angular/src/lib/calendar/calendar.component.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,6 @@ export class IgxCalendarComponent extends IgxCalendarBaseDirective implements Af
483483
.set("PageUp", this.handlePageUp)
484484
.set("PageDown", this.handlePageDown);
485485

486-
this.wrapper.nativeElement.addEventListener('focus', (event: FocusEvent) => this.onWrapperFocus(event));
487-
this.wrapper.nativeElement.addEventListener('blur', (event: FocusEvent) => this.onWrapperBlur(event));
488-
489486
this.startPageScroll$.pipe(
490487
takeUntil(this.stopPageScroll$),
491488
switchMap(() => this.scrollPage$.pipe(
@@ -516,12 +513,12 @@ export class IgxCalendarComponent extends IgxCalendarBaseDirective implements Af
516513
});
517514
}
518515

519-
private onWrapperFocus(_event: FocusEvent) {
516+
protected onWrapperFocus(_event: FocusEvent) {
520517
this.showActiveDay = true;
521518
this.monthViews.forEach(view => view.changePreviewRange(this.activeDate));
522519
}
523520

524-
private onWrapperBlur(_event: FocusEvent) {
521+
protected onWrapperBlur(_event: FocusEvent) {
525522
this.showActiveDay = false;
526523
this.monthViews.forEach(view => view.clearPreviewRange());
527524
this._onTouchedCallback();
@@ -1059,8 +1056,6 @@ export class IgxCalendarComponent extends IgxCalendarBaseDirective implements Af
10591056
*/
10601057
public ngOnDestroy(): void {
10611058
this.keyboardNavigation.detachKeyboardHandlers();
1062-
this.wrapper?.nativeElement.removeEventListener('focus', this.onWrapperFocus);
1063-
this.wrapper?.nativeElement.removeEventListener('blur', this.onWrapperBlur);
10641059
}
10651060

10661061
/**

projects/igniteui-angular/src/lib/calendar/month-picker/month-picker.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
[attr.aria-multiselectable]="selection !== 'single'"
103103
aria-labelledby="calendar-desc"
104104
role="grid"
105+
(focus)="this.onWrapperFocus($event)";
106+
(blur)="this.onWrapperBlur($event)";
105107
>
106108
<caption id="calendar-desc" tabindex="-1" class="igx-calendar__aria-off-screen">
107109
{{ resourceStrings.igx_calendar_singular_single_selection}}

projects/igniteui-angular/src/lib/calendar/month-picker/month-picker.component.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,6 @@ export class IgxMonthPickerComponent extends IgxCalendarBaseDirective implements
331331
.set("PageUp", this.handlePageUp)
332332
.set("PageDown", this.handlePageDown);
333333

334-
this.wrapper.nativeElement.addEventListener('focus', (event: FocusEvent) => this.onWrapperFocus(event));
335-
this.wrapper.nativeElement.addEventListener('blur', (event: FocusEvent) => this.onWrapperBlur(event));
336-
337334
this.activeView$.subscribe((view) => {
338335
this.activeViewChanged.emit(view);
339336

@@ -344,12 +341,12 @@ export class IgxMonthPickerComponent extends IgxCalendarBaseDirective implements
344341
});
345342
}
346343

347-
private onWrapperFocus(event: FocusEvent) {
344+
protected onWrapperFocus(event: FocusEvent) {
348345
event.stopPropagation();
349346
this.showActiveDay = true;
350347
}
351348

352-
private onWrapperBlur(event: FocusEvent) {
349+
protected onWrapperBlur(event: FocusEvent) {
353350
event.stopPropagation();
354351

355352
this.showActiveDay = false;
@@ -456,8 +453,6 @@ export class IgxMonthPickerComponent extends IgxCalendarBaseDirective implements
456453
*/
457454
public ngOnDestroy(): void {
458455
this.keyboardNavigation.detachKeyboardHandlers();
459-
this.wrapper?.nativeElement.removeEventListener('focus', this.onWrapperFocus);
460-
this.wrapper?.nativeElement.removeEventListener('blur', this.onWrapperBlur);
461456
}
462457

463458
/**

0 commit comments

Comments
 (0)