fix: handle default values in date select
This commit is contained in:
@@ -76,19 +76,17 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) {
|
||||
}
|
||||
}
|
||||
|
||||
if (year && month && day) {
|
||||
const newDate = dt()
|
||||
.year(Number(year))
|
||||
.month(Number(month) - 1)
|
||||
.date(Number(day))
|
||||
const newDate = dt()
|
||||
.year(Number(year))
|
||||
.month(Number(month) - 1)
|
||||
.date(Number(day))
|
||||
|
||||
if (newDate.isValid()) {
|
||||
setValue(name, newDate.format("YYYY-MM-DD"), {
|
||||
shouldDirty: true,
|
||||
shouldTouch: true,
|
||||
shouldValidate: true,
|
||||
})
|
||||
}
|
||||
if (newDate.isValid()) {
|
||||
setValue(name, newDate.format("YYYY-MM-DD"), {
|
||||
shouldDirty: true,
|
||||
shouldTouch: true,
|
||||
shouldValidate: true,
|
||||
})
|
||||
}
|
||||
}, [year, month, day, setValue, name, formState.isSubmitting])
|
||||
|
||||
@@ -106,6 +104,16 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) {
|
||||
console.warn("Known error for parse date in DateSelect: ", error)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (formState.isSubmitting) return
|
||||
|
||||
if (!(day && month && year) && dateValue) {
|
||||
setValue(DateName.day, Number(dateValue.day))
|
||||
setValue(DateName.month, Number(dateValue.month))
|
||||
setValue(DateName.year, Number(dateValue.year))
|
||||
}
|
||||
}, [setValue, formState.isSubmitting, dateValue, day, month, year])
|
||||
|
||||
return (
|
||||
<DatePicker
|
||||
aria-label={intl.formatMessage({ id: "Select date of birth" })}
|
||||
|
||||
Reference in New Issue
Block a user