Skip to content

Commit 5420d84

Browse files
committed
fix: Calendar not reacting to switch between range and multi-dates (fixes #860)
1 parent dd85ae9 commit 5420d84

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/VueDatePicker/composables/external-internal-mapper.ts

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export const useExternalInternalMapper = (emit: VueEmit, props: AllPropsType, is
4444
{ deep: true },
4545
);
4646

47+
watch(defaultedRange, (newVal, oldVal) => {
48+
if (newVal.enabled !== oldVal.enabled) {
49+
internalModelValue.value = null;
50+
}
51+
});
52+
4753
watch(formatRef, () => {
4854
formatInputValue();
4955
});

src/VueDatePicker/composables/model.ts

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { computed, ref, reactive } from 'vue';
1+
import { computed, ref, reactive, watch } from 'vue';
22
import { getHours, getMinutes, getMonth, getYear } from 'date-fns';
33

44
import { getDate } from '@/utils/date-utils';
55

6-
import type { ICalendarData, InternalModuleValue, VueEmit } from '@/interfaces';
6+
import type { ICalendarData, InternalModuleValue, TimeType, VueEmit } from '@/interfaces';
77
import type { PickerBasePropsType } from '@/props';
88
import { useDefaults } from '@/composables/defaults';
99
import { localToTz } from '@/utils/timezone';
@@ -14,13 +14,34 @@ export const useModel = (props: PickerBasePropsType, emit: VueEmit) => {
1414
const today = getDate(localToTz(getDate(), defaultedTz.value.timezone));
1515
const calendars = ref<ICalendarData[]>([{ month: getMonth(today), year: getYear(today) }]);
1616

17+
const timeGetter = (type: TimeType) => {
18+
const fn = {
19+
hours: getHours(today),
20+
minutes: getMinutes(today),
21+
seconds: 0,
22+
};
23+
24+
return defaultedRange.value.enabled ? [fn[type], fn[type]] : fn[type];
25+
};
1726
// Time values
1827
const time = reactive({
19-
hours: defaultedRange.value.enabled ? [getHours(today), getHours(today)] : getHours(today),
20-
minutes: defaultedRange.value.enabled ? [getMinutes(today), getMinutes(today)] : getMinutes(today),
21-
seconds: defaultedRange.value.enabled ? [0, 0] : 0,
28+
hours: timeGetter('hours'),
29+
minutes: timeGetter('minutes'),
30+
seconds: timeGetter('seconds'),
2231
});
2332

33+
watch(
34+
defaultedRange,
35+
(newVal, oldVal) => {
36+
if (newVal.enabled !== oldVal.enabled) {
37+
time.hours = timeGetter('hours');
38+
time.minutes = timeGetter('minutes');
39+
time.seconds = timeGetter('seconds');
40+
}
41+
},
42+
{ deep: true },
43+
);
44+
2445
const modelValue = computed({
2546
get: (): InternalModuleValue => {
2647
return props.internalModelValue;

0 commit comments

Comments
 (0)