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: correctly disabled/enabled addOffset propsGetter with maxDate using the offsetValue #51

Conversation

susosamuel
Copy link
Contributor

@susosamuel susosamuel commented Feb 14, 2024

Hello, This is my attempt to fix the behaviour described in #46

I tried the approach that you, @Feshchenko, mentioned in your comments and used the offsetDate in order to achieve the desired behavior without needing to update the disabling logic.

This is an example of what the code does behind the scenes (the same happens in the videos attached below):

Situation 1 - Edge case maxDate:

  • Previously, the offsetDate was set to selectedDate but in my case:
    • Initial selectedDate = 20.1.2024
    • maxDate = 13.2024
    • (new) offsetDate = 13.1.2024 -> maxDate - 1 month (previously, the offset Date was 20.1.2024 which resulted in a disabled button)

Situation 2 - Edge case minDate:

  • initial selectedDate = 20.1.2024
  • minDate = 22.11.2023
  • offsetDate = 20.1.2024 - no change needed - same as the initial value
    -> Now, the user clicks on the previous button, what happens next:
    • selectedDate remains unchanged
    • minDate remains unchanged
    • (new) offsetDate = 22.12.2023 -> minDate + 1 month (previously, the offset Date would be set to 20.12.2023 which resulted in a disabled button as 20.12.2023 - 1 month is less than minDate 22.11.2023)

I'm also attaching the before and after showcases

Before:
https://github.com/rehookify/datepicker/assets/17575434/2b4209a9-072c-4679-a7f5-99692b6643ea

After:
https://github.com/rehookify/datepicker/assets/17575434/f8a20c7f-8153-40e9-9b94-0e70fc558820

Props setup from the video:
mode: 'single'
maxDate: 13.2.2024
minDate: 22.11.2024
selectedDates: [20.1.2024]

fyi @marianadrozdova @ebartunek

Copy link
Contributor

@Feshchenko Feshchenko left a comment

Choose a reason for hiding this comment

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

@susosamuel Great job! Thank you for your contribution
Left several comments and suggestion.
Also please bump version to 6.6.0 here https://github.com/rehookify/datepicker/blob/main/packages/datepicker/package.json#L3

expect(result.current.propGetters.addOffset({ months: 1 }).disabled).toBe(
true,
);
expect(
result.current.propGetters.subtractOffset({ months: 1 }).disabled,
).toBe(true);
).toBe(undefined);
Copy link
Contributor

Choose a reason for hiding this comment

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

The only thing I would like to see here is to get to the point when this become false. So you need to perform one extra click and maybe reload the state.

Copy link
Contributor Author

@susosamuel susosamuel Feb 16, 2024

Choose a reason for hiding this comment

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

I implemented the points from the other comments, but I'm struggling with this one. I'm not sure if I understand your goal correctly and at the same time I may be not proficient enough in the lib/test suite functionality to be able to implement this. Please can you give some guidance on how to do this ?

{ days, months, years }: DPOffsetValue,
dateEdge?: Date,
): Date => {
if (dateEdge) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer return first approach here

Suggested change
if (dateEdge) {
if (!dateEdge) return offsetDate;

It will reduce 1 lvl of ifs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 38 to 46
if (days && days !== 0) {
return calculateNewDateWithOffset(offsetDate, dateEdge, days, 'date');
}
if (months && months !== 0) {
return calculateNewDateWithOffset(offsetDate, dateEdge, months, 'month');
}
if (years && years !== 0) {
return calculateNewDateWithOffset(offsetDate, dateEdge, years, 'year');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Here we can simplify by adding default params during destructuring at the params

{ days = 0, months = 0, years = 0 }: DPOffsetValue,

Then your check will shrink to if (days > 0), if (month > 0) ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Choose a reason for hiding this comment

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

@susosamuel Thanks so much for the fix. @Feshchenko, thanks for the release. It was perfecting timing, thanks to both of you, for my project! There is one little bug that needs to be addressed from this fix. We should also check on the edge if it is either first or last in the array because now the first and last months in the calendar are enabled then after the click event are disabled. My project has asked to fix this in low priority. I can do an external fix in my UI but it would best to handle it in the library. I can make a PR in the coming weeks to fix this small bug.

@Feshchenko Feshchenko merged commit 95fdda4 into rehookify:main Feb 16, 2024
4 checks passed
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.

3 participants