mengxiong10 / vue2-datepicker

A datepicker / datetimepicker component for Vue2
https://mengxiong10.github.io/vue2-datepicker/index.html
MIT License
1.52k stars 407 forks source link

How to make calendar-range months always adjacent? #682

Closed IgorShayderov closed 2 years ago

IgorShayderov commented 2 years ago

Hello. I'm trying to make calendar-range's months adjacent. Whenever user picked date or navigated to next month using buttons, months should be adjacent. Like this:

(October, November)

Screenshot 2022-01-19 at 23 22 37

This is my code:

import DatePicker from 'vue2-datepicker';

const { updateCalendars } = DatePicker.CalendarRange.methods;

DatePicker.CalendarRange.methods.updateCalendars = function (
  calendars,
  adjustIndex = 1
) {
  const [newStartDate, newEndDate] = calendars;
  const [oldStartDate, oldEndDate] = this.calendars;

  if (this.calendars.length === 0 || newStartDate !== oldStartDate) {
    const newStartDateMonth = newStartDate.getMonth();
    const newStartDateYear = newStartDate.getFullYear();
    const nextDate = new Date(newStartDateYear, newStartDateMonth + 1);

    this.calendars[0] = newStartDate;
    this.calendars[1] = nextDate;
  } else if (newEndDate !== oldEndDate) {
    const newEndDateMonth = newEndDate.getMonth();
    const newEndDateYear = newEndDate.getFullYear();
    const previousDate = new Date(newEndDateYear, newEndDateMonth - 1);

    this.calendars[0] = previousDate;
    this.calendars[1] = newEndDate;
  }
};

When I look at this.calendars I see that everything is perfect, but in real one side of calendar every time don't updating. I have tried $forceUpdate() but it doesn't work.

mengxiong10 commented 2 years ago

It's a problem for vue2 https://vuejs.org/v2/guide/reactivity.html#For-Arrays.

    // fix it
    // this.calendars[0] = newStartDate;
    // this.calendars[1] = nextDate;
    this.calendars = [newStartDate, nextDate]
IgorShayderov commented 2 years ago

@mengxiong10 Thank you very much! It works.