Skip to content

Commit

Permalink
Merge pull request #165 from uvarov-frontend/fix/range_enabled_and_di…
Browse files Browse the repository at this point in the history
…sabled_dates

Fix range enabled and disabled dates
  • Loading branch information
uvarov-frontend authored Dec 8, 2023
2 parents 2e4f9a0 + e332044 commit 50c2486
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
23 changes: 4 additions & 19 deletions package/src/scripts/helpers/setVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,13 @@ const initRange = (self: VanillaCalendar) => {
: self.settings.range.max;

// set self.rangeDisabled
const firstDay = getDate(self.rangeMin);
const lastDay = getDate(self.rangeMax);
firstDay.setDate(firstDay.getDate() - 1);
lastDay.setDate(lastDay.getDate() + 1);
self.rangeDisabled = self.settings.range.disabled ? parseDates(self.settings.range.disabled) : [];

if (self.settings.range.disableAllDays) {
const daysInCurrentMonth = new Date(self.selectedYear, self.selectedMonth + 1, 0).getDate();
for (let i = 1; i <= daysInCurrentMonth; i++) {
self.rangeDisabled.push(getDateString(new Date(self.selectedYear, self.selectedMonth, i)));
}
}

self.rangeDisabled.push(getDateString(firstDay));
self.rangeDisabled.push(getDateString(lastDay));
self.rangeDisabled.sort((a, b) => +new Date(a) - +new Date(b));
self.rangeDisabled = self.settings.range.disabled && !self.settings.range.disableAllDays ? parseDates(self.settings.range.disabled) : [];
if (self.rangeDisabled.length > 1) self.rangeDisabled.sort((a, b) => +new Date(a) - +new Date(b));

// set self.rangeEnabled
self.rangeEnabled = self.settings.range.enabled ? parseDates(self.settings.range.enabled) : [];
if (self.rangeEnabled?.[0]) self.rangeDisabled = self.rangeDisabled?.filter((d) => !self.rangeEnabled?.includes(d));

self.rangeEnabled.sort((a, b) => +new Date(a) - +new Date(b));
if (self.rangeEnabled?.[0] && self.rangeDisabled?.[0]) self.rangeDisabled = self.rangeDisabled.filter((d) => !self.rangeEnabled.includes(d));
if (self.rangeEnabled.length > 1) self.rangeEnabled.sort((a, b) => +new Date(a) - +new Date(b));

if (self.rangeEnabled?.[0] && self.settings.range.disableAllDays) {
self.rangeMin = self.rangeEnabled[0];
Expand Down
13 changes: 9 additions & 4 deletions package/src/scripts/methods/createDays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import getWeekNumber from '@scripts/helpers/getWeekNumber';
import createPopup from '@scripts/methods/createPopup';
import createWeekNumbers from '@scripts/methods/createWeekNumbers';

const setDisableWeekday = (self: VanillaCalendar, date: FormatDateString, dayWeekID: number) => {
if (self.settings.range.disableWeekday?.includes(dayWeekID) && !self.rangeDisabled?.includes(date)) {
self.rangeDisabled = self.rangeDisabled ? [...self.rangeDisabled, date] : [date];
const setDisabledDays = (self: VanillaCalendar, date: FormatDateString, dayWeekID: number) => {
const isDisableWeekday = self.settings.range.disableWeekday?.includes(dayWeekID);
const isDisableAllDays = self.settings.range.disableAllDays && self.rangeEnabled?.[0];

if ((isDisableWeekday || isDisableAllDays) && !self.rangeEnabled?.includes(date) && !self.rangeDisabled?.includes(date)) {
self.rangeDisabled.push(date);
self.rangeDisabled?.sort((a, b) => +new Date(a) - +new Date(b));
}
};
Expand Down Expand Up @@ -100,7 +103,7 @@ const createDay = (
dayEl.append(dayBtnEl);
}

setDisableWeekday(self, date, dayWeekID);
setDisabledDays(self, date, dayWeekID);
setDayModifier(self, year, dayEl, dayBtnEl, dayWeekID, date, otherMonth);

daysEl.append(dayEl);
Expand Down Expand Up @@ -149,6 +152,8 @@ const createDays = (self: VanillaCalendar) => {
const weekNumbersEls: NodeListOf<HTMLElement> = self.HTMLElement.querySelectorAll(`.${self.CSSClasses.weekNumbers}`);
const initDate = new Date(self.selectedYear as number, self.selectedMonth as number, 1);

if ((self.settings.range.disableAllDays || self.settings.range.disableWeekday) && self.rangeDisabled[0]) self.rangeDisabled = [];

daysEls.forEach((daysEl, index: number) => {
const selectedDate = new Date(initDate);
selectedDate.setMonth(selectedDate.getMonth() + index);
Expand Down

0 comments on commit 50c2486

Please sign in to comment.