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

Calendar did not update marking date after select #194

Closed
sonnguyenit opened this issue Oct 2, 2017 · 5 comments
Closed

Calendar did not update marking date after select #194

sonnguyenit opened this issue Oct 2, 2017 · 5 comments

Comments

@sonnguyenit
Copy link

sonnguyenit commented Oct 2, 2017

I want to use this calendar for my app with range select.
My solution is:

  • Users first select date to mark it from date. (startingdate)
  • User select second date to mark it to date. (endingdate)
  • Then the date between them also marked.

here is my code:

constructor(props) {
	  super(props);
	  this.state = {
	  	isFromDatePicked: false,
	  	isToDatePicked: false,
      FromDate: '',
      ToDate: '',
      markedDates: {}
	  };
	}
onDayPress(day) {
      let markedDates = this.state.markedDates;
      if(this.state.isFromDatePicked == false){
        markedDates[day.dateString] = [{startingDay: true, color: '#AED8D6'}];
        console.log(markedDates);
        this.setState({
          isFromDatePicked: true,
          FromDate: day.dateString,
          markedDates: markedDates
        });
      }else {
        //Check if toDate not yet check
        if(this.state.isToDatePicked == false){//not checked yet
          let fromDate = new XDate(this.state.FromDate);
          let toDate = new XDate(day.dateString);
          let range = fromDate.diffDays(toDate);
          if( range> 0){
            for (var i = 1; i <= range; i++) {
              //let tempFromDate = fromDate;
              let tempDate = fromDate.addDays(1);
              if(i < range){
                markedDates[tempDate.toString('yyyy-MM-dd')] = [{ color: '#AED8D6'}];
              }else{
                markedDates[tempDate.toString('yyyy-MM-dd')] = [{endingDay: true, color: '#AED8D6'}];
              }
            }
            this.setState({
              isToDatePicked: true,
              ToDate: day.dateString,
              markedDates: markedDates
            });
          }else{
            alert('To Date must greater than From Date');
          }

        }else{
          //do nothing
        }

      }
	  }

<Calendar
	 		minDate={new XDate()}
      onDayPress={(day) => {this.onDayPress(day)}}
      style={styles.calendar}
      hideExtraDays
      markedDates={this.state.markedDates}
      markingType={'interactive'}
	    />

This code seem to be work fine as it logic, but the UI did not change immediate, i must press the arrow to go to next month and press arrow back to see the UI updated.
What about the missing code here to update the UI? Or can we use a trigger to update UI after select dates?

@shapovalstas
Copy link

Hey, @sonnguyenit! Have you solved this problem?

@shapovalstas
Copy link

Hey, @tautvilas! Could you help with the solution of this problem?

@shapovalstas
Copy link

let markedDates = {...this.state.markedDates} should fix your problem

@tautvilas
Copy link
Contributor

Thx @sshapoval . The trick is that markedDates should be treated as immutable

@lazaronixon
Copy link

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

4 participants