Skip to content
Merged
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
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
Loading