Skip to content

Commit

Permalink
fix: input wasn't close if it uses swapRange prop
Browse files Browse the repository at this point in the history
  • Loading branch information
mirus-ua committed Apr 15, 2024
1 parent 0d2765f commit b9a83df
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 b9a83df

Please sign in to comment.