fix(SW-649): move getDaysInMonth outside component function scope

This commit is contained in:
Chuma McPhoy
2024-10-30 14:50:54 +01:00
parent 8763346f9d
commit 1b7106deef

View File

@@ -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) {
</DatePicker>
)
}
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()
}