Skip to content

Commit

Permalink
update createYears.ts and initSelectedMonthYear.ts
Browse files Browse the repository at this point in the history
uvarov-frontend committed Dec 17, 2024
1 parent 3687d04 commit cdda7c8
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package/src/scripts/creators/createYears.ts
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ const createYears = (self: Calendar, target?: HTMLElement) => {
const templateYearEl = document.createElement('button');
templateYearEl.type = 'button';

for (let i = (self.context.displayYear as number) - 7; i < (self.context.displayYear as number) + 8; i++) {
for (let i = self.context.displayYear - 7; i < self.context.displayYear + 8; i++) {
const yearDisabled = i < getDate(self.context.dateMin).getFullYear() + relationshipID || i > getDate(self.context.dateMax).getFullYear();
const yearEl = createYearEl(self, templateYearEl, selectedYear, yearDisabled, i);
yearsEl.appendChild(yearEl);
18 changes: 12 additions & 6 deletions package/src/scripts/utils/initVariables/initSelectedMonthYear.ts
Original file line number Diff line number Diff line change
@@ -3,22 +3,28 @@ import parseDates from '@scripts/utils/parseDates';
import setContext from '@scripts/utils/setContext';
import type { Calendar, Range } from '@src/index';

const setInitialContext = (self: Calendar, month: Range<12>, year: number) => {
setContext(self, 'selectedMonth', month);
setContext(self, 'selectedYear', year);
setContext(self, 'displayYear', year);
};

const initSelectedMonthYear = (self: Calendar) => {
const isJumpToSelectedDate = self.enableJumpToSelectedDate && self.selectedDates?.[0] && self.selectedMonth === undefined && self.selectedYear === undefined;

if (isJumpToSelectedDate) {
const selectedDate = getDate(parseDates(self.selectedDates)[0]);
setContext(self, 'selectedMonth', selectedDate.getMonth() as Range<12>);
setContext(self, 'selectedYear', selectedDate.getFullYear());
setInitialContext(self, selectedDate.getMonth() as Range<12>, selectedDate.getFullYear());
return;
}

const isValidMonth = self.selectedMonth !== undefined && Number(self.selectedMonth) >= 0 && Number(self.selectedMonth) < 12;
const isValidYear = self.selectedYear !== undefined && Number(self.selectedYear) >= 0 && Number(self.selectedYear) <= 9999;

setContext(self, 'selectedMonth', (isValidMonth ? Number(self.selectedMonth) : getDate(self.context.dateToday).getMonth()) as Range<12>);
setContext(self, 'selectedYear', isValidYear ? Number(self.selectedYear) : getDate(self.context.dateToday).getFullYear());
setContext(self, 'displayYear', self.context.selectedYear);
setInitialContext(
self,
(isValidMonth ? Number(self.selectedMonth) : getDate(self.context.dateToday).getMonth()) as Range<12>,
isValidYear ? Number(self.selectedYear) : getDate(self.context.dateToday).getFullYear(),
);
};

export default initSelectedMonthYear;

0 comments on commit cdda7c8

Please sign in to comment.