Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty list of years with enableJumpToSelectedDate #349

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions demo/pages/lang/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const options: Options = {
type: 'multiple',
selectionDatesMode: 'multiple-ranged',
disableDatesGaps: true,
// disableDatesPast: true,
disableDatesPast: true,
dateMin: '2021-02-01',
dateMax: '2025-11-30',
displayDatesOutside: false,
Expand Down Expand Up @@ -65,12 +65,11 @@ const options: Options = {
};

document.addEventListener('DOMContentLoaded', () => {
const calendar = new Calendar('#calendar', options);
const calendar = new Calendar('#calendar');
calendar.init();

// const btnSetEl = document.querySelector('#set-options');
// btnSetEl?.addEventListener('click', () => {
// calendar.set(options, { dates: false });
// console.log(calendar);
// });
const btnSetEl = document.querySelector('#set-options');
btnSetEl?.addEventListener('click', () => {
calendar.set(options, { dates: false });
});
});
2 changes: 1 addition & 1 deletion package/src/scripts/creators/createYears.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 12 additions & 6 deletions package/src/scripts/utils/initVariables/initSelectedMonthYear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading