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 #5179

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 18 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,28 @@ export default class DatePicker extends Component<
}
};

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call can be simplified into this.input?.focus?.({ preventScroll: true })

🔹 Simplify Code (Nice to have)

Image of Jacob Jacob

}
}, 0);
};

safeBlur = () => {
setTimeout(() => {
if (this.input && this.input.blur) {
this.input.blur();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise into this.input?.blur?.()

🔹 Simplify Code (Nice to have)

Image of Jacob Jacob

Copy link
Author

@balajis-qb balajis-qb Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved the existing code into these new methods for the fix. I have updated the code as per your suggestion.

}
}, 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