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
7 changes: 5 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,13 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
};

handleCalendarClickOutside = (event: MouseEvent) => {
if (!this.props.inline) {
// Call user's onClickOutside first, allowing them to call preventDefault()
this.props.onClickOutside?.(event);

// Only close if not prevented and not inline
if (!this.props.inline && !event.defaultPrevented) {
this.setOpen(false);
}
this.props.onClickOutside?.(event);
if (this.props.withPortal) {
event.preventDefault();
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/datepicker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2793,6 +2793,28 @@ describe("DatePicker", () => {
expect(onClickOutsideSpy).toHaveBeenCalledTimes(1);
});

it("should not close date picker when onClickOutside calls preventDefault", () => {
const onClickOutside = (event: MouseEvent) => {
event.preventDefault();
};
const { container } = render(
<div>
<span className="outsideElement">outside</span>
<DatePicker onClickOutside={onClickOutside} />
</div>,
);

const input = safeQuerySelector(container, "input");
fireEvent.focus(input);
expect(container.querySelector(".react-datepicker")).not.toBeNull();

const outsideElement = safeQuerySelector(container, ".outsideElement");
fireEvent.mouseDown(outsideElement);

// Calendar should remain open because preventDefault was called
expect(container.querySelector(".react-datepicker")).not.toBeNull();
});

it("should not close date picker on input click", () => {
const onClickOutsideSpy = jest.fn();
const { container } = render(
Expand Down
Loading