1

I'm using Airbnb react-dates. I've added it to my project and it's working fine, the component looks like this:

<DateRangePicker
    startDate={this.state.startDate} // momentPropTypes.momentObj or null,
    startDateId="your_unique_start_date_id" // PropTypes.string.isRequired,
    endDate={this.state.endDate} // momentPropTypes.momentObj or null,
    endDateId="your_unique_end_date_id" // PropTypes.string.isRequired,
    onDatesChange={this.onDatesChange} // PropTypes.func.isRequired,
    focusedInput={this.state.focusedInput} // PropTypes.oneOf([START_DATE, END_DATE]) or null,
    onFocusChange={focusedInput => this.setState({ focusedInput })} // PropTypes.func.isRequired,
    daySize={50}
    noBorder={true}
    isOutsideRange={() => false}
  />

I just added it to my project, and as you can see there is my method onDatesChange and everything is fine, but I'm wondering how can I trigger some method when END_DATE is selected.

For example, I would like to filter something when the end date is touched...

sergdenisov
  • 8,327
  • 9
  • 48
  • 63
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102

2 Answers2

2

You need to add callback in onDatesChange property:

<DateRangePicker
  {...dateRangePickerProps}
  onDatesChange={({ endDate }) => {
    console.log("End Date change callback"); // Your callback
  }}
/>;

Dennis Vash
  • 50,196
  • 9
  • 100
  • 118
2

You need to use onFocusChange and onDatesChange props of <DateRangePicker>, and also componentDidUpdate() React lifecycle method:

CodeSandbox

import React, { Component } from "react";
import "react-dates/initialize";
import "react-dates/lib/css/_datepicker.css";
import { DateRangePicker } from "react-dates";
import { START_DATE, END_DATE } from "react-dates/constants";

export default class Dates extends Component {
  state = {
    startDate: null,
    endDate: null,
    focusedInput: null
  };

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

  onFocusChange = focusedInput => this.setState({ focusedInput });

  componentDidUpdate(prevProps, prevState) {
    if (
      prevState.focusedInput !== this.state.focusedInput &&
      this.state.focusedInput === END_DATE
    ) {
      alert("End date is focused!"); // your code here
    }

    if (prevState.endDate !== this.state.endDate) {
      alert("End date is changed!"); // your code here
    }
  }

  render() {
    const { startDate, endDate, focusedInput } = this.state;

    return (
      <DateRangePicker
        startDate={startDate}
        startDateId={START_DATE}
        endDate={endDate}
        endDateId={END_DATE}
        onDatesChange={this.onDatesChange}
        focusedInput={focusedInput}
        onFocusChange={this.onFocusChange}
      />
    );
  }
}
sergdenisov
  • 8,327
  • 9
  • 48
  • 63
  • Yes it helped, thanks! I modified code but you got the point! :) Thanks a lot, soon I'll post another question so you might probably help me, I'll leave a link here soon, thanks again! :) – Roxy'Pro Jul 03 '19 at 13:11
  • @Roxy'Pro I'm glad to help! – sergdenisov Jul 03 '19 at 15:12
  • https://stackoverflow.com/questions/56871089/how-to-prevent-user-from-selecting-date-above-end-date-airbnb-daterange-picker/56872468#56872468 – Roxy'Pro Jul 03 '19 at 15:14