Skip to content

Commit

Permalink
Fix primefaces#3258: Calendar setValue should update view date
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Sep 8, 2022
1 parent d136962 commit 99793be
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions components/lib/calendar/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2330,6 +2330,7 @@ export const Calendar = React.memo(

if (props.value) {
updateInputfield(props.value);
setValue(props.value);
}

return () => {
Expand All @@ -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]);

Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 99793be

Please sign in to comment.