From ebecad7f9edf65b9cc979f6331ebd0145819c536 Mon Sep 17 00:00:00 2001 From: Paolo Guerra Date: Mon, 30 Oct 2023 18:10:07 +0100 Subject: [PATCH] fix(ui): initialize min and max date if missing (#478) * fix(ui): initialize min and max date if missing * docs: add changeset --- .changeset/chilly-lizards-prove.md | 5 +++++ .../ui/src/components/date-time-input/picker/index.tsx | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 .changeset/chilly-lizards-prove.md diff --git a/.changeset/chilly-lizards-prove.md b/.changeset/chilly-lizards-prove.md new file mode 100644 index 000000000..7ac94a88f --- /dev/null +++ b/.changeset/chilly-lizards-prove.md @@ -0,0 +1,5 @@ +--- +"@carrot-kpi/ui": patch +--- + +Initialize min and max date if missing diff --git a/packages/ui/src/components/date-time-input/picker/index.tsx b/packages/ui/src/components/date-time-input/picker/index.tsx index 42ca4d4dd..aeda6ebdb 100644 --- a/packages/ui/src/components/date-time-input/picker/index.tsx +++ b/packages/ui/src/components/date-time-input/picker/index.tsx @@ -85,10 +85,13 @@ export const DateTimePicker = ({ max: maxDate, }: DateTimePickerProps) => { const [min, setMin] = useState(minDate); - const [max] = useState(maxDate); + const [max, setMax] = useState(maxDate); // avoid inconsistent min and max values useEffect(() => { + if (!min || !dayjs(min).isValid()) setMin(minDate); + if (!max || !dayjs(max).isValid()) setMax(maxDate); + if (dayjs(min).isAfter(dayjs(max))) { setMin(max); console.warn("inconsistent min and max values", { @@ -96,7 +99,7 @@ export const DateTimePicker = ({ max: max?.toISOString(), }); } - }, [min, max]); + }, [min, max, minDate, maxDate]); // in case a value change happened, check if we're still // alright with validation and rectify if needed