Skip to content

Commit

Permalink
Merge pull request #5179 from qburst/issue-5084/fix/firefox-tab-issue
Browse files Browse the repository at this point in the history
🐛 Fix #5084: Fixfox Tab switch issue
  • Loading branch information
martijnrusschen authored Oct 21, 2024
2 parents 5cbaa12 + 2019c8e commit cdc3dfa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
21 changes: 14 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,24 @@ export default class DatePicker extends Component<
}
};

safeFocus = () => {
setTimeout(() => {
this.input?.focus?.({ preventScroll: true });
}, 0);
};

safeBlur = () => {
setTimeout(() => {
this.input?.blur?.();
}, 0);
};

setFocus = () => {
if (this.input && this.input.focus) {
this.input.focus({ preventScroll: true });
}
this.safeFocus();
};

setBlur = () => {
if (this.input && this.input.blur) {
this.input.blur();
}

this.safeBlur();
this.cancelFocusInput();
};

Expand Down
29 changes: 25 additions & 4 deletions src/test/datepicker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,24 @@ describe("DatePicker", () => {
});
});

it("should hide the calendar when the pressing Shift + Tab in the date input", () => {
it("should auto-close the datepicker and lose focus when Tab key is pressed when the date input is focused", async () => {
const { container } = render(<DatePicker />);
const input = safeQuerySelector(container, "input");
fireEvent.focus(input);

let reactCalendar = container.querySelector("div.react-datepicker");
expect(reactCalendar).not.toBeNull();

fireEvent.keyDown(input, getKey(KeyType.Tab));

reactCalendar = container.querySelector("div.react-datepicker");
expect(reactCalendar).toBeNull();
await waitFor(() => {
expect(document.activeElement).not.toBe(input);
});
});

it("should hide the calendar when the pressing Shift + Tab in the date input", async () => {
// eslint-disable-next-line prefer-const
let onBlurSpy: ReturnType<typeof jest.spyOn>;
const onBlur: React.FocusEventHandler<HTMLElement> = (
Expand All @@ -594,7 +611,9 @@ describe("DatePicker", () => {
fireEvent.focus(input);
fireEvent.keyDown(input, getKey(KeyType.Tab, true));
expect(container.querySelector(".react-datepicker")).toBeNull();
expect(onBlurSpy).toHaveBeenCalled();
await waitFor(() => {
expect(onBlurSpy).toHaveBeenCalled();
});
});

it("should not apply the react-datepicker-ignore-onclickoutside class to the date input when closed", () => {
Expand Down Expand Up @@ -1964,7 +1983,7 @@ describe("DatePicker", () => {
});
expect(div.querySelector("input")).toBe(document.activeElement);
});
it("should autoFocus the input when calling the setFocus method", () => {
it("should autoFocus the input when calling the setFocus method", async () => {
const div = document.createElement("div");
document.body.appendChild(div);
let instance: DatePicker | null = null;
Expand All @@ -1982,7 +2001,9 @@ describe("DatePicker", () => {
act(() => {
instance!.setFocus();
});
expect(div.querySelector("input")).toBe(document.activeElement);
await waitFor(() => {
expect(div.querySelector("input")).toBe(document.activeElement);
});
});
it("should clear preventFocus timeout id when component is unmounted", () => {
const div = document.createElement("div");
Expand Down

0 comments on commit cdc3dfa

Please sign in to comment.