Skip to content

Commit

Permalink
fix(VCalendar): fixed issue in calendar file instead of vuetify.ts
Browse files Browse the repository at this point in the history
Signed-off-by: Hazel <[email protected]>
  • Loading branch information
wyhsunflower committed Aug 27, 2024
1 parent 90b7058 commit 928727f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
7 changes: 5 additions & 2 deletions packages/vuetify/src/composables/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,17 @@ export function useCalendar (props: CalendarProps) {
v => adapter.getMonth(v)
)

const defaultFirstDayOfWeek = computed(() => {
return props.firstDayOfWeek ?? props.weekdays[0]
})

const weekDays = computed(() => {
const firstDayOfWeek = Number(props.firstDayOfWeek ?? 0)

return props.weekdays.map(day => (day + firstDayOfWeek) % 7)
})

const weeksInMonth = computed(() => {
const weeks = adapter.getWeekArray(month.value, props.firstDayOfWeek, weekDays.value)
const weeks = adapter.getWeekArray(month.value, defaultFirstDayOfWeek.value)

const days = weeks.flat()

Expand Down
2 changes: 1 addition & 1 deletion packages/vuetify/src/composables/date/DateAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface DateAdapter<T = unknown> {
getYear (date: T): number
setYear (date: T, year: number): T
getDiff (date: T, comparing: T | string, unit?: string): number
getWeekArray (date: T, firstDayOfWeek?: number | string, weekDays?: number[]): T[][]
getWeekArray (date: T, firstDayOfWeek?: number | string): T[][]
getWeekdays (firstDayOfWeek?: number | string): string[]
getMonth (date: T): number
setMonth (date: T, month: number): T
Expand Down
20 changes: 7 additions & 13 deletions packages/vuetify/src/composables/date/adapters/vuetify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,26 +160,22 @@ const firstDay: Record<string, number> = {
ZW: 0,
}

function getWeekArray (date: Date, locale: string, firstDayOfWeek?: number, weekDays?: number[]) {
function getWeekArray (date: Date, locale: string, firstDayOfWeek?: number) {
const weeks = []
let currentWeek = []
const firstDayOfMonth = startOfMonth(date)
const lastDayOfMonth = endOfMonth(date)
const first = firstDayOfWeek ?? firstDay[locale.slice(-2).toUpperCase()] ?? 0

const offSetFirstIndex = weekDays ? weekDays[0] : 0
const firstDayWeekIndex = (firstDayOfMonth.getDay() - first + 7) % 7 - offSetFirstIndex
const lastDayWeekIndex = (lastDayOfMonth.getDay() - first + 7) % 7 - offSetFirstIndex
const firstDayWeekIndex = (firstDayOfMonth.getDay() - first + 7) % 7
const lastDayWeekIndex = (lastDayOfMonth.getDay() - first + 7) % 7

for (let i = 0; i < firstDayWeekIndex; i++) {
const adjacentDay = new Date(firstDayOfMonth)
adjacentDay.setDate(adjacentDay.getDate() - (firstDayWeekIndex - i))
currentWeek.push(adjacentDay)
}

const secondWeekIndex = firstDayWeekIndex < 0 ? Math.abs(firstDayWeekIndex) + 1 : 1

for (let i = secondWeekIndex; i <= lastDayOfMonth.getDate(); i++) {
for (let i = 1; i <= lastDayOfMonth.getDate(); i++) {
const day = new Date(date.getFullYear(), date.getMonth(), i)

// Add the day to the current week
Expand All @@ -192,9 +188,7 @@ function getWeekArray (date: Date, locale: string, firstDayOfWeek?: number, week
}
}

const lastWeekIndex = lastDayWeekIndex < 0 ? Math.abs(lastDayWeekIndex) : 7 - lastDayWeekIndex

for (let i = 1; i < lastWeekIndex; i++) {
for (let i = 1; i < 7 - lastDayWeekIndex; i++) {
const adjacentDay = new Date(lastDayOfMonth)
adjacentDay.setDate(adjacentDay.getDate() + i)
currentWeek.push(adjacentDay)
Expand Down Expand Up @@ -610,8 +604,8 @@ export class VuetifyDateAdapter implements DateAdapter<Date> {
return addMonths(date, amount)
}

getWeekArray (date: Date, firstDayOfWeek?: number | string, weekDays?: number[]) {
return getWeekArray(date, this.locale, firstDayOfWeek ? Number(firstDayOfWeek) : undefined, weekDays)
getWeekArray (date: Date, firstDayOfWeek?: number | string) {
return getWeekArray(date, this.locale, firstDayOfWeek ? Number(firstDayOfWeek) : undefined)
}

startOfWeek (date: Date, firstDayOfWeek?: number | string): Date {
Expand Down

0 comments on commit 928727f

Please sign in to comment.