Fix calendar month value type to use Date instead of string
Fixed invalid date error when selecting month: - Changed valueYear (string) to valueMonth (Date) state - Set valueMonth using moment().toDate() for all calendar types - Parse startMonth, endMonth, singleMonth with moment and convert to Date - Calendar component now receives proper Date object instead of year string - This fixes the "Invalid date" error and prevents error cascade Changes: - useState<string> → useState<Date | undefined> - setValueYear(year) → setValueMonth(moment().toDate()) - Calendar value prop now receives Date object 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -29,7 +29,7 @@ const NiceCalendarMonth = ({
|
||||
const { i18n } = useTranslation();
|
||||
const currentLocale = i18n.language || 'en';
|
||||
|
||||
const [valueYear, setValueYear] = useState<string>();
|
||||
const [valueMonth, setValueMonth] = useState<Date | undefined>();
|
||||
const [minMonth, setMinMonth] = useState<Date | undefined>();
|
||||
const [maxMonth, setMaxMonth] = useState<Date | undefined>();
|
||||
const onChangeToMonth = (selectedMonth: any) => {
|
||||
@@ -50,7 +50,12 @@ const NiceCalendarMonth = ({
|
||||
setMaxMonth(endDate.toDate());
|
||||
}
|
||||
}
|
||||
setValueYear(startMonth?.substring(0, 4));
|
||||
if (startMonth) {
|
||||
const startDate = moment(startMonth, ['YYYY.MM', 'YYYYMM'], true);
|
||||
if (startDate.isValid()) {
|
||||
setValueMonth(startDate.toDate());
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(calendarType === CalendarType.End){
|
||||
if(!!startMonth){
|
||||
@@ -61,10 +66,20 @@ const NiceCalendarMonth = ({
|
||||
}
|
||||
}
|
||||
setMaxMonth(new Date());
|
||||
setValueYear(endMonth?.substring(0, 4));
|
||||
if (endMonth) {
|
||||
const endDate = moment(endMonth, ['YYYY.MM', 'YYYYMM'], true);
|
||||
if (endDate.isValid()) {
|
||||
setValueMonth(endDate.toDate());
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(calendarType === CalendarType.Single){
|
||||
setValueYear(singleMonth?.substring(0, 4));
|
||||
if (singleMonth) {
|
||||
const singleDate = moment(singleMonth, ['YYYY.MM', 'YYYYMM'], true);
|
||||
if (singleDate.isValid()) {
|
||||
setValueMonth(singleDate.toDate());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -115,7 +130,7 @@ const NiceCalendarMonth = ({
|
||||
minDate={ minMonth }
|
||||
maxDate={ maxMonth }
|
||||
onClickMonth={ onChangeToMonth }
|
||||
value={ valueYear }
|
||||
value={ valueMonth }
|
||||
formatMonthYear={ formatMonthYear }
|
||||
formatYear= { formatYear }
|
||||
formatMonth={ formmatMonth }
|
||||
|
||||
Reference in New Issue
Block a user