Repository: Hacker0x01/react-datepicker (ts)

Issue:
Uncaught Error: Invalid date received
**Describe the bug**
If the user writes a date and deletes everything with CRTL+A, it ends up with an error.
I identified this in the date picker range component, by typing only the start date and deleting everything right after

> [!CAUTION]
> Uncaught Error: Invalid date received

**To Reproduce**
Steps to reproduce the behavior:

1. Go to [https://reactdatepicker.com/#example-date-range-using-input-with-clear-button](https://reactdatepicker.com/#example-date-range-using-input-with-clear-button)
2. Click on **input** (Date Range using input with clear button)
3. Select start date
4. Clear all

**Screenshots**
![datepickerange](https://github.com/Hacker0x01/react-datepicker/assets/37590954/3330808e-0486-4d19-92e5-2e9527df688f)
![datepickerange-1](https://github.com/Hacker0x01/react-datepicker/assets/37590954/847b0ee7-0c0b-487f-9aed-d01dbc3084fc)


**Desktop (please complete the following information):**

- OS: Windows 10
- Browser Edge
- Version [latest]

Pull request description:
fix: #4594 fix setSelected

## Description
**Linked issue**: close #4594

**Problem**
See issue #4594

**Changes**
Added null check because passing null as the first argument of isBefore causes an error.

## Screenshots
<!-- If applicable, add screenshots to help explain your improvements -->

## To reviewers
<!-- Additional comments for reviewers -->

## Contribution checklist
- [x] I have followed the [contributing guidelines](https://github.com/Hacker0x01/react-datepicker/blob/main/CONTRIBUTING.md).
- [x] I have added sufficient test coverage for my changes.
- [x] I have formatted my code with Prettier and checked for linting issues with ESLint for code readability.


Patch:
diff --git a/src/index.jsx b/src/index.jsx
index b4059d36..0c41e6c4 100644
--- a/src/index.jsx
+++ b/src/index.jsx
@@ -697,7 +697,9 @@ export default class DatePicker extends React.Component {
         if (noRanges) {
           onChange([changedDate, null], event);
         } else if (hasStartRange) {
-          if (isDateBefore(changedDate, startDate)) {
+          if (changedDate === null) {
+            onChange([null, null], event);
+          } else if (isDateBefore(changedDate, startDate)) {
             onChange([changedDate, null], event);
           } else {
             onChange([startDate, changedDate], event);


Test patch:
diff --git a/test/datepicker_test.test.js b/test/datepicker_test.test.js
index 139e14ad..0eb71222 100644
--- a/test/datepicker_test.test.js
+++ b/test/datepicker_test.test.js
@@ -1444,6 +1444,30 @@ describe("DatePicker", () => {
     });
     expect(cleared).toBe(true);
   });
+  it("should correctly clear date with empty input string (selectsRange)", () => {
+    let instance;
+    const onChangeSpy = jest.fn();
+
+    render(
+      <DatePicker
+        ref={(node) => {
+          instance = node;
+        }}
+        selectsRange
+        startDate={utils.newDate("2016-11-22")}
+        endDate={null}
+        onChange={onChangeSpy}
+        isClearable
+      />,
+    );
+    fireEvent.change(instance.input, {
+      target: {
+        value: "",
+      },
+    });
+
+    expect(onChangeSpy.mock.calls.at(-1)[0]).toStrictEqual([null, null]);
+  });
   it("should correctly update the input when the value prop changes", () => {
     let instance;
     const { rerender } = render(