Skip to content

Commit

Permalink
fix(ui): initialize min and max date if missing (#478)
Browse files Browse the repository at this point in the history
* fix(ui): initialize min and max date if missing

* docs: add changeset
  • Loading branch information
guerrap authored and luzzif committed Oct 31, 2023
1 parent e9e02f2 commit ebecad7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-lizards-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@carrot-kpi/ui": patch
---

Initialize min and max date if missing
7 changes: 5 additions & 2 deletions packages/ui/src/components/date-time-input/picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,21 @@ 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", {
min: min?.toISOString(),
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
Expand Down

0 comments on commit ebecad7

Please sign in to comment.