Skip to content

Commit d07308d

Browse files
author
Balaji Sridharan
committed
🐛 Fix the Esc key input refocus issue by deferring the focus operation to the next event loop
- This issue is caused due to the defer blur made in the commit 3682222 is getting executed after the focus operation for the ESC key press Close #5084
1 parent 3682222 commit d07308d

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/index.tsx

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

455+
safeFocus = () => {
456+
setTimeout(() => {
457+
if (this.input && this.input.focus) {
458+
this.input.focus({ preventScroll: true });
459+
}
460+
}, 0);
461+
};
462+
455463
safeBlur = () => {
456464
setTimeout(() => {
457465
if (this.input && this.input.blur) {
@@ -461,9 +469,7 @@ export default class DatePicker extends Component<
461469
};
462470

463471
setFocus = () => {
464-
if (this.input && this.input.focus) {
465-
this.input.focus({ preventScroll: true });
466-
}
472+
this.safeFocus();
467473
};
468474

469475
setBlur = () => {

src/test/datepicker_test.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,7 @@ describe("DatePicker", () => {
19831983
});
19841984
expect(div.querySelector("input")).toBe(document.activeElement);
19851985
});
1986-
it("should autoFocus the input when calling the setFocus method", () => {
1986+
it("should autoFocus the input when calling the setFocus method", async () => {
19871987
const div = document.createElement("div");
19881988
document.body.appendChild(div);
19891989
let instance: DatePicker | null = null;
@@ -2001,7 +2001,9 @@ describe("DatePicker", () => {
20012001
act(() => {
20022002
instance!.setFocus();
20032003
});
2004-
expect(div.querySelector("input")).toBe(document.activeElement);
2004+
await waitFor(() => {
2005+
expect(div.querySelector("input")).toBe(document.activeElement);
2006+
});
20052007
});
20062008
it("should clear preventFocus timeout id when component is unmounted", () => {
20072009
const div = document.createElement("div");

0 commit comments

Comments
 (0)