Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ›πŸš€ Fix and Enhance Date Range Selection #4306

Closed
wants to merge 2 commits into from

Conversation

franmc01
Copy link
Contributor

@franmc01 franmc01 commented Oct 11, 2023

Steps to Reproduce πŸ•΅οΈβ€β™‚οΈ

  1. Visit the documentation and date range selection example:
    Navigate to the date range picker example in the documentation.

  2. Observe the second input (for endDate):
    Note that the days prior to the date selected in the first input are disabled, which may not be the desired behavior in some use cases.

  3. Select a start date (startDate) that is after the end date (endDate):
    The invalid range is allowed, posing a usability concern as users can select a start date that is after their chosen end date, which might not be intuitive or may not validate against business logic.

🚨 Issue:

  1. πŸ“† Invalid Date Ranges: The component allows users to select invalid date ranges when using two inputs for range selection.

  2. 🚫 Days Becoming Incorrectly Disabled: Selecting certain date ranges results in days becoming incorrectly disabled in the UI.

πŸ’‘ Proposed Solutions:

  1. πŸ”’ Prevent Invalid Range Selection: Implement logic to prevent the user from selecting an invalid date range by doing nothing (or potentially showing an error message) when they attempt to select an end date that is earlier than the start date and vice versa.

    if (selectsStart || selectsEnd) {
      if (isValid(new Date(startDate)) && isValid(new Date(endDate))) {
        if (isAfter(new Date(changedDate), new Date(endDate)) && selectsStart) {
          return;
        } else if (isBefore(new Date(changedDate), new Date(startDate)) && selectsEnd) {
          return;
        }
      }
    }
  2. πŸ”„ Equalizing Start and End Dates: Introduce logic that sets the end date equal to the start date if the user attempts to set a start date that is later than the end date, and vice versa.

  3. πŸ”„ Resetting the Opposite Input: If a user selects an invalid range, automatically reset the opposite input (reset end date if an invalid start date is selected, and vice versa).

  4. ✨ Allow Invalid Ranges Prop: Introduce a new prop allowInvalidRanges, which enables developers to control whether invalid date ranges can be set by the user. When true, invalid ranges are allowed, potentially enabling developers to handle this case in a way that suits their application, such as displaying a custom error message.

    <DatePicker
      selected={startDate}
      onChange={(date) => setStartDate(date)}
      selectsStart
      startDate={startDate}
      endDate={endDate}
      allowInvalidRanges={false}
    />

πŸ“ Additional Notes:

  • Developers must be able to select an approach that suits their application’s needs.
  • The "days becoming incorrectly disabled" issue needs to be addressed to ensure user experience remains consistent and logical.

πŸ“‹ Tasks:

  • Implement logic to prevent invalid date range selection.
  • Ensure days do not become incorrectly disabled.
  • Implement logic to equalize start and end dates upon invalid selection.
  • Implement logic to reset opposite input upon invalid selection.
  • Implement allowInvalidRanges prop to provide additional flexibility for developers.
  • Update documentation and examples to illustrate the usage of the new prop.

πŸ§ͺ Tests:

  • Ensure all existing tests pass.
  • Add new tests for the introduced logic and prop to ensure future changes do not break this functionality.

NOTE: Temporarily disable linter rules in tests due to React 18 updates

- Add logic to disallow the selection of an end date before the start date and vice versa in the date range picker.
- Temporarily disable linter rules in tests due to React 18 updates and
Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

βœ… This pull request was sent to the PullRequest network.


@frankops11 you can click here to see the review status or cancel the code review job.

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PullRequest Breakdown

Reviewable lines of change

+ 27
- 1

93% JavaScript
7% JavaScript (tests)

Type of change

Fix - These changes are likely to be fixing a bug or issue.

@codecov
Copy link

codecov bot commented Oct 11, 2023

Codecov Report

Merging #4306 (bb25295) into main (d770de8) will decrease coverage by 0.37%.
Report is 6 commits behind head on main.
The diff coverage is 18.18%.

