From 1b7106deef0a3f8e92d9e65542e65d496445c9b5 Mon Sep 17 00:00:00 2001 From: Chuma McPhoy Date: Wed, 30 Oct 2024 14:50:54 +0100 Subject: [PATCH] fix(SW-649): move getDaysInMonth outside component function scope --- .../TempDesignSystem/Form/Date/index.tsx | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/components/TempDesignSystem/Form/Date/index.tsx b/components/TempDesignSystem/Form/Date/index.tsx index 36a0ff44a..268b2c30d 100644 --- a/components/TempDesignSystem/Form/Date/index.tsx +++ b/components/TempDesignSystem/Form/Date/index.tsx @@ -21,7 +21,7 @@ import type { DateProps } from "./date" export default function DateSelect({ name, registerOptions = {} }: DateProps) { const intl = useIntl() - const { control, setValue, trigger, formState, watch } = useFormContext() + const { control, setValue, formState, watch } = useFormContext() const { field, fieldState } = useController({ control, name, @@ -43,20 +43,6 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) { .reverse() .map((year) => ({ value: year, label: year.toString() })) - function getDaysInMonth(year: number | null, month: number | null): number { - if (month === null) { - return 31 - } - - // If month is February and no year selected, return minimum. - if (month === 1 && !year) { - return 28 - } - - const yearToUse = year ?? new Date().getFullYear() - return dt(`${yearToUse}-${month + 1}-01`).daysInMonth() - } - // Calculate available days based on selected year and month const daysInMonth = getDaysInMonth( year ? Number(year) : null, @@ -202,3 +188,17 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) { ) } + +function getDaysInMonth(year: number | null, month: number | null): number { + if (month === null) { + return 31 + } + + // If month is February and no year selected, return minimum. + if (month === 1 && !year) { + return 28 + } + + const yearToUse = year ?? new Date().getFullYear() + return dt(`${yearToUse}-${month + 1}-01`).daysInMonth() +}