Skip to content

Commit 1261da5

Browse files
committed
fix: Enabled month can't be selected (fixes #865)
1 parent b99f825 commit 1261da5

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

src/VueDatePicker/composables/defaults.ts

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export const useDefaults = (props: AllPropsType | PickerBasePropsType) => {
9090
defaultedHighlight.value,
9191
props.markers,
9292
defaultedTz.value,
93+
props.monthPicker || props.yearPicker || props.quarterPicker,
9394
),
9495
);
9596

src/VueDatePicker/utils/date-utils.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
setYear,
2424
subMonths,
2525
format,
26+
startOfMonth,
2627
} from 'date-fns';
2728
import { errors } from '@/utils/util';
2829

@@ -119,14 +120,15 @@ export const dateToUtc = (date: Date, preserve: boolean, enableSeconds: boolean)
119120
};
120121

121122
// Reset date time
122-
export const resetDateTime = (value: Date | string): Date => {
123-
let dateParse = getDate(JSON.parse(JSON.stringify(value)));
124-
dateParse = setHours(dateParse, 0);
125-
dateParse = setMinutes(dateParse, 0);
126-
dateParse = setSeconds(dateParse, 0);
127-
dateParse = setMilliseconds(dateParse, 0);
128-
129-
return dateParse;
123+
export const resetDateTime = (value: Date | string, beginning?: boolean): Date => {
124+
const dateParse = getDate(JSON.parse(JSON.stringify(value)));
125+
const timeReset = set(dateParse, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 });
126+
if (beginning) {
127+
return startOfMonth(timeReset);
128+
}
129+
return timeReset;
130+
131+
// return set(dateParse, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 });
130132
};
131133

132134
export const setDateTime = (

src/VueDatePicker/utils/defaults.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,11 @@ export const getDefaultTimeZone = (timeZone: TimeZoneProp, emitTimezone?: string
250250
const datesArrToMap = (
251251
datesArr: (Date | string | number)[],
252252
timezone: TimeZoneConfig | undefined,
253+
reset?: boolean,
253254
): Map<string, Date | null> => {
254255
return new Map(
255256
datesArr.map((date) => {
256-
const d = dateToTimezoneSafe(date, timezone);
257+
const d = dateToTimezoneSafe(date, timezone, reset);
257258
return [getMapKey(d), d];
258259
}),
259260
);
@@ -283,12 +284,13 @@ export const mapPropDates = (
283284
highlight: HighlightFn | Highlight,
284285
markers: IMarker[],
285286
timezone: TimeZoneConfig | undefined,
287+
isSpecific: boolean,
286288
): PropDates => {
287289
return {
288-
minDate: sanitizeDateToLocal(minDate, timezone),
289-
maxDate: sanitizeDateToLocal(maxDate, timezone),
290-
disabledDates: shouldMap(disabledDates) ? datesArrToMap(disabledDates, timezone) : disabledDates,
291-
allowedDates: shouldMap(allowedDates) ? datesArrToMap(allowedDates, timezone) : null,
290+
minDate: sanitizeDateToLocal(minDate, timezone, isSpecific),
291+
maxDate: sanitizeDateToLocal(maxDate, timezone, isSpecific),
292+
disabledDates: shouldMap(disabledDates) ? datesArrToMap(disabledDates, timezone, isSpecific) : disabledDates,
293+
allowedDates: shouldMap(allowedDates) ? datesArrToMap(allowedDates, timezone, isSpecific) : null,
292294
highlight:
293295
typeof highlight === 'object' && shouldMap(highlight?.dates)
294296
? datesArrToMap(highlight.dates, timezone)

src/VueDatePicker/utils/timezone.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDate } from '@/utils/date-utils';
1+
import { getDate, resetDateTime } from '@/utils/date-utils';
22
import type { MaybeDate, TimeZoneConfig } from '@/interfaces';
33

44
/**
@@ -9,22 +9,23 @@ export const localToTz = (date: Date, timeZone?: string) => {
99
return new Date(date.toLocaleString('en-US', { timeZone }));
1010
};
1111

12-
export const dateToTimezoneSafe = (date: Date | string | number, tz?: TimeZoneConfig) => {
13-
const d = sanitizeDateToLocal(date, tz);
12+
export const dateToTimezoneSafe = (date: Date | string | number, tz?: TimeZoneConfig, reset?: boolean) => {
13+
const d = sanitizeDateToLocal(date, tz, reset);
1414
if (!d) return getDate();
1515
return d;
1616
};
1717

18-
const getDateInTz = (date: Date | number | string, tz: TimeZoneConfig) => {
19-
return tz.dateInTz ? localToTz(new Date(date), tz.dateInTz) : getDate(date);
18+
const getDateInTz = (date: Date | number | string, tz: TimeZoneConfig, reset?: boolean) => {
19+
const newDate = tz.dateInTz ? localToTz(new Date(date), tz.dateInTz) : getDate(date);
20+
return reset ? resetDateTime(newDate, true) : newDate;
2021
};
2122

2223
// Converts specific date to a Date object based on a provided timezone
23-
export const sanitizeDateToLocal = (date: MaybeDate, tz?: TimeZoneConfig) => {
24+
export const sanitizeDateToLocal = (date: MaybeDate, tz?: TimeZoneConfig, reset?: boolean) => {
2425
if (!date) return null;
25-
if (!tz) return getDate(date);
26-
const local = getDate(date);
27-
return tz.exactMatch ? getDateInTz(date, tz) : localToTz(local, tz.timezone);
26+
const newDate = reset ? resetDateTime(getDate(date), true) : getDate(date);
27+
if (!tz) return newDate;
28+
return tz.exactMatch ? getDateInTz(date, tz, reset) : localToTz(newDate, tz.timezone);
2829
};
2930

3031
export const getTimezoneOffset = (timezone?: string) => {

0 commit comments

Comments
 (0)