feat: fallback to date-fns.parseISO when date cannot be parsed (#1080)

This commit is contained in:
oliverpool 2024-02-07 20:26:28 +01:00 committed by GitHub
parent 9b26aff35b
commit 7bd97328ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -82,7 +82,14 @@ const DateTimeControl: FC<WidgetControlProps<string | Date, DateTimeField>> = ({
return valueToParse; return valueToParse;
} }
return storageFormat ? parse(valueToParse, storageFormat, new Date()) : parseISO(valueToParse); if (storageFormat) {
const parsed = parse(valueToParse, storageFormat, new Date());
// if parsing fails, Invalid Date (NaN) will be returned: fallback to parseISO
if (!isNaN(parsed.getTime())) {
return parsed;
}
}
return parseISO(valueToParse);
}, [defaultValue, storageFormat, internalValue]); }, [defaultValue, storageFormat, internalValue]);
const handleChange = useCallback( const handleChange = useCallback(