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

DayPickerRangeController does not change visible month when startDate or endDate update from state value #2232

Open
AaronMcCloskey opened this issue Apr 19, 2023 · 4 comments

Comments

@AaronMcCloskey
Copy link

AaronMcCloskey commented Apr 19, 2023

react-dates version
react-dates@21.8.0

Describe the bug
In my component, the startDate & endDate props are powered by a select's value in a form. When a new option is selected the startDate & endDate do update as expected and are selected correctly in the range, however, the visible month does not change. I don't necessarily think this is a bug, but I am unsure if there is a way that if the startDate or endDate updates that the visible month jumps to the startDate month?

Source code (including props configuration)
Steps to reproduce the behavior:

When clicking on the Add 2 Months + element, it updates the startDate & endDate state that powers the startDate and endDate props on DayPickerRangeController, however, the visible month does not update in line with these changes.

const Calendar = (props) => {
  const incrementMonths = () => {
    props.onDatesChange({
      startDate:
        (props.startDate && props.startDate.add(2, "months")) ||
        moment(new Date()).add(2, "months"),
      endDate:
        (props.endDate && props.endDate.add(2, "months")) ||
        moment(new Date()).add(3, "months")
    });
  };

  return (
    <>
      <DayPickerRangeController
        startDate={props.startDate}
        endDate={props.endDate}
        numberOfMonths={2}
        noBorder
        onDatesChange={props.onDatesChange}
        onFocusChange={props.onFocusChange}
        initialVisibleMonth={() => props.startDate || moment()}
        focusedInput={props.focusedInput}
        hideKeyboardShortcutsPanel
        firstDayOfWeek={1}
        daySize={44}
        weekDayFormat="dd"
        horizontalMonthPadding={5}
      />
      <div
        style={{
          background: "red",
          padding: "20px",
          color: "white",
          cursor: "pointer"
        }}
        onClick={incrementMonths}
      >
        Add 2 Months +
      </div>
    </>
  );
};

export class AppWrapper extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      focusedInput: "startDate"
    };
  }

  onDatesChange = ({ startDate, endDate }) => {
    this.setState({ startDate, endDate });
  };

  onFocusChange = (focusedInput) => {
    this.setState({
      focusedInput: !focusedInput ? "startDate" : focusedInput
    });
  };

  render() {
    return (
      <Calendar
        onDatesChange={this.onDatesChange}
        onFocusChange={this.onFocusChange}
        focusedInput={this.state.focusedInput}
        startDate={this.state.startDate}
        endDate={this.state.endDate}
      />
    );
  }
}

Edit react-date-demo-daypickerrangecontroller (forked)

Screenshots/Gifs
https://user-images.githubusercontent.com/20436343/233184001-a85714b8-c2e5-4cb0-a756-4cf13e198ebf.mov

Desktop (please complete the following information):

  • OS: MacOS Monterey 12.6
  • Browser Chrome
  • Version 111.0.5563.146 (Official Build) (x86_64)

Additional context
Any help would be greatly appreciated.

@roildev
Copy link

roildev commented Apr 20, 2023

the same issue

@davemjones
Copy link

I've got the same issue with the component.

@Rliulinz
Copy link

Rliulinz commented Aug 8, 2023

Requires this feature as well, also it will be good if clicks on the date input, the calendar could shuffle to the month which has the start date or end date according to the focused input.

@fcastaneda9725
Copy link

Any update here?

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

No branches or pull requests

5 participants