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

Revert "removes check if initial month was set before" #189

Merged
merged 1 commit into from
Nov 17, 2016

Conversation

viktoriyavulfson
Copy link
Contributor

This reverts commit 68e991a.

@viktoriyavulfson
Copy link
Contributor Author

@ljharb this PR reverts previous changes to initial month logic. Sorry about that, please merge this and I'll investigate a better way to solve my use case

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.1%) to 86.293% when pulling 478acbc on plangrid:master into c108674 on airbnb:master.

@majapw majapw merged commit a29c988 into react-dates:master Nov 17, 2016
@majapw
Copy link
Collaborator

majapw commented Nov 17, 2016

So conceivably, since we want this to happen only on open we could make our check the following:
if (this.props.hidden && !nextProps.hidden) {

However, this means that the initialVisibleMonth will be reset every time the DayPicker is opened and closed unless the parent changes the prop on open or close. Which is feasible, but puts a lot of responsibility and leaves us in this kind of half-controlled state.

What do you think @ljharb?

@ljharb ljharb added the semver-patch: fixes/refactors/etc Anything that's not major or minor. label Nov 17, 2016
@ljharb
Copy link
Member

ljharb commented Nov 17, 2016

Hmm - I think it makes sense for initialVisibleMonth to only matter on mount, or when it transitions from hidden to visible. All other times it would be ignored.

@majapw
Copy link
Collaborator

majapw commented Nov 17, 2016

well that's the current behavior. :/

@ljharb
Copy link
Member

ljharb commented Nov 17, 2016

In that case I'm not sure what @ViktoriyaSavkina's change is meant to accomplish. @ViktoriyaSavkina, could you elaborate?

@majapw
Copy link
Collaborator

majapw commented Nov 17, 2016

I mean, I think she explained it pretty thoroughly here @ljharb - #176 (comment)

I was brainstorming alternative solutions given that we had to revert the initial change.

@ljharb
Copy link
Member

ljharb commented Nov 17, 2016

Right, but in that case, you'd render it hidden, and then change the initialVisibleMonth and render it visible in the new location, no?

@majapw
Copy link
Collaborator

majapw commented Nov 17, 2016

The DayPicker is always rendered to the screen (it's not getting mounted and unmounted every time you open it for reasons) which is why this more problematic than it seems. I think that is part of the problem. The other is that then the onus is on the parent to update initialVisibleMonth on close so that it doesn't always reopen to the same month.

@ljharb
Copy link
Member

ljharb commented Nov 17, 2016

right but doesn't componentWillReceiveProps get called with different props when the calendar is shown in a different location?

@majapw
Copy link
Collaborator

majapw commented Nov 18, 2016

So I was brainstorming ideas for how to handle both @ViktoriyaSavkina's usecase while not introducing unnecessary complication to the users of DateRangePicker, and one idea that we came up with was adding a forceUpdate type prop. Basically, if you provide an initialVisibleMonth prop, that would get set exactly once and then the rest of the time the DayPicker would continue relying on internal state. If you needed to reset the visible month for some reason (you have some sort of external data that is changing and you would like the month it opens to to change), you would pass in forceVisibleMonthToUpdate or something similar and this would change the DayPicker view. What do you think?

@viktoriyavulfson
Copy link
Contributor Author

@majapw @ljharb to build on the idea of forceVisibleMonthToUpdate, do we really need an extra property for this? Maybe we could just check if previous value of initialMonth is different from the next value in componentWillRecieveProps?

componentWillReceiveProps(nextProps) {
    if (this.state.currentMonth && this.state.currentMonth != nextProps.initialVisibleMonth()) {
      this.setState({
        currentMonth: nextProps.initialVisibleMonth(),
      });
    }
  }

This way every time a parent component passes in different value (based on whatever logic) current month for the DayPicker will be updated. Passing the same value in won't make any changes to the initial value of the current month though.

@ljharb
Copy link
Member

ljharb commented Nov 18, 2016

@ViktoriyaSavkina hm, that's not a bad suggestion.

Overall, I'm beginning to think that the concept of "initial visible month" is problematic overall, because of the "initial" - because a prop is being used to initialize state, only once, and because this component needs to be concerned with the business logic about when that prop is copied to state and when not.

I'm beginning to think it might be better to deprecate/eliminate the initialVisibleMonth prop, and then replace it with a getVisibleMonth function prop - called on every render (with the currently about-to-be displayed month), and when it returns a moment object (including if it just returns the provided moment object), force that month to be visible - otherwise (when it returns a falsy value) do nothing to override the state. The prop would default to x => x.

That way, the parent fully controls when the visible month changes, is initially set, or does not change - and the datepicker doesn't need to be concerned with "initial", or with when the prop should be copied to state.

Thoughts?

@viktoriyavulfson
Copy link
Contributor Author

I like this idea! This way we can maintain existing behavior for the day picker navigation and customize what we want the initial month to be when getVisibleMonth is called without a parameter

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.

None yet

4 participants