🩹(react) harmonize date picker components output
By introducing this utility function, we ensure that any output value is converted back to an ISO 8601 date and time string in UTC timezone. Overall, it harmonize and factorize the way we output values from date picker components.
This commit is contained in:
committed by
aleb_the_flash
parent
bae7392fe1
commit
8cf8e1eba2
@@ -1,7 +1,11 @@
|
||||
import {
|
||||
CalendarDate,
|
||||
DateValue,
|
||||
parseAbsoluteToLocal,
|
||||
toCalendarDate,
|
||||
ZonedDateTime,
|
||||
toZoned,
|
||||
getLocalTimeZone,
|
||||
} from "@internationalized/date";
|
||||
import { DateRange } from "react-aria";
|
||||
import {
|
||||
@@ -38,6 +42,19 @@ export const parseRangeCalendarDate = (
|
||||
};
|
||||
};
|
||||
|
||||
export const convertDateValueToString = (date: DateValue | null): string => {
|
||||
try {
|
||||
const localTimezone = getLocalTimeZone();
|
||||
// If timezone is already set, it would be kept, else the selection is set at midnight
|
||||
// on the local timezone, then converted to a UTC offset.
|
||||
return date ? toZoned(date, localTimezone).toAbsoluteString() : "";
|
||||
} catch (e) {
|
||||
throw new Error(
|
||||
"Invalid date format when converting date value on DatePicker component"
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export const getDefaultPickerOptions = (props: DatePickerAuxSubProps): any => ({
|
||||
minValue: parseCalendarDate(props.minValue),
|
||||
maxValue: parseCalendarDate(props.maxValue),
|
||||
|
||||
Reference in New Issue
Block a user