From dc5192ac7339f4516165faec586f901084bc25ac Mon Sep 17 00:00:00 2001 From: melloware Date: Tue, 1 Nov 2022 12:53:10 -0400 Subject: [PATCH] Fix #3545: Calendar set range programmatically --- components/lib/calendar/Calendar.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/components/lib/calendar/Calendar.js b/components/lib/calendar/Calendar.js index 3d8ce62e8e..3766153059 100644 --- a/components/lib/calendar/Calendar.js +++ b/components/lib/calendar/Calendar.js @@ -2496,10 +2496,27 @@ export const Calendar = React.memo( // #3516 view date not updated when value set programatically if (!visible && newDate) { - validateDate(newDate); - setViewDateState(newDate); - setCurrentMonth(newDate.getMonth()); - setCurrentYear(newDate.getFullYear()); + let viewDate; + + if (isSingleSelection()) { + viewDate = newDate; + } else if (isMultipleSelection()) { + if (newDate.length) { + viewDate = newDate[newDate.length - 1]; + } + } else if (isRangeSelection()) { + if (newDate.length) { + let startDate = newDate[0]; + let endDate = newDate[1]; + + viewDate = endDate || startDate; + } + } + + validateDate(viewDate); + setViewDateState(viewDate); + setCurrentMonth(viewDate.getMonth()); + setCurrentYear(viewDate.getFullYear()); } } }, [props.value, visible]);