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

Commit f9b1e0d

Browse files
committed
fix: Missing calendar days when weekStart is changed
1 parent f432a49 commit f9b1e0d

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ interface Vue3DatePicker {
3737
maxDate?: Date | string;
3838
minTime?: { hours?: number | string; minutes?: number | string };
3939
maxTime?: { hours?: number | string; minutes?: number | string };
40-
weekStart?: string | number;
40+
weekStart?: '0' | '1' | '2' | '3' | '4' | '5' | '6' | 0 | 1 | 2 | 3 | 4 | 5 | 6;
4141
disabled?: boolean;
4242
readonly?: boolean;
4343
format?:

src/Vue3DatePicker/Vue3DatePicker.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@
107107
ITextInputOptions,
108108
ModelValue,
109109
ITimeValue,
110+
WeekStartNum,
111+
WeekStartStr,
110112
} from './interfaces';
111113
import { clickOutsideDirective as vClickOutsideDirective } from './directives/clickOutside';
112114
import { getDateHours, getDateMinutes, getDefaultPattern } from './utils/date-utils';
@@ -137,7 +139,7 @@
137139
maxDate: { type: [Date, String] as PropType<Date | string>, default: null },
138140
minTime: { type: Object as PropType<ITimeValue>, default: () => ({}) },
139141
maxTime: { type: Object as PropType<ITimeValue>, default: () => ({}) },
140-
weekStart: { type: [String, Number] as PropType<string | number>, default: 1 },
142+
weekStart: { type: [String, Number] as PropType<WeekStartNum | WeekStartStr>, default: 1 },
141143
disabled: { type: Boolean as PropType<boolean>, default: false },
142144
readonly: { type: Boolean as PropType<boolean>, default: false },
143145
monthNameFormat: { type: String as PropType<'long' | 'short'>, default: 'short' },

src/Vue3DatePicker/components/DatepickerMenu.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
IFormat,
8383
InternalModuleValue,
8484
ITimeValue,
85+
WeekStartNum,
86+
WeekStartStr,
8587
} from '../interfaces';
8688
import { mapSlots } from './composition/slots';
8789
import { getCalendarDays, getMonths, getYears } from '../utils/util';
@@ -100,7 +102,7 @@
100102
uid: { type: String as PropType<string>, default: 'dp' },
101103
internalModelValue: { type: [Date, Array] as PropType<InternalModuleValue>, default: null },
102104
weekNumbers: { type: Boolean as PropType<boolean>, default: false },
103-
weekStart: { type: [Number, String] as PropType<number | string>, default: 1 },
105+
weekStart: { type: [Number, String] as PropType<WeekStartNum | WeekStartStr>, default: 1 },
104106
disableMonthYearSelect: { type: Boolean as PropType<boolean>, default: false },
105107
menuClassName: { type: String as PropType<string>, default: null },
106108
calendarClassName: { type: String as PropType<string>, default: null },

src/Vue3DatePicker/interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export interface ITimeValue {
7474

7575
export type ModelValue = Date | Date[] | string | string[] | ITimeValue | ITimeValue[] | IMonthValue | null;
7676

77+
export type WeekStartNum = 0 | 1 | 2 | 3 | 4 | 5 | 6;
78+
export type WeekStartStr = '0' | '1' | '2' | '3' | '4' | '5' | '6';
79+
7780
export type UseCalendar = {
7881
range: boolean;
7982
startDate: Date | string;

src/Vue3DatePicker/utils/util.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { ICalendarDate, ICalendarDay, IDateFilter, IDefaultSelect, ITextInputOptions } from '../interfaces';
1+
import {
2+
ICalendarDate,
3+
ICalendarDay,
4+
IDateFilter,
5+
IDefaultSelect,
6+
ITextInputOptions,
7+
WeekStartNum,
8+
} from '../interfaces';
9+
import { getAddedDays, isDateEqual, resetDateTime } from './date-utils';
210

311
/**
412
* Depending on a week start get starting date of the current calendar
@@ -13,12 +21,6 @@ const getFirstDayOfTheFirstWeek = (firstDate: Date, start: number): Date => {
1321
return startDate;
1422
};
1523

16-
// From the giving date get the date after 7 days
17-
const getSevenDaysOffset = (date: Date): Date => {
18-
const startDate = new Date(JSON.parse(JSON.stringify(date)));
19-
return new Date(startDate.setDate(startDate.getDate() + 7));
20-
};
21-
2224
// Get 7 days from the provided start date, month is used to check whether the date is from the specified month or in the offset
2325
const getWeekDays = (startDay: Date, month: number, hideOffsetDates: boolean): ICalendarDay[] => {
2426
const startDate = new Date(JSON.parse(JSON.stringify(startDay)));
@@ -36,33 +38,30 @@ const getWeekDays = (startDay: Date, month: number, hideOffsetDates: boolean): I
3638
return dates;
3739
};
3840

39-
/**
40-
* Returns the number of weeks in a month, each week must have current month date in
41-
*/
42-
const getNumberOfWeeksInAMonth = (firstDate: Date, lastDate: Date): number => {
43-
return Math.ceil((firstDate.getDay() + lastDate.getDate() - 1) / 7);
44-
};
45-
4641
// Get days for the calendar to be displayed in a table grouped by weeks
4742
export const getCalendarDays = (
4843
month: number,
4944
year: number,
50-
start: number,
45+
start: WeekStartNum,
5146
hideOffsetDates: boolean,
5247
): ICalendarDate[] => {
5348
const weeks: ICalendarDate[] = [];
5449
const firstDate = new Date(year, month, 1);
5550
const lastDate = new Date(year, month + 1, 0);
5651

57-
let firstDateInCalendar = getFirstDayOfTheFirstWeek(firstDate, start);
58-
const weeksInMonth = getNumberOfWeeksInAMonth(firstDate, lastDate);
59-
for (let week = 0; week < weeksInMonth; week++) {
60-
if (week !== 0) {
61-
firstDateInCalendar = getSevenDaysOffset(firstDateInCalendar);
62-
}
63-
const days = getWeekDays(firstDateInCalendar, month, hideOffsetDates);
52+
const firstDateInCalendar = getFirstDayOfTheFirstWeek(firstDate, start);
53+
54+
const addDaysToWeek = (date: Date) => {
55+
const days = getWeekDays(date, month, hideOffsetDates);
6456
weeks.push({ days });
65-
}
57+
if (
58+
!weeks[weeks.length - 1].days.some((day) => isDateEqual(resetDateTime(day.value), resetDateTime(lastDate)))
59+
) {
60+
const nextDate = getAddedDays(date, 7);
61+
addDaysToWeek(nextDate);
62+
}
63+
};
64+
addDaysToWeek(firstDateInCalendar);
6665

6766
return weeks;
6867
};

0 commit comments

Comments
 (0)