diff --git a/packages/vant/src/calendar/Calendar.tsx b/packages/vant/src/calendar/Calendar.tsx index 1ed7d4a32..fae18ad93 100644 --- a/packages/vant/src/calendar/Calendar.tsx +++ b/packages/vant/src/calendar/Calendar.tsx @@ -622,9 +622,8 @@ export default defineComponent({ ); watch( () => props.defaultDate, - (value = null) => { - currentDate.value = value; - scrollToCurrentDate(); + (value) => { + reset(value); }, ); diff --git a/packages/vant/src/calendar/test/prop.spec.ts b/packages/vant/src/calendar/test/prop.spec.ts index eca31b095..d92a26451 100644 --- a/packages/vant/src/calendar/test/prop.spec.ts +++ b/packages/vant/src/calendar/test/prop.spec.ts @@ -308,3 +308,30 @@ test('should allow default date to be maxDate when using allowSameDay prop', () wrapper.find('.van-calendar__confirm').trigger('click'); expect(wrapper.emitted<[Date]>('confirm')![0][0]).toEqual([maxDate, maxDate]); }); + +test('should call reset when defaultDate prop changes', async () => { + const wrapper = mount(Calendar, { + props: { + poppable: false, + defaultDate: undefined, + minDate, + maxDate, + }, + }); + + wrapper.find('.van-calendar__confirm').trigger('click'); + expect(wrapper.emitted<[Date]>('confirm')![0][0]).toEqual(maxDate); + + await wrapper.setProps({ defaultDate: minDate }); + wrapper.find('.van-calendar__confirm').trigger('click'); + expect(wrapper.emitted<[Date]>('confirm')![1][0]).toEqual(minDate); + + await wrapper.setProps({ defaultDate: null }); + expect(wrapper.find('.van-calendar__confirm').classes()).toContain( + 'van-button--disabled', + ); + + await wrapper.setProps({ defaultDate: undefined }); + wrapper.find('.van-calendar__confirm').trigger('click'); + expect(wrapper.emitted<[Date]>('confirm')![2][0]).toEqual(maxDate); +});