MCPcopy Create free account
hub / github.com/adobe/react-spectrum / setValue

Function setValue

packages/react-stately/src/datepicker/useDateFieldState.ts:305–341  ·  view source on GitHub ↗
(newValue: DateValue | IncompleteDate | null)

Source from the content-addressed store, hash-verified

303 }
304
305 let setValue = (newValue: DateValue | IncompleteDate | null) => {
306 if (props.isDisabled || props.isReadOnly) {
307 return;
308 }
309
310 if (
311 newValue == null ||
312 (newValue instanceof IncompleteDate && newValue.isCleared(displaySegments))
313 ) {
314 setDisplayValue(new IncompleteDate(calendar, hourCycle, calendarValue));
315 setDate(null);
316 } else if (!(newValue instanceof IncompleteDate)) {
317 // The display calendar should not have any effect on the emitted value.
318 // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
319 newValue = toCalendar(newValue, v?.calendar || new GregorianCalendar());
320 setDisplayValue(new IncompleteDate(calendar, hourCycle, calendarValue));
321 setDate(newValue);
322 } else {
323 // If the new value is complete and valid, trigger onChange eagerly.
324 // If it represents an incomplete or invalid value (e.g. February 30th),
325 // wait until the field is blurred to trigger onChange.
326 if (newValue.isComplete(displaySegments)) {
327 let dateValue = newValue.toValue(calendarValue ?? placeholder);
328 if (newValue.validate(dateValue, displaySegments)) {
329 let newDateValue = toCalendar(dateValue, v?.calendar || new GregorianCalendar());
330 if (!value || newDateValue.compare(value) !== 0) {
331 setDisplayValue(new IncompleteDate(calendar, hourCycle, calendarValue)); // reset in case prop isn't updated
332 setDate(newDateValue);
333 return;
334 }
335 }
336 }
337
338 // Incomplete/invalid value. Set temporary display override.
339 setDisplayValue(newValue);
340 }
341 };
342
343 let dateValue = useMemo(() => {
344 let v = displayValue.toValue(calendarValue ?? placeholder);

Callers 8

onChangeFunction · 0.70
commitValueFunction · 0.70
selectDateFunction · 0.70
adjustSegmentFunction · 0.70
incrementToMaxFunction · 0.70
decrementToMinFunction · 0.70
setSegmentFunction · 0.70
clearSegmentFunction · 0.70

Calls 7

toCalendarFunction · 0.90
setDateFunction · 0.85
isClearedMethod · 0.80
isCompleteMethod · 0.80
toValueMethod · 0.80
validateMethod · 0.65
compareMethod · 0.45

Tested by

no test coverage detected