Skip to content

Commit 6a0a839

Browse files
refactor: use useState
1 parent 8acff74 commit 6a0a839

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

packages/@react-stately/datepicker/src/useDateFieldState.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,7 @@ export function useDateFieldState<T extends DateValue = DateValue>(props: DateFi
188188
);
189189

190190
const [isValueConfirmed, setIsValueConfirmed] = useState(!!(value));
191-
192-
193-
const previousValue = useRef(value);
191+
const [previousValue, setPreviousValue] = useState(value);
194192

195193
let [initialValue] = useState(value);
196194
let calendarValue = useMemo(() => convertValue(value, calendar) ?? null, [value, calendar]);
@@ -247,21 +245,21 @@ export function useDateFieldState<T extends DateValue = DateValue>(props: DateFi
247245
}, [calendar, granularity, validSegments, defaultTimeZone, props.placeholderValue]);
248246

249247
// If there is a value prop, and some segments were previously placeholders, mark them all as valid.
250-
if (value !== previousValue.current && value && Object.keys(validSegments).length <= Object.keys(allSegments).length) {
248+
if (value !== previousValue && value && Object.keys(validSegments).length <= Object.keys(allSegments).length) {
251249
validSegments = {...allSegments};
252250
setValidSegments(validSegments);
253251
setPlaceholderDate(value);
254-
previousValue.current = value;
252+
setPreviousValue(value);
255253
setIsValueConfirmed(true);
256254
}
257255

258256

259257
// If the value is set to null and all segments are valid, reset the placeholder.
260-
if (value !== previousValue.current && value == null && Object.keys(validSegments).length === Object.keys(allSegments).length) {
258+
if (value !== previousValue && value == null && Object.keys(validSegments).length === Object.keys(allSegments).length) {
261259
validSegments = {};
262260
setValidSegments(validSegments); // reason
263261
setPlaceholderDate(createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone));
264-
previousValue.current = value;
262+
setPreviousValue(value);
265263
setIsValueConfirmed(true);
266264
}
267265
// If all segments are valid, use the date from state, otherwise use the placeholder date.
@@ -277,6 +275,7 @@ export function useDateFieldState<T extends DateValue = DateValue>(props: DateFi
277275
// if all the segments are completed or a timefield with everything but am/pm set the time, also ignore when am/pm cleared
278276
if (newValue == null) {
279277
setDate(null);
278+
setPreviousValue(null);
280279
setPlaceholderDate(createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone));
281280
setValidSegments({});
282281
} else if (
@@ -296,7 +295,7 @@ export function useDateFieldState<T extends DateValue = DateValue>(props: DateFi
296295
const value = toCalendar(newValue, v?.calendar || new GregorianCalendar());
297296
setDate(value);
298297
setIsValueConfirmed(true);
299-
previousValue.current = value;
298+
setPreviousValue(value);
300299
setPlaceholderDate(value);
301300
}
302301
};
@@ -444,8 +443,8 @@ export function useDateFieldState<T extends DateValue = DateValue>(props: DateFi
444443
setValue(constrainDate(currentValue.current));
445444
} else {
446445
setDate(null);
446+
setPreviousValue(null);
447447
setIsValueConfirmed(true);
448-
previousValue.current = null;
449448
}
450449
},
451450
clearSegment(part) {

0 commit comments

Comments
 (0)