From 8dd13eae11184323c4245f2c3953349814c9a842 Mon Sep 17 00:00:00 2001 From: pany <939630029@qq.com> Date: Mon, 2 Sep 2024 22:23:58 +0800 Subject: [PATCH] fix(Calendar): fix getInitialDate function error (#13075) Co-authored-by: neverland --- packages/vant/src/calendar/Calendar.tsx | 5 +++++ packages/vant/src/calendar/test/index.spec.ts | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/packages/vant/src/calendar/Calendar.tsx b/packages/vant/src/calendar/Calendar.tsx index 2da16a6a4..1ed7d4a32 100644 --- a/packages/vant/src/calendar/Calendar.tsx +++ b/packages/vant/src/calendar/Calendar.tsx @@ -165,6 +165,11 @@ export default defineComponent({ defaultDate = []; } + // reset invalid default date + if (defaultDate.length === 1 && compareDay(defaultDate[0], now) === 1) { + defaultDate = []; + } + const min = minDate.value; const max = maxDate.value; diff --git a/packages/vant/src/calendar/test/index.spec.ts b/packages/vant/src/calendar/test/index.spec.ts index f566beed5..e31b16516 100644 --- a/packages/vant/src/calendar/test/index.spec.ts +++ b/packages/vant/src/calendar/test/index.spec.ts @@ -655,3 +655,19 @@ test('should render confirm-text slot correctly', async () => { expect(wrapper.find('.van-calendar__confirm').html()).toMatchSnapshot(); }); + +test('the defaultDate length of 1 should be handled correctly', async () => { + const wrapper = mount(Calendar, { + props: { + poppable: false, + defaultDate: [getNextDay(now)], + type: 'range', + }, + }); + + wrapper.find('.van-calendar__confirm').trigger('click'); + expect(wrapper.emitted<[Date]>('confirm')![0][0]).toEqual([ + now, + getNextDay(now), + ]); +});