Skip to content

Commit cdc3dfa

Browse files
Merge pull request #5179 from qburst/issue-5084/fix/firefox-tab-issue
🐛 Fix #5084: Fixfox Tab switch issue
2 parents 5cbaa12 + 2019c8e commit cdc3dfa

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

src/index.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -452,17 +452,24 @@ export default class DatePicker extends Component<
452452
}
453453
};
454454

455+
safeFocus = () => {
456+
setTimeout(() => {
457+
this.input?.focus?.({ preventScroll: true });
458+
}, 0);
459+
};
460+
461+
safeBlur = () => {
462+
setTimeout(() => {
463+
this.input?.blur?.();
464+
}, 0);
465+
};
466+
455467
setFocus = () => {
456-
if (this.input && this.input.focus) {
457-
this.input.focus({ preventScroll: true });
458-
}
468+
this.safeFocus();
459469
};
460470

461471
setBlur = () => {
462-
if (this.input && this.input.blur) {
463-
this.input.blur();
464-
}
465-
472+
this.safeBlur();
466473
this.cancelFocusInput();
467474
};
468475

src/test/datepicker_test.test.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,24 @@ describe("DatePicker", () => {
580580
});
581581
});
582582

583-
it("should hide the calendar when the pressing Shift + Tab in the date input", () => {
583+
it("should auto-close the datepicker and lose focus when Tab key is pressed when the date input is focused", async () => {
584+
const { container } = render(<DatePicker />);
585+
const input = safeQuerySelector(container, "input");
586+
fireEvent.focus(input);
587+
588+
let reactCalendar = container.querySelector("div.react-datepicker");
589+
expect(reactCalendar).not.toBeNull();
590+
591+
fireEvent.keyDown(input, getKey(KeyType.Tab));
592+
593+
reactCalendar = container.querySelector("div.react-datepicker");
594+
expect(reactCalendar).toBeNull();
595+
await waitFor(() => {
596+
expect(document.activeElement).not.toBe(input);
597+
});
598+
});
599+
600+
it("should hide the calendar when the pressing Shift + Tab in the date input", async () => {
584601
// eslint-disable-next-line prefer-const
585602
let onBlurSpy: ReturnType<typeof jest.spyOn>;
586603
const onBlur: React.FocusEventHandler<HTMLElement> = (
@@ -594,7 +611,9 @@ describe("DatePicker", () => {
594611
fireEvent.focus(input);
595612
fireEvent.keyDown(input, getKey(KeyType.Tab, true));
596613
expect(container.querySelector(".react-datepicker")).toBeNull();
597-
expect(onBlurSpy).toHaveBeenCalled();
614+
await waitFor(() => {
615+
expect(onBlurSpy).toHaveBeenCalled();
616+
});
598617
});
599618

600619
it("should not apply the react-datepicker-ignore-onclickoutside class to the date input when closed", () => {
@@ -1964,7 +1983,7 @@ describe("DatePicker", () => {
19641983
});
19651984
expect(div.querySelector("input")).toBe(document.activeElement);
19661985
});
1967-
it("should autoFocus the input when calling the setFocus method", () => {
1986+
it("should autoFocus the input when calling the setFocus method", async () => {
19681987
const div = document.createElement("div");
19691988
document.body.appendChild(div);
19701989
let instance: DatePicker | null = null;
@@ -1982,7 +2001,9 @@ describe("DatePicker", () => {
19822001
act(() => {
19832002
instance!.setFocus();
19842003
});
1985-
expect(div.querySelector("input")).toBe(document.activeElement);
2004+
await waitFor(() => {
2005+
expect(div.querySelector("input")).toBe(document.activeElement);
2006+
});
19862007
});
19872008
it("should clear preventFocus timeout id when component is unmounted", () => {
19882009
const div = document.createElement("div");

0 commit comments

Comments
 (0)