Skip to content
Open
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
9 changes: 8 additions & 1 deletion src/runtime/components/Calendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,15 @@ function setPlaceholder(date: DateValue) {
emits('update:placeholder', date)
}

// The picker keeps an uncontrolled value internally and is replaced on each view change.
const uncontrolledValue = shallowRef(props.modelValue ?? props.defaultValue)
watch(() => props.modelValue, (value) => {
uncontrolledValue.value = value
})

function onSelect(value: DateValue | DateRange) {
if (isMinView.value) {
uncontrolledValue.value = value as CalendarDefaultValue<R, M>
emits('update:modelValue', value as CalendarModelValue<R, M>)
return
}
Expand Down Expand Up @@ -318,7 +325,7 @@ const ui = computed(() => tv({ extend: theme, ...(appConfig.ui?.calendar || {})
v-bind="rootProps"
:calendar-label="calendarLabel"
:model-value="(isMinView ? (props.modelValue as DateValue | DateValue[] | DateRange) : undefined)"
:default-value="(isMinView ? (props.defaultValue as DateValue | DateValue[] | DateRange) : undefined)"
:default-value="(isMinView ? (uncontrolledValue as DateValue | DateValue[] | DateRange) : undefined)"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
:placeholder="placeholder"
data-slot="root"
:class="ui.root({ class: [props.ui?.root, props.class] })"
Expand Down
23 changes: 23 additions & 0 deletions test/components/Calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ describe('Calendar', () => {
})

describe('views', () => {
test('keeps an uncontrolled selection after cycling through the views', async () => {
const wrapper = await mountSuspended(Calendar, { props: { placeholder: new CalendarDate(2025, 1, 1) } })

await wrapper.find('[data-reka-calendar-cell-trigger][data-value="2025-01-15"]').trigger('click')
for (let i = 0; i < 3; i++) {
await wrapper.find('[data-slot="heading"] button').trigger('click')
}

expect(wrapper.find('[data-reka-calendar-cell-trigger][data-selected]').attributes('data-value')).toBe('2025-01-15')
})

test('does not bring back a selection cleared by the parent after cycling through the views', async () => {
const wrapper = await mountSuspended(Calendar, { props: { 'placeholder': new CalendarDate(2025, 1, 1), 'onUpdate:modelValue': (value: any) => wrapper.setProps({ modelValue: value }) } })

await wrapper.find('[data-reka-calendar-cell-trigger][data-value="2025-01-15"]').trigger('click')
await wrapper.setProps({ modelValue: undefined })
for (let i = 0; i < 3; i++) {
await wrapper.find('[data-slot="heading"] button').trigger('click')
}

expect(wrapper.find('[data-reka-calendar-cell-trigger][data-selected]').exists()).toBe(false)
})

test('heading button cycles through day, month and year views', async () => {
const wrapper = await mountSuspended(Calendar)

Expand Down
Loading