fix(SW-649): move getDaysInMonth outside component function scope
This commit is contained in:
@@ -21,7 +21,7 @@ import type { DateProps } from "./date"
|
|||||||
|
|
||||||
export default function DateSelect({ name, registerOptions = {} }: DateProps) {
|
export default function DateSelect({ name, registerOptions = {} }: DateProps) {
|
||||||
const intl = useIntl()
|
const intl = useIntl()
|
||||||
const { control, setValue, trigger, formState, watch } = useFormContext()
|
const { control, setValue, formState, watch } = useFormContext()
|
||||||
const { field, fieldState } = useController({
|
const { field, fieldState } = useController({
|
||||||
control,
|
control,
|
||||||
name,
|
name,
|
||||||
@@ -43,20 +43,6 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) {
|
|||||||
.reverse()
|
.reverse()
|
||||||
.map((year) => ({ value: year, label: year.toString() }))
|
.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
|
// Calculate available days based on selected year and month
|
||||||
const daysInMonth = getDaysInMonth(
|
const daysInMonth = getDaysInMonth(
|
||||||
year ? Number(year) : null,
|
year ? Number(year) : null,
|
||||||
@@ -202,3 +188,17 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) {
|
|||||||
</DatePicker>
|
</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()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user