Skip to content
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

fix(VDatePicker): fix select current month or year #19153

Merged
Show file tree
Hide file tree
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
20 changes: 14 additions & 6 deletions packages/vuetify/src/components/VDatePicker/VDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ export const VDatePicker = genericComponent<new <
} else {
year.value++
month.value = 0
onUpdateYear(year.value)
}
onUpdateMonth(month.value)
}

function onClickPrev () {
Expand All @@ -191,7 +193,9 @@ export const VDatePicker = genericComponent<new <
} else {
year.value--
month.value = 11
onUpdateYear(year.value)
}
onUpdateMonth(month.value)
}

function onClickDate () {
Expand All @@ -206,17 +210,17 @@ export const VDatePicker = genericComponent<new <
viewMode.value = viewMode.value === 'year' ? 'month' : 'year'
}

watch(month, () => {
function onUpdateMonth (value: number) {
if (viewMode.value === 'months') onClickMonth()

emit('update:month', month.value)
})
emit('update:month', value)
}

watch(year, () => {
function onUpdateYear (value: number) {
if (viewMode.value === 'year') onClickYear()

emit('update:year', year.value)
})
emit('update:year', value)
}

watch(model, (val, oldVal) => {
const before = adapter.date(wrapInArray(val)[0])
Expand Down Expand Up @@ -294,6 +298,7 @@ export const VDatePicker = genericComponent<new <
key="date-picker-months"
{ ...datePickerMonthsProps }
v-model={ month.value }
onUpdate:modelValue={ onUpdateMonth }
min={ minDate.value }
max={ maxDate.value }
/>
Expand All @@ -302,6 +307,7 @@ export const VDatePicker = genericComponent<new <
key="date-picker-years"
{ ...datePickerYearsProps }
v-model={ year.value }
onUpdate:modelValue={ onUpdateYear }
min={ minDate.value }
max={ maxDate.value }
/>
Expand All @@ -312,6 +318,8 @@ export const VDatePicker = genericComponent<new <
v-model={ model.value }
v-model:month={ month.value }
v-model:year={ year.value }
onUpdate:month={ onUpdateMonth }
onUpdate:year={ onUpdateYear }
min={ minDate.value }
max={ maxDate.value }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const VDatePickerMonths = genericComponent<VDatePickerMonthsSlots>()({
'update:modelValue': (date: any) => true,
},

setup (props, { slots }) {
setup (props, { emit, slots }) {
const adapter = useDate()
const model = useProxiedModel(props, 'modelValue')

Expand Down Expand Up @@ -82,6 +82,10 @@ export const VDatePickerMonths = genericComponent<VDatePickerMonthsSlots>()({
} as const

function onClick (i: number) {
if (model.value === i) {
emit('update:modelValue', model.value)
return
}
model.value = i
}

Expand All @@ -93,7 +97,6 @@ export const VDatePickerMonths = genericComponent<VDatePickerMonthsSlots>()({
<VBtn
key="month"
{ ...btnProps }
onClick={ () => onClick(i) }
/>
)
})}
Expand Down
10 changes: 8 additions & 2 deletions packages/vuetify/src/components/VDatePicker/VDatePickerYears.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const VDatePickerYears = genericComponent<VDatePickerYearsSlots>()({
'update:modelValue': (year: number) => true,
},

setup (props, { slots }) {
setup (props, { emit, slots }) {
const adapter = useDate()
const model = useProxiedModel(props, 'modelValue')
const years = computed(() => {
Expand Down Expand Up @@ -109,7 +109,13 @@ export const VDatePickerYears = genericComponent<VDatePickerYearsSlots>()({
rounded: true,
text: year.text,
variant: model.value === year.value ? 'flat' : 'text',
onClick: () => model.value = year.value,
onClick: () => {
if (model.value === year.value) {
emit('update:modelValue', model.value)
return
}
model.value = year.value
},
} as const

return slots.year?.({
Expand Down
Loading