1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-26 13:48:33 +02:00

refactor: fix crash on empty target date (#798)

* refactor: fix crash on empty target date

* refactor: remove date input clear button
This commit is contained in:
olav 2022-03-17 09:55:57 +01:00 committed by GitHub
parent 960460e61c
commit 3850cb42bd

View File

@ -1,5 +1,5 @@
import { ConstraintFormHeader } from '../ConstraintFormHeader/ConstraintFormHeader';
import { format } from 'date-fns';
import { format, isValid } from 'date-fns';
import Input from 'component/common/Input/Input';
interface IDateSingleValueProps {
setValue: (value: string) => void;
@ -31,14 +31,25 @@ export const DateSingleValue = ({
value={parseDateValue(value)}
onChange={e => {
setError('');
setValue(new Date(e.target.value).toISOString());
const parsedDate = parseValidDate(e.target.value);
const dateString = parsedDate?.toISOString();
dateString && setValue(dateString);
}}
InputLabelProps={{
shrink: true,
}}
error={Boolean(error)}
errorText={error}
required
/>
</>
);
};
const parseValidDate = (value: string): Date | undefined => {
const parsed = new Date(value);
if (isValid(parsed)) {
return parsed;
}
};