Skip to content

Commit

Permalink
Fix condition preventing props.onFocus execution
Browse files Browse the repository at this point in the history
  • Loading branch information
dshster committed Jan 14, 2025
1 parent 6c6e8d4 commit 1285af4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,13 @@ export default class DatePicker extends Component<
this.resetHiddenStatus();
}

if (!this.state.preventFocus && isOpenAllowed) {
if (!this.state.preventFocus) {
this.props.onFocus?.(event);
if (!this.props.preventOpenOnFocus && !this.props.readOnly) {
if (
isOpenAllowed &&
!this.props.preventOpenOnFocus &&
!this.props.readOnly
) {
this.setOpen(true);
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/datepicker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ describe("DatePicker", () => {
expect(container.querySelector(".react-datepicker")).toBeFalsy();
});

it("should be executed props.onFocus on input focus when the document visibility changes", () => {
const onFocusSpy = jest.fn();

const { container } = render(<DatePicker onFocus={onFocusSpy} />);

const input = safeQuerySelector<HTMLInputElement>(container, "input");

fireEvent.focus(input);
expect(onFocusSpy).toHaveBeenCalled();

expect(container.querySelector(".react-datepicker")).toBeTruthy();
fireEvent.keyDown(input, getKey(KeyType.Escape));
fireEvent.blur(input);
expect(container.querySelector(".react-datepicker")).toBeFalsy();

hideDocument(input);
showDocument(input);

expect(onFocusSpy).toHaveBeenCalled();
expect(container.querySelector(".react-datepicker")).toBeFalsy();

fireEvent.click(input);
expect(onFocusSpy).toHaveBeenCalled();
expect(container.querySelector(".react-datepicker")).toBeTruthy();
});

it("should show the calendar when focusing on the date input", () => {
const { container } = render(<DatePicker />);

Expand Down

0 comments on commit 1285af4

Please sign in to comment.