2
votes

In my code I've used one of the npm react components for date range picker (https://www.npmjs.com/package/react-date-range). In my screen I've an apply dates button. If i click that button i want to print date range value. How to get that range value onclick?

    var Calendarcomponent = React.createClass({
    getInitialState: function(){
        this.state = {
          'rangePicker' : {},
          'linked' : {},
          'datePicker' : null,
          'firstDayOfWeek' : null,
          'predefined' : {},
        }
    }
  handleSelect:function(range){
    console.log(range);
  },
  handleClick:function(range){
    console.log(range);
  },
  render:function(){
    var rangePicker = this.state;
    var format = 'dddd, D MMMM YYYY';
      return (
        <div>
            <h4>FILTER BY DATE</h4>
            <div className="date-range text-center">
                <div className="group">      
<label>Date range</label>
                    <input className="inputMaterial" type="text" value={ rangePicker['endDate'] && rangePicker['endDate'].format(format).toString() } required />
                </div>
            </div>
            <div className="set-borrow-dates">
              <div id="select-date">Select Borrow date</div>
              <DateRange onInit={this.handleSelect} onclick={this.handleClick} />
              <button id="apply-dates"><i className="fa fa-check-circle"></i> Apply Dates</button>
            </div>
        </div>
      )
  }
});
1
@azium. I have updated the code. Can you please check now?Sathya
Can you post a fiddle? (using jsfiddle.net or snippet)Mosh Feu
Does your handleSelect function log range as you expected?azium
no @azium. but i got this value 'Object {startDate: Moment, endDate: Moment}'Sathya
Hi @MoshFeu. Please check above updated code. Is there any way get start and end date value on click?Sathya

1 Answers

0
votes

You can use the onChange callback to store the range in state and read it when clicking the button:

handleSelect(range){
  this.setState({ range });
},

handleClick(){
  console.log(this.state.range);
},

render:function(){
  return <div>
    <DateRange onInit={this.handleSelect}
     onChange={this.handleSelect} />
    <button id="apply-dates" onClick={this.handleClick} >
      <i className="fa fa-check-circle"></i>
      Apply Dates
    </button>
  </div>;
}