From 99793be298bb41eeff5d7f504d81a42c7feeb18c Mon Sep 17 00:00:00 2001 From: melloware Date: Thu, 8 Sep 2022 07:28:21 -0400 Subject: [PATCH] Fix #3258: Calendar setValue should update view date --- components/lib/calendar/Calendar.js | 40 ++++++++++++++++------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/components/lib/calendar/Calendar.js b/components/lib/calendar/Calendar.js index fc05326231..0dc0ba293a 100644 --- a/components/lib/calendar/Calendar.js +++ b/components/lib/calendar/Calendar.js @@ -2330,6 +2330,7 @@ export const Calendar = React.memo( if (props.value) { updateInputfield(props.value); + setValue(props.value); } return () => { @@ -2343,24 +2344,7 @@ export const Calendar = React.memo( useUpdateEffect(() => { if (!props.onViewDateChange && !viewStateChanged.current) { - let propValue = props.value; - if (Array.isArray(propValue)) { - propValue = propValue[0]; - } - - let prevPropValue = previousValue; - if (Array.isArray(prevPropValue)) { - prevPropValue = prevPropValue[0]; - } - - if ((!prevPropValue && propValue) || (propValue && propValue instanceof Date && propValue.getTime() !== prevPropValue.getTime())) { - let viewDate = props.viewDate && isValidDate(props.viewDate) ? props.viewDate : propValue && isValidDate(propValue) ? propValue : new Date(); - - validateDate(viewDate); - - setViewDateState(viewDate); - viewStateChanged.current = true; - } + setValue(props.value); } }, [props.onViewDateChange, props.value]); @@ -2402,6 +2386,26 @@ export const Calendar = React.memo( getInput: () => inputRef.current })); + const setValue = (propValue) => { + if (Array.isArray(propValue)) { + propValue = propValue[0]; + } + + let prevPropValue = previousValue; + if (Array.isArray(prevPropValue)) { + prevPropValue = prevPropValue[0]; + } + + if ((!prevPropValue && propValue) || (propValue && propValue instanceof Date && propValue.getTime() !== prevPropValue.getTime())) { + let viewDate = props.viewDate && isValidDate(props.viewDate) ? props.viewDate : propValue && isValidDate(propValue) ? propValue : new Date(); + + validateDate(viewDate); + + setViewDateState(viewDate); + viewStateChanged.current = true; + } + }; + const createBackwardNavigator = (isVisible) => { const navigatorProps = isVisible ? { onClick: onPrevButtonClick, onKeyDown: (e) => onContainerButtonKeydown(e) } : { style: { visibility: 'hidden' } }; return (