Skip to content

Commit

Permalink
fix(components/datetime): datepicker calendar overlay prevents uninte…
Browse files Browse the repository at this point in the history
…nded keyboard navigation from reaching parent components (#1952)
  • Loading branch information
Blackbaud-CoreyArcher authored Jan 23, 2024
1 parent 1d3f528 commit 34c8941
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,16 @@ describe('datepicker', () => {
fixture.detectChanges();
tick();
clickTrigger(fixture);
let picker = getCalendar();

SkyAppTestUtility.fireDomEvent(window.document, 'keydown', {
SkyAppTestUtility.fireDomEvent(picker, 'keyup', {
customEventInit: {
key: 'escape',
},
});
fixture.detectChanges();
tick();
const picker = getCalendar();
picker = getCalendar();

expect(picker).toBeNull();
}));
Expand Down Expand Up @@ -406,15 +407,16 @@ describe('datepicker', () => {
fixture.detectChanges();
tick();
clickTrigger(fixture);
let picker = getCalendar();

SkyAppTestUtility.fireDomEvent(window.document, 'keydown', {
SkyAppTestUtility.fireDomEvent(picker, 'keydown', {
customEventInit: {
key: undefined,
},
});
fixture.detectChanges();
tick();
const picker = getCalendar();
picker = getCalendar();

expect(picker).not.toBeNull();
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export class SkyDatepickerComponent implements OnDestroy, OnInit {
if (value) {
this.#_calendarRef = value;

this.#addKeyupListener();

// Wait for the calendar component to render before gauging dimensions.
setTimeout(() => {
if (this.calendarRef) {
Expand Down Expand Up @@ -181,7 +183,7 @@ export class SkyDatepickerComponent implements OnDestroy, OnInit {

#overlay: SkyOverlayInstance | undefined;

#overlayKeydownListener: Subscription | undefined;
#overlayKeyupListener: Subscription | undefined;

#_calendarRef: ElementRef | undefined;

Expand Down Expand Up @@ -382,8 +384,6 @@ export class SkyDatepickerComponent implements OnDestroy, OnInit {
}
});

this.#addKeydownListener();

overlay.attachTemplate(this.calendarTemplateRef);

this.#overlay = overlay;
Expand All @@ -398,25 +398,29 @@ export class SkyDatepickerComponent implements OnDestroy, OnInit {
}
}

#addKeydownListener(): void {
this.#overlayKeydownListener = fromEvent<KeyboardEvent>(
window.document,
'keydown',
)
.pipe(takeUntil(this.#ngUnsubscribe))
.subscribe((event) => {
const key = event.key?.toLowerCase();
if (key === 'escape' && this.isOpen) {
this.#closePicker();
}
});
#addKeyupListener(): void {
const datepickerCalendarElement = this.calendarRef?.nativeElement;

if (datepickerCalendarElement) {
this.#overlayKeyupListener = fromEvent<KeyboardEvent>(
datepickerCalendarElement,
'keyup',
)
.pipe(takeUntil(this.#ngUnsubscribe))
.subscribe((event) => {
const key = event.key?.toLowerCase();
if (key === 'escape' && this.isOpen) {
this.#closePicker();
}
});
}
}

#removePickerEventListeners(): void {
this.#calendarUnsubscribe.next();
this.#calendarUnsubscribe.complete();
this.#calendarUnsubscribe = new Subject<void>();
this.#overlayKeydownListener?.unsubscribe();
this.#overlayKeyupListener?.unsubscribe();
}

#cancelCustomDatesSubscription(): void {
Expand Down

0 comments on commit 34c8941

Please sign in to comment.