Skip to content

Commit f01edcf

Browse files
Fix Calendar displaying value when controlled state is not updated (#8457)
* Fix Calendar showing value when controlled state is not updated * Fix lint issues in Calendar controlled value sync * Fix unstable updateInputfield dependency via ref * Fix: stabilize updateInputfield ref to satisfy hooks lint * Refactor useEffect for input field update --------- Co-authored-by: Melloware <mellowaredev@gmail.com>
1 parent 01d2fdf commit f01edcf

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

components/lib/calendar/Calendar.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const Calendar = React.memo(
6262
const viewChangedWithKeyDown = React.useRef(false);
6363
const onChangeRef = React.useRef(null);
6464
const isClearClicked = React.useRef(false);
65+
const updateInputfieldRef = React.useRef(null);
6566

6667
const [currentView, setCurrentView] = React.useState('date');
6768
const [currentMonth, setCurrentMonth] = React.useState(null);
@@ -1771,8 +1772,6 @@ export const Calendar = React.memo(
17711772
value: date
17721773
});
17731774
}
1774-
1775-
updateInputfield(selectedValues);
17761775
};
17771776

17781777
const decrementDecade = () => {
@@ -2562,6 +2561,8 @@ export const Calendar = React.memo(
25622561
inputRef.current.value = formattedValue;
25632562
};
25642563

2564+
updateInputfieldRef.current = updateInputfield;
2565+
25652566
const formatDateTime = (date) => {
25662567
if (props.formatDateTime) {
25672568
return props.formatDateTime(date);
@@ -3037,6 +3038,12 @@ export const Calendar = React.memo(
30373038
ObjectUtils.combinedRefs(inputRef, props.inputRef);
30383039
}, [inputRef, props.inputRef]);
30393040

3041+
React.useEffect(() => {
3042+
if (props.value !== previousValue) {
3043+
updateInputfieldRef.current && updateInputfieldRef.current(props.value);
3044+
}
3045+
}, [props.value, previousValue]);
3046+
30403047
useMountEffect(() => {
30413048
let viewDate = getViewDate(props.viewDate);
30423049

0 commit comments

Comments
 (0)