Skip to content

feat(VDatePicker): Calculate disabled year and month #21470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions packages/vuetify/src/components/VDatePicker/VDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useProxiedModel } from '@/composables/proxiedModel'

// Utilities
import { computed, ref, shallowRef, toRef, watch } from 'vue'
import { genericComponent, omit, propsFactory, useRender, wrapInArray } from '@/util'
import { createRange, genericComponent, omit, propsFactory, useRender, wrapInArray } from '@/util'

// Types
import type { VPickerSlots } from '@/labs/VPicker/VPicker'
Expand Down Expand Up @@ -195,17 +195,41 @@ export const VDatePicker = genericComponent<new <
})

function allowedYears (year: number) {
if (!Array.isArray(props.allowedDates) || !props.allowedDates.length) return true
return props.allowedDates.map(date => adapter.getYear(adapter.date(date))).includes(year)
if (typeof props.allowedDates === 'function') {
const isAllowedDate = props.allowedDates
const start = adapter.parseISO(`${year}-01-01`)
const yearSize = adapter.getDiff(adapter.endOfYear(start), start, 'days')

return createRange(yearSize)
.some(count => isAllowedDate(adapter.addDays(start, count)))
}

if (Array.isArray(props.allowedDates) && props.allowedDates.length) {
return props.allowedDates.map(date => adapter.getYear(adapter.date(date))).includes(year)
}

return true
}

function allowedMonths (month: number) {
if (!Array.isArray(props.allowedDates) || !props.allowedDates.length) return true
if (!allowedYears(year.value)) return false
if (typeof props.allowedDates === 'function') {
const isAllowedDate = props.allowedDates
const start = adapter.parseISO(`${year.value}-${month}-01`)
const monthSize = adapter.getDiff(adapter.endOfMonth(start), start, 'days')

return createRange(monthSize)
.some(count => isAllowedDate(adapter.addDays(start, count)))
}

if (Array.isArray(props.allowedDates) && props.allowedDates.length) {
if (!allowedYears(year.value)) return false

return props.allowedDates
.filter(date => adapter.getYear(adapter.date(date)) === year.value)
.map(date => adapter.getMonth(adapter.date(date))).includes(month)
}

return props.allowedDates
.filter(date => adapter.getYear(adapter.date(date)) === year.value)
.map(date => adapter.getMonth(adapter.date(date))).includes(month)
return true
}

// function onClickAppend () {
Expand Down