diff --git a/package/src/scripts/helpers/setVariables.ts b/package/src/scripts/helpers/setVariables.ts index e8d23a77..2a6c97a6 100644 --- a/package/src/scripts/helpers/setVariables.ts +++ b/package/src/scripts/helpers/setVariables.ts @@ -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]; diff --git a/package/src/scripts/methods/createDays.ts b/package/src/scripts/methods/createDays.ts index 0e14a33e..959ef98c 100644 --- a/package/src/scripts/methods/createDays.ts +++ b/package/src/scripts/methods/createDays.ts @@ -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)); } }; @@ -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); @@ -149,6 +152,8 @@ const createDays = (self: VanillaCalendar) => { const weekNumbersEls: NodeListOf = 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);