mymth / vanillajs-datepicker

A vanilla JavaScript remake of bootstrap-datepicker for Bulma and other CSS frameworks
MIT License
719 stars 147 forks source link

Do not disable nextButton when maxDate is set #147

Closed softail closed 10 months ago

softail commented 1 year ago

Hey, I've looked around docs and issues, but could not find the answer. I have an odd requirement to still show the future months to users, even though maxDate is used.

For example on this screen below i have set maxDate to 1st of June and I want to still click on nextButton, is this possible?

image

mymth commented 1 year ago

How about using datesDisabled with callback function, like this?

var datepicker = new Datepicker(elem, {
  datesDisabled(date, viewId) {
    if (viewId > 0) {
      return;
    }
    const dt = new Date();
    const maxDateVal = dt.setDate(dt.getDate() + 14);
    return date.getTime() > maxDateVal;
  },
});

https://codepen.io/mymth/pen/wvYZzqe

If you use v1.2 or earlier, using beforeShowDay would work similar, but it only prevents users from clicking the date—users can enter the date by typing it into the input field. So, there's no true solution in this case, unfortunately.

softail commented 1 year ago

I use an array of dates for datesDisabled, but beforeShowDay function did the job thanks!

mymth commented 1 year ago

Just in case you aren't aware of this, disabling dates (or months/years/decades) with the beforeShow*** hook is deprecated and will be removed. Therefore, unless you plan to never update this library, your code will stop working at some point in the future.

If you need to disable some specific dates too, I highly recommend doing like this.

const datesToDisable = [ /* ...some dates */ ];

var datepicker = new Datepicker(elem, {
  datesDisabled(date, viewId) {
    if (viewId > 0) {
      return;
    }
    if (datesToDisable.find(toDisable => date.toDateString() === toDisable.toDateString())) {
      return true;
    }
    const dt = new Date();
    const maxDateVal = dt.setDate(dt.getDate() + 14);
    return date.getTime() > maxDateVal;
  },
});

(I updated the example https://codepen.io/mymth/pen/wvYZzqe)

Just FYI.

PS. It would be appreciated if you close the issue if your problem is solved.