From d56def8ed64b2cad2005f0b2b7602d4b32c227b0 Mon Sep 17 00:00:00 2001 From: Michael Zetterberg Date: Thu, 17 Oct 2024 11:39:42 +0200 Subject: [PATCH] fix: date selection --- .../TempDesignSystem/Form/Date/index.tsx | 59 +++++-------------- 1 file changed, 14 insertions(+), 45 deletions(-) diff --git a/components/TempDesignSystem/Form/Date/index.tsx b/components/TempDesignSystem/Form/Date/index.tsx index 222bbd1c1..7441ff471 100644 --- a/components/TempDesignSystem/Form/Date/index.tsx +++ b/components/TempDesignSystem/Form/Date/index.tsx @@ -28,62 +28,36 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) { rules: registerOptions, }) - const [dateSegments, setDateSegment] = useState<{ - year: number | null - month: number | null - date: number | null - daysInMonth: number - }>({ - year: null, - month: null, - date: null, - daysInMonth: 31, - }) - const currentYear = new Date().getFullYear() + const months = rangeArray(1, 12).map((month) => ({ value: month, label: `${month}`, })) + const years = rangeArray(1900, currentYear - 18) .reverse() .map((year) => ({ value: year, label: year.toString() })) + // Ensure the user can't select a date that doesn't exist. + const daysInMonth = dt(currentValue).daysInMonth() + const days = rangeArray(1, daysInMonth).map((day) => ({ + value: day, + label: `${day}`, + })) + function createOnSelect(selector: DateName) { /** * Months are 0 index based and therefore we * must subtract by 1 to get the selected month */ return (select: Key) => { - const value = - selector === DateName.month ? Number(select) - 1 : Number(select) - const newSegments = { ...dateSegments, [selector]: value } - - /** - * Update daysInMonth when year or month changes - * to ensure the user can't select a date that doesn't exist. - */ - if (selector === DateName.year || selector === DateName.month) { - const year = selector === DateName.year ? value : newSegments.year - const month = selector === DateName.month ? value : newSegments.month - if (year !== null && month !== null) { - newSegments.daysInMonth = dt().year(year).month(month).daysInMonth() - } else if (month !== null) { - newSegments.daysInMonth = dt().month(month).daysInMonth() - } + if (selector === DateName.month) { + select = Number(select) - 1 } - - if (Object.values(newSegments).every((val) => val !== null)) { - const newDate = dt() - .utc() - .set("year", newSegments.year!) - .set("month", newSegments.month!) - .set("date", Math.min(newSegments.date!, newSegments.daysInMonth)) - - setValue(name, newDate.format("YYYY-MM-DD")) - trigger(name) - } - setDateSegment(newSegments) + const newDate = dt(currentValue).set(selector, Number(select)) + setValue(name, newDate.format("YYYY-MM-DD")) + trigger(name) } } @@ -117,11 +91,6 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) { {(segment) => { switch (segment.type) { case "day": - const maxDays = dateSegments.daysInMonth - const days = rangeArray(1, maxDays).map((day) => ({ - value: day, - label: `${day}`, - })) return (