Skip to content

Fix issue where range doesn't clear on invisible months#575

Merged
majapw merged 2 commits intomasterfrom
maja-fix-issue-where-range-doesnt-clear-on-invisible-month
Jun 28, 2017
Merged

Fix issue where range doesn't clear on invisible months#575
majapw merged 2 commits intomasterfrom
maja-fix-issue-where-range-doesnt-clear-on-invisible-month

Conversation

@majapw
Copy link
Copy Markdown
Collaborator

@majapw majapw commented Jun 16, 2017

Fix for #567

I think this is a more bullet-proof solution than #574

It took me quite a while to figure out how to get this working for the enableOutsideDays prop, but I think this solution is holistic and does not have outside side effects. The changes to addModifier help to update invisible days if you happen to click on an "outside day".

PTAL @airbnb/webinfra @acr13

@majapw majapw added the semver-patch: fixes/refactors/etc Anything that's not major or minor. label Jun 16, 2017
@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.08%) to 86.35% when pulling e9a3485 on maja-fix-issue-where-range-doesnt-clear-on-invisible-month into 0c14a9d on master.

@majapw majapw force-pushed the maja-fix-issue-where-range-doesnt-clear-on-invisible-month branch 2 times, most recently from e9a3485 to a479c4f Compare June 16, 2017 22:57
@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.08%) to 86.35% when pulling a479c4f on maja-fix-issue-where-range-doesnt-clear-on-invisible-month into 0c14a9d on master.

const iso = toISODateString(day);

let updatedDaysAfterAddition = { ...updatedDays };
if (enableOutsideDays) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@ljharb This first change is in the addModifier method. The basic premise is that if outside days are enabled that means that days at the beginning and end of the month will exist as a part of two separate CalendarMonth objects. As a result if you click on a day, you may want to add modifiers to more than one month instance in the state. This is what this branch of the conditional accomplishes.

const iso = toISODateString(day);

let updatedDaysAfterAddition = { ...updatedDays };
if (enableOutsideDays) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@ljharb This first change is in the addModifier method. The basic premise is that if outside days are enabled that means that days at the beginning and end of the month will exist as a part of two separate CalendarMonth objects. As a result if you click on a day, you may want to add modifiers to more than one month instance in the state. This is what this branch of the conditional accomplishes.

const iso = toISODateString(day);

let updatedDaysAfterDeletion = { ...updatedDays };
if (enableOutsideDays) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This addition to the deleteModifier method is very similar to what happens in addModifier. Namely, for outside days, sometimes a selected day might be in two CalendarMonths and we need to remove the modifiers in both cases.


let currentMonth = firstVisibleMonth;
let numberOfMonths = numberOfVisibleMonths;
if (orientation !== VERTICAL_SCROLLABLE) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

currentMonth and numberOfMonths only apply to the visible months and not to those hidden for animation. However, we want to make sure modifiers are updates even in the invisible months so we expand the range here. ... I didn't think we needed this when adding modifiers but on second thought I think we do (there's like this edge case where you select a start date, scroll it off screen, select and end date and scroll back).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This if logic (ie, getting currentMonth and numberOfMonths) is repeated in DayPickerRangeController, and in SingleDatePicker. Could we move it to a shared function instead?

@majapw
Copy link
Copy Markdown
Collaborator Author

majapw commented Jun 17, 2017

@ljharb I added comments here as well to help guide the review!

@majapw majapw force-pushed the maja-fix-issue-where-range-doesnt-clear-on-invisible-month branch from a479c4f to 2953125 Compare June 17, 2017 06:26
@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) to 86.452% when pulling 2953125 on maja-fix-issue-where-range-doesnt-clear-on-invisible-month into 0c14a9d on master.


let currentMonth = firstVisibleMonth;
let numberOfMonths = numberOfVisibleMonths;
if (orientation !== VERTICAL_SCROLLABLE) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This if logic (ie, getting currentMonth and numberOfMonths) is repeated in DayPickerRangeController, and in SingleDatePicker. Could we move it to a shared function instead?

Object.keys(visibleDays[monthKey]).indexOf(iso) > -1
));

updatedDaysAfterAddition = monthsToUpdate.reduce((days, monthIso) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

also one or both branches of this if/else seem repeated in both components; could they also be shared?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think it is complicated to share these methods because a lot of these variables depend on state and/or props. I think the dream scenario is eventually to have a withModifiers HOC of some sort that could do a lot of this stuff on its own.

Copy link
Copy Markdown
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Per discussion, LGTM for now, and there should be a followup to consolidate all the repeated code <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver-patch: fixes/refactors/etc Anything that's not major or minor.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants