From 8d15fa4fdf2f6a2fc056d7587551e37cd5b08285 Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 27 Aug 2022 11:29:30 +0800 Subject: [PATCH] fix(Calendar): allow default date to be maxDate when enable allowSameDay (#10976) --- packages/vant/src/calendar/Calendar.tsx | 2 +- packages/vant/src/calendar/test/prop.spec.ts | 38 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/vant/src/calendar/Calendar.tsx b/packages/vant/src/calendar/Calendar.tsx index 823ee3856..7ba8d334d 100644 --- a/packages/vant/src/calendar/Calendar.tsx +++ b/packages/vant/src/calendar/Calendar.tsx @@ -149,7 +149,7 @@ export default defineComponent({ const start = limitDateRange( defaultDate[0] || now, minDate, - getPrevDay(maxDate) + allowSameDay ? maxDate : getPrevDay(maxDate) ); const end = limitDateRange( defaultDate[1] || now, diff --git a/packages/vant/src/calendar/test/prop.spec.ts b/packages/vant/src/calendar/test/prop.spec.ts index 5e9002996..8f3ebf912 100644 --- a/packages/vant/src/calendar/test/prop.spec.ts +++ b/packages/vant/src/calendar/test/prop.spec.ts @@ -270,3 +270,41 @@ test('should emit over-range when exceeded max range', async () => { expect(onOverRange).toHaveBeenCalledTimes(1); }); + +test('should allow default date to be minDate when using allowSameDay prop', () => { + const minDate = new Date(1800, 0, 1); + const maxDate = new Date(1800, 0, 29); + const wrapper = mount(Calendar, { + props: { + type: 'range', + poppable: false, + minDate, + maxDate, + defaultDate: [minDate, minDate], + lazyRender: false, + allowSameDay: true, + }, + }); + + wrapper.find('.van-calendar__confirm').trigger('click'); + expect(wrapper.emitted<[Date]>('confirm')![0][0]).toEqual([minDate, minDate]); +}); + +test('should allow default date to be maxDate when using allowSameDay prop', () => { + const minDate = new Date(1800, 0, 1); + const maxDate = new Date(1800, 0, 29); + const wrapper = mount(Calendar, { + props: { + type: 'range', + poppable: false, + minDate, + maxDate, + defaultDate: [maxDate, maxDate], + lazyRender: false, + allowSameDay: true, + }, + }); + + wrapper.find('.van-calendar__confirm').trigger('click'); + expect(wrapper.emitted<[Date]>('confirm')![0][0]).toEqual([maxDate, maxDate]); +});