(newValue: DateValue | IncompleteDate | null)
| 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); |
no test coverage detected