Skip to content

Commit 4b5f451

Browse files
committed
fix: Use proper multi-calendars map on month and quarter pickers (fixes #875)
1 parent 22346de commit 4b5f451

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/VueDatePicker/components/MonthPicker/month-picker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const useMonthPicker = (props: PickerBasePropsType, emit: VueEmit) => {
5555
} = useMonthOrQuarterPicker({
5656
modelValue,
5757
multiCalendars: defaultedMultiCalendars,
58+
range: defaultedRange,
5859
highlight: defaultedHighlight,
5960
calendars,
6061
year,

src/VueDatePicker/components/QuarterPicker/quarter-picker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const useQuarterPicker = (props: PickerBasePropsType, emit: VueEmit) => {
3838
useMonthOrQuarterPicker({
3939
modelValue,
4040
multiCalendars: defaultedMultiCalendars,
41+
range: defaultedRange,
4142
highlight: defaultedHighlight,
4243
calendars,
4344
propDates,

src/VueDatePicker/components/shared/month-quarter-picker.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { computed, onMounted, ref } from 'vue';
2-
import { addYears, endOfYear, getMonth, getYear, set, startOfYear, subYears } from 'date-fns';
2+
import { addYears, differenceInYears, endOfYear, getMonth, getYear, set, startOfYear, subYears } from 'date-fns';
33

44
import { checkHighlightYear, getDate, getMinMaxYear, resetDate, validateMonthYear } from '@/utils/date-utils';
55
import { checkMinMaxValue, getYears, groupListAndMap } from '@/utils/util';
@@ -16,6 +16,7 @@ import type {
1616
HighlightFn,
1717
PropDates,
1818
DateFilter,
19+
RangeConfig,
1920
} from '@/interfaces';
2021
import type { PickerBasePropsType } from '@/props';
2122
import { FlowStep } from '@/constants';
@@ -30,6 +31,7 @@ interface Opts {
3031
highlight: ComputedRef<Highlight | HighlightFn>;
3132
propDates: ComputedRef<PropDates>;
3233
filters: ComputedRef<DateFilter>;
34+
range: ComputedRef<RangeConfig>;
3335
emit: VueEmit;
3436
}
3537

@@ -38,6 +40,7 @@ interface Opts {
3840
*/
3941
export const useMonthOrQuarterPicker = ({
4042
multiCalendars,
43+
range,
4144
highlight,
4245
propDates,
4346
calendars,
@@ -66,10 +69,19 @@ export const useMonthOrQuarterPicker = ({
6669
);
6770
});
6871

72+
const isSoloMultiInRange = () => {
73+
return Array.isArray(modelValue.value) && multiCalendars.value.solo && modelValue.value[1];
74+
};
75+
6976
const assignMultiCalendars = () => {
7077
for (let i = 0; i < multiCalendars.value.count; i++) {
7178
if (i === 0) {
7279
calendars.value[i] = calendars.value[0];
80+
} else if (i === multiCalendars.value.count - 1 && isSoloMultiInRange()) {
81+
calendars.value[i] = {
82+
month: getMonth((modelValue.value as Date[])[1]),
83+
year: getYear((modelValue.value as Date[])[1]),
84+
};
7385
} else {
7486
const prevDate = set(getDate(), calendars.value[i - 1]);
7587
calendars.value[i] = { month: getMonth(prevDate), year: getYear(addYears(prevDate, 1)) };
@@ -84,9 +96,15 @@ export const useMonthOrQuarterPicker = ({
8496
return assignMultiCalendars();
8597
};
8698

99+
const getDateToFocus = (dateOne: Date, dateTwo: Date) => {
100+
const diff = differenceInYears(dateTwo, dateOne);
101+
return range.value.showLastInRange && diff > 1 ? dateTwo : dateOne;
102+
};
103+
87104
const getRangedValueDate = (dates: Date[]) => {
88105
if (props.focusStartDate) return dates[0];
89-
return dates[1] ? dates[1] : dates[0];
106+
if (multiCalendars.value.solo) return dates[0];
107+
return dates[1] ? getDateToFocus(dates[0], dates[1]) : dates[0];
90108
};
91109

92110
const checkModelValue = () => {

0 commit comments

Comments
 (0)