From b9a83df5c75dc045862ad601858f98a5543b65d4 Mon Sep 17 00:00:00 2001 From: Mirus Date: Mon, 15 Apr 2024 11:34:52 +0300 Subject: [PATCH] fix: input wasn't close if it uses swapRange prop --- src/index.jsx | 6 +++++- test/datepicker_test.test.js | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/index.jsx b/src/index.jsx index 5168b25d3..9f572cf3a 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -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); } } diff --git a/test/datepicker_test.test.js b/test/datepicker_test.test.js index bc51ebcb1..efca15374 100644 --- a/test/datepicker_test.test.js +++ b/test/datepicker_test.test.js @@ -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( + , + ); + 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");