Skip to content

Commit

Permalink
Merge pull request #4689 from mirus-ua/fix/swapRange-with-input-not-c…
Browse files Browse the repository at this point in the history
…losing

fix: popper dosen't close if swapRange prop was passed
  • Loading branch information
martijnrusschen authored Apr 15, 2024
2 parents 0d2765f + b9a83df commit d861078
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,11 @@ export default class DatePicker extends React.Component {

const { startDate, endDate } = this.props;

if (startDate && !endDate && !isDateBefore(date, startDate)) {
if (
startDate &&
!endDate &&
(this.props.swapRange || !isDateBefore(date, startDate))
) {
this.setOpen(false);
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/datepicker_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,25 @@ describe("DatePicker", () => {
expect(container.querySelector(".react-datepicker")).toBeNull();
});

it("should be closed after clicking day when startDate has a value (endDate is being selected) and swapRange prop was passed", () => {
const startDate = new Date("2021-01-01 00:00:00");
const endDate = null;
const { container } = render(
<DatePicker
swapRange
selectsRange
startDate={startDate}
endDate={endDate}
/>,
);
fireEvent.click(container.querySelector("input"));

const days = container.querySelectorAll(".react-datepicker__day");
const day = days[Math.floor(days.length / 2)];
fireEvent.click(day);
expect(container.querySelector(".react-datepicker")).toBeNull();
});

it("has clear button rendered when isClearable is true and startDate has value", () => {
const startDate = new Date("2021-01-01 00:00:00");
const endDate = new Date("2021-01-21 00:00:00");
Expand Down

0 comments on commit d861078

Please sign in to comment.