This commit is contained in:
focp212@naver.com
2025-11-12 18:16:19 +09:00
parent bfcb63681f
commit 2abf0ebab4
2 changed files with 31 additions and 22 deletions

View File

@@ -15,7 +15,7 @@ interface NiceCalendarProps {
endMonth?: string; endMonth?: string;
singleMonth?: string; singleMonth?: string;
calendarType: CalendarType; calendarType: CalendarType;
setNewMonth: (month: string) => void; setNewMonth: (month: string, calendarType: CalendarType) => void;
searchPeriod?: number; searchPeriod?: number;
periodType?: 'year' | 'month'; periodType?: 'year' | 'month';
}; };
@@ -37,8 +37,28 @@ const NiceCalendarMonth = ({
const [valueMonth, setValueMonth] = useState<Date | undefined>(); const [valueMonth, setValueMonth] = useState<Date | undefined>();
const [minMonth, setMinMonth] = useState<Date | undefined>(); const [minMonth, setMinMonth] = useState<Date | undefined>();
const [maxMonth, setMaxMonth] = useState<Date | undefined>(); const [maxMonth, setMaxMonth] = useState<Date | undefined>();
const [calendarSearchPeriod, setCalendatSearchPeriod] = useState<number>(searchPeriod || 2);
const [calendarPeriodType, setCalendarPeriodType] = useState<'year' | 'month'>(periodType || 'year');
const onChangeToMonth = (selectedMonth: any) => { const onChangeToMonth = (selectedMonth: any) => {
setNewMonth(moment(selectedMonth).format('YYYY.MM')); if(calendarType === CalendarType.Start){
if(endMonth){
let newEndDateLimit = moment(selectedMonth).add(calendarSearchPeriod, calendarPeriodType).format('YYYYMM');
if(moment(endMonth).format('YYYYMM') > newEndDateLimit){
setNewMonth(moment(newEndDateLimit).format('YYYY.MM'), CalendarType.End);
}
}
}
else if(calendarType === CalendarType.End){
if(startMonth){
let newStartDateLimit = moment(selectedMonth).subtract(calendarSearchPeriod, calendarPeriodType).format('YYYYMM');
if(moment(startMonth).format('YYYYMM') < newStartDateLimit){
setNewMonth(moment(newStartDateLimit).format('YYYY.MM'), CalendarType.Start);
}
}
}
setNewMonth(moment(selectedMonth).format('YYYY.MM'), calendarType);
setCalendarOpen(false); setCalendarOpen(false);
}; };
@@ -47,30 +67,20 @@ const NiceCalendarMonth = ({
}; };
const setMinMaxValueDate = () => { const setMinMaxValueDate = () => {
if(calendarType === CalendarType.Start){ if(calendarType === CalendarType.Start){
if(!searchPeriod){ setMinMonth(undefined);
searchPeriod = 3;
}
if(!periodType){
periodType = 'year';
}
if(periodType && searchPeriod && searchPeriod > 0){
const endDate = moment(endMonth, ['YYYY.MM', 'YYYYMM'], true);
setMinMonth(moment(endDate, 'YYYY.MM.DD').subtract(searchPeriod, periodType).toDate());
}
// setMinMonth(undefined);
if(!!endMonth){ if(!!endMonth){
// Parse endMonth with moment, handling both YYYY.MM and YYYYMM formats
const endDate = moment(endMonth, ['YYYY.MM', 'YYYYMM'], true); const endDate = moment(endMonth, ['YYYY.MM', 'YYYYMM'], true);
if (endDate.isValid()) { if(endDate.isValid()){
setMaxMonth(endDate.toDate()); setMaxMonth(endDate.toDate());
} }
} }
if (startMonth) { if(startMonth){
const startDate = moment(startMonth, ['YYYY.MM', 'YYYYMM'], true); const startDate = moment(startMonth, ['YYYY.MM', 'YYYYMM'], true);
if (startDate.isValid()) { if (startDate.isValid()) {
setValueMonth(startDate.toDate()); setValueMonth(startDate.toDate());
} }
} else { }
else{
setValueMonth(undefined); setValueMonth(undefined);
} }
} }
@@ -85,10 +95,11 @@ const NiceCalendarMonth = ({
setMaxMonth(new Date()); setMaxMonth(new Date());
if (endMonth) { if (endMonth) {
const endDate = moment(endMonth, ['YYYY.MM', 'YYYYMM'], true); const endDate = moment(endMonth, ['YYYY.MM', 'YYYYMM'], true);
if (endDate.isValid()) { if(endDate.isValid()){
setValueMonth(endDate.toDate()); setValueMonth(endDate.toDate());
} }
} else { }
else{
setValueMonth(undefined); setValueMonth(undefined);
} }
} }

View File

@@ -65,9 +65,7 @@ export const FilterCalendarMonth = ({
} }
}; };
const setNewMonth = (month: string) => { const setNewMonth = (month: string, calendarType: CalendarType) => {
console.log(month)
// month is already in 'YYYY.MM' format from NiceCalendarMonth
const parsedMonth = moment(month, 'YYYY.MM', true); const parsedMonth = moment(month, 'YYYY.MM', true);
if (parsedMonth.isValid()) { if (parsedMonth.isValid()) {
if(calendarType === CalendarType.Start){ if(calendarType === CalendarType.Start){