Skip to content

Commit 699219b

Browse files
committed
feat: Add keepViewOnOffsetClick in config prop to prevent auto month switch on offset date click (resolves #797)
1 parent b8b24ca commit 699219b

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ export interface VueDatePickerProps {
320320
onClickOutside?: (validate: () => boolean) => void;
321321
tabOutClosesMenu?: boolean;
322322
arrowLeft?: string;
323+
keepViewOnOffsetClick?: boolean;
323324
};
324325
quarterPicker?: boolean;
325326
yearFirst?: boolean;

src/VueDatePicker/components/DatePicker/date-picker.ts

+29-8
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@ export const useDatePicker = (
4646
) => {
4747
const tempRange = ref<Date[]>([]);
4848
const lastScrollTime = ref(new Date());
49+
const clickedDate = ref<ICalendarDay | undefined>();
4950

5051
const { modelValue, calendars, time } = useModel(props, emit);
51-
const { defaultedMultiCalendars, defaultedStartTime, defaultedRange, defaultedTz, propDates, defaultedMultiDates } =
52-
useDefaults(props);
52+
const {
53+
defaultedMultiCalendars,
54+
defaultedStartTime,
55+
defaultedRange,
56+
defaultedConfig,
57+
defaultedTz,
58+
propDates,
59+
defaultedMultiDates,
60+
} = useDefaults(props);
5361
const { validateMonthYearInRange, isDisabled, isDateRangeAllowed, checkMinMaxRange } = useValidation(props);
5462
const { updateTimeValues, getSetDateTime, setTime, assignStartTime, validateTime, disabledTimesConfig } =
5563
useTimePickerUtils(props, time, modelValue, updateFlow);
@@ -67,13 +75,25 @@ export const useDatePicker = (
6775
calendars.value[instance] ? calendars.value[instance].year : 0,
6876
);
6977

78+
const shouldUpdateMonthView = (isAction: boolean) => {
79+
if (!defaultedConfig.value.keepViewOnOffsetClick || isAction) return true;
80+
return !clickedDate.value;
81+
};
82+
7083
// Any update for month or year value will go through this function
71-
const setCalendarMonthYear = (instance: number, month: number | null, year: number | null): void => {
72-
if (!calendars.value[instance]) {
73-
calendars.value[instance] = { month: 0, year: 0 };
84+
const setCalendarMonthYear = (
85+
instance: number,
86+
month: number | null,
87+
year: number | null,
88+
isAction: boolean = false,
89+
): void => {
90+
if (shouldUpdateMonthView(isAction)) {
91+
if (!calendars.value[instance]) {
92+
calendars.value[instance] = { month: 0, year: 0 };
93+
}
94+
calendars.value[instance].month = isNumNullish(month) ? calendars.value[instance]?.month : month;
95+
calendars.value[instance].year = isNumNullish(year) ? calendars.value[instance]?.year : year;
7496
}
75-
calendars.value[instance].month = isNumNullish(month) ? calendars.value[instance]?.month : month;
76-
calendars.value[instance].year = isNumNullish(year) ? calendars.value[instance]?.year : year;
7797
};
7898

7999
const selectOnAutoApply = () => {
@@ -552,6 +572,7 @@ export const useDatePicker = (
552572
*/
553573
const selectDate = (day: ICalendarDay, isNext = false): void => {
554574
if (isDisabled(day.value) || (!day.current && props.hideOffsetDates)) return emit('invalid-date', day.value);
575+
clickedDate.value = JSON.parse(JSON.stringify(day));
555576

556577
if (!defaultedRange.value.enabled) return handleSingleDateSelect(day);
557578

@@ -563,7 +584,7 @@ export const useDatePicker = (
563584

564585
// Handles selection of month/year
565586
const updateMonthYear = (instance: number, val: { month: number; year: number; fromNav?: boolean }): void => {
566-
setCalendarMonthYear(instance, val.month, val.year);
587+
setCalendarMonthYear(instance, val.month, val.year, true);
567588

568589
if (defaultedMultiCalendars.value.count && !defaultedMultiCalendars.value.solo) {
569590
autoChangeMultiCalendars(instance);

src/VueDatePicker/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ export interface Config {
282282
onClickOutside?: (validate: () => boolean) => void;
283283
tabOutClosesMenu: boolean;
284284
arrowLeft?: string;
285+
keepViewOnOffsetClick?: boolean;
285286
}
286287

287288
export interface Highlight {

src/VueDatePicker/utils/defaults.ts

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export const getDefaultConfig = (config?: Partial<Config>): Config => {
168168
onClickOutside: undefined,
169169
tabOutClosesMenu: true,
170170
arrowLeft: undefined,
171+
keepViewOnOffsetClick: false,
171172
};
172173
return { ...defaultConfig, ...(config ?? {}) };
173174
};

0 commit comments

Comments
 (0)