❗ Current head bb25295 differs from pull request most recent head 104977f. Consider uploading reports for the commit 104977f to get more accurate results

@@            Coverage Diff             @@
##             main    #4306      +/-   ##
==========================================
- Coverage   96.54%   96.18%   -0.37%     
==========================================
  Files          27       27              
  Lines        2374     2384      +10     
  Branches      966      974       +8     
==========================================
+ Hits         2292     2293       +1     
- Misses         82       91       +9     
Files Coverage Ξ”
src/index.jsx 92.48% <18.18%> (-1.76%) ⬇️

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a concern left inline r.e. the catch block. We're also missing test coverage for the changes here.

Image of Graham C Graham C


Reviewed with ❀️ by PullRequest

src/index.jsx Outdated
this.setState({ inputValue: null });
}
} catch (error) {
console.log(error);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is catching the error actually the appropriate thing to do? Logging using console.log is perhaps also not appropriate. Either we should throw this error so the caller can see it, or at least use console.error. Entering this catch is also missing test coverage.

πŸ”Ί Bug (Critical)

Image of Graham C Graham C

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that a more appropriate log level would be better here. However that ties in with my point on understanding what errors are actually going to occur here.

Image of Ryan Ryan

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘‹ Hello everyone!

I would like to provide some clarifications regarding the use of the try...catch block in the previously shared code. πŸ™‡β€β™‚οΈ

Initially, I introduced this block in an attempt to pinpoint the invalid date issue that I was grappling with during the debugging phase. The idea was to ensure that, if any related error emerged, I could rapidly identify its origin. πŸ•΅οΈβ€β™‚οΈ

However, upon further review and additional testing, I was able to verify that that particular section of the code was not the root cause of the issue at hand, thus rendering the try...catch not only unnecessary but also disruptive to code fluidity and readability. πŸ§ͺπŸ”

Consequently, I have now removed this block from the code to preserve clarity and prevent confusion in future reviews. 🧹✨

I greatly appreciate your attention and sharp observation. Any feedback is always valuable! Moreover, have you had a chance to observe the error mentioned in the PR? Gaining insights from your impressions and awareness of this issue would be immensely helpful. πŸš€

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graham has already left some good feedback inline. I had an additional concern about the broad try-catch block without a clear picture of what errors we'll actually encounter.

Image of Ryan Ryan


Reviewed with ❀️ by PullRequest

src/index.jsx Outdated Show resolved Hide resolved
Eliminate the try...catch block that was initially introduced for debugging the invalid date issue, but was determined not to be the root cause and therefore is not needed.
Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're still missing test coverage. I have another comment left inline, too.

Image of Graham C Graham C


Reviewed with ❀️ by PullRequest

@@ -639,6 +646,23 @@ export default class DatePicker extends React.Component {
this.setState({ monthSelectedIn: monthSelectedIn });
}
}

if (selectsStart || selectsEnd) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we cleanup the logic here? The extra nesting and else ifs are unnecessary.

πŸ”Έ Simplify Code (Important)

Image of Graham C Graham C

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Could you explain me because are unnecessary?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The level of nesting and use of else. One can replace lines 650-664 with:

if ((selectsStart || selectsEnd) && isValid(new Date(startDate)) && isValid(new Date(endDate))) {
  if (isAfter(new Date(changedDate), new Date(endDate)) && selectsStart) {
    return;
  }

  if (isBefore(new Date(changedDate), new Date(startDate)) && selectsEnd) {
    return;
  }
}

Image of Graham C Graham C

@@ -1,3 +1,4 @@
/* eslint-disable react/no-deprecated */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martijnrusschen I wrote: Temporarily disable linter rules in tests due to React 18 updates

@@ -1,3 +1,4 @@
/* eslint-disable react/no-deprecated */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that more tests could be added to capture the fix and new behavior. At a minimum a test for the original bug would be good to prevent future regressions.

Image of Ryan Ryan


Reviewed with ❀️ by PullRequest

@franmc01 franmc01 closed this Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants