Skip to content
This repository was archived by the owner on Apr 17, 2022. It is now read-only.

Commit 1f475ff

Browse files
committed
fix: Months filter causing year jump
1 parent 33d825f commit 1f475ff

File tree

3 files changed

+40
-74
lines changed

3 files changed

+40
-74
lines changed

src/Vue3DatePicker/components/MonthYearInput.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<template v-if="!monthPicker">
44
<div
55
class="dp__month_year_col_nav"
6-
@click="onPrev"
7-
@keydown.enter="onPrev"
6+
@click="handleMonthYearChange(false)"
7+
@keydown.enter="handleMonthYearChange(false)"
88
v-if="showLeftIcon"
99
tabindex="0"
1010
>
@@ -73,9 +73,9 @@
7373
</transition>
7474
<div
7575
class="dp__month_year_col_nav"
76-
@click="onNext"
76+
@click="handleMonthYearChange(true)"
7777
v-if="showRightIcon"
78-
@keydown.enter="onNext"
78+
@keydown.enter="handleMonthYearChange(true)"
7979
tabindex="0"
8080
>
8181
<div class="dp__inner_nav" role="button" aria-label="Next month">
@@ -193,7 +193,7 @@
193193
194194
const showMonthPicker = ref(false);
195195
const showYearPicker = ref(false);
196-
const { onNext, onPrev } = useMontYearPick(props, emit);
196+
const { handleMonthYearChange } = useMontYearPick(props, emit);
197197
198198
const onMonthUpdate = (month: number): void => {
199199
emit('update:month', month);
Lines changed: 35 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,52 @@
1-
import { IMonthYearHook, UseMonthYearPick, VueEmit } from '../../interfaces';
1+
import { addMonths, addYears, getMonth, getYear, set, subMonths, subYears } from 'date-fns';
2+
import { UseMonthYearPick, VueEmit } from '../../interfaces';
23

3-
export const useMontYearPick = (props: UseMonthYearPick, emit: VueEmit): IMonthYearHook => {
4-
const months = props.months.map((month) => month.value);
5-
const years = props.years.map((year) => year.value);
6-
const excludedMonths = months.filter((month: number) => !props.filters.months.some((mon: number) => mon === month));
7-
const excludedYears = years.filter((year: number) => !props.filters.years.some((yr: number) => yr === year));
4+
export const useMontYearPick = (
5+
props: UseMonthYearPick,
6+
emit: VueEmit,
7+
): { handleMonthYearChange(isNext: boolean): void } => {
8+
const recursiveMonthAdjust = (date: Date, increment: boolean): Date => {
9+
let monthDate = date;
10+
if (props.filters.months.includes(getMonth(monthDate))) {
11+
monthDate = increment ? addMonths(date, 1) : subMonths(date, 1);
12+
return recursiveMonthAdjust(monthDate, increment);
13+
}
14+
return monthDate;
15+
};
816

9-
const onNext = (): void => {
10-
let tempMonth;
11-
let month = props.month;
12-
let year = props.year;
13-
if (props.month === 11) {
14-
month = 0;
15-
year = props.year + 1;
16-
} else {
17-
month += 1;
18-
}
19-
if (props.filters.months.includes(month)) {
20-
if (month === 0) {
21-
month = Math.min(...excludedMonths);
22-
} else {
23-
month = Math.max(...excludedMonths);
24-
}
25-
tempMonth = month;
26-
}
27-
if (month === tempMonth) {
28-
month = Math.min(...excludedMonths);
29-
year = props.year + 1;
30-
}
31-
if (props.filters.years.includes(year)) {
32-
const foundYear = excludedYears.find((availableYear: number) => availableYear > year);
33-
if (foundYear) {
34-
year = foundYear;
35-
}
36-
}
37-
if (year <= years[years.length - 1]) {
38-
updateMonthYear(month, year);
17+
const recursiveYearAdjust = (date: Date, increment: boolean): Date => {
18+
let yearDate = date;
19+
if (props.filters.years.includes(getYear(yearDate))) {
20+
yearDate = increment ? addYears(date, 1) : subYears(date, 1);
21+
return recursiveYearAdjust(yearDate, increment);
3922
}
23+
return yearDate;
4024
};
4125

42-
const onPrev = (): void => {
43-
let tempMonth;
44-
let month = props.month;
45-
let year = props.year;
46-
if (props.month === 0) {
47-
month = 11;
48-
year = props.year - 1;
49-
} else {
50-
month -= 1;
51-
}
26+
const handleMonthYearChange = (isNext: boolean): void => {
27+
const initialDate = set(new Date(), { month: props.month, year: props.year });
28+
let date = isNext ? addMonths(initialDate, 1) : subMonths(initialDate, 1);
29+
30+
let month = getMonth(date);
31+
let year = getYear(date);
32+
5233
if (props.filters.months.includes(month)) {
53-
if (month === 11) {
54-
month = Math.max(...excludedMonths);
55-
} else {
56-
month = Math.min(...excludedMonths);
57-
}
58-
tempMonth = month;
59-
}
60-
if (month === tempMonth) {
61-
month = Math.max(...excludedMonths);
62-
year = props.year - 1;
34+
date = recursiveMonthAdjust(date, isNext);
35+
month = getMonth(date);
36+
year = getYear(date);
6337
}
38+
6439
if (props.filters.years.includes(year)) {
65-
const foundYear = excludedYears.reverse().find((availableYear: number) => availableYear < year);
66-
if (foundYear) {
67-
year = foundYear;
68-
}
69-
}
70-
if (year >= years[0]) {
71-
updateMonthYear(month, year);
40+
date = recursiveYearAdjust(date, isNext);
41+
year = getYear(date);
7242
}
43+
updateMonthYear(month, year);
7344
};
7445

7546
const updateMonthYear = (month: number, year: number): void => {
7647
emit('update:month', month);
7748
emit('update:year', year);
7849
};
7950

80-
return { onNext, onPrev };
51+
return { handleMonthYearChange };
8152
};

src/Vue3DatePicker/interfaces.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ export interface ICalendarDate {
3939
days: ICalendarDay[];
4040
}
4141

42-
export interface IMonthYearHook {
43-
onNext(): void;
44-
onPrev(): void;
45-
}
46-
4742
export interface ITextInputOptions {
4843
enterSubmit: boolean;
4944
tabSubmit: boolean;

0 commit comments

Comments
 (0)