Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix #5084: Fixfox Tab switch issue #35

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading