Skip to content

Commit ca1aaba

Browse files
committed
fix(Calendar): 点击年/月触发onChange返回时间
1 parent e2d35c0 commit ca1aaba

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

example/examples/src/routes/Calendar/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class CalendarView extends React.Component<CalendarViewProps> {
2424
<Layout>
2525
<Header title={title} description={description} />
2626
<Body>
27-
<Calendar />
27+
<Calendar onChange={date => console.log('date', date)} />
2828
<Spacing size={'large'} />
2929
<Calendar color="red" lunarHoliday={true} />
3030
<Spacing size={'large'} />

packages/core/src/Calendar/index.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,37 +210,46 @@ const Calendar = (props: CalendarProps) => {
210210
onChange?.(day.date || '');
211211
};
212212
const getCurrentYear = (type: string) => {
213+
let year = 0;
214+
let month;
213215
if (type === 'last') {
214-
let year = currentYear - 1;
216+
year = currentYear - 1;
215217
setCurrentYear(year);
218+
month = currentMonth === 0 ? 11 : currentMonth;
216219
} else {
217-
let year = currentYear + 1;
220+
year = currentYear + 1;
218221
setCurrentYear(year);
222+
month = currentMonth === 11 ? 0 : currentMonth;
219223
}
224+
onChange?.(year + '-' + (month + 1) + '-' + currentDays);
220225
};
221226
const getCurrentMonth = (type: string) => {
227+
let month;
222228
if (type === 'last') {
223-
let month = currentMonth - 1;
229+
month = currentMonth - 1;
224230
if (month < 0) {
225-
getCurrentYear('last');
226231
setCurrentMonth(11);
232+
getCurrentYear('last');
227233
} else {
228234
setCurrentMonth(month);
235+
onChange?.(currentYear + '-' + (month + 1) + '-' + currentDays);
229236
}
230237
} else {
231-
let month = currentMonth + 1;
238+
month = currentMonth + 1;
232239
if (month > 11) {
233-
getCurrentYear('next');
234240
setCurrentMonth(0);
241+
getCurrentYear('next');
235242
} else {
236243
setCurrentMonth(month);
244+
onChange?.(currentYear + '-' + (month + 1) + '-' + currentDays);
237245
}
238246
}
239247
};
240248
const goToday = () => {
241249
setCurrentYear(toYear);
242250
setCurrentMonth(toMonth);
243251
setCurrentDays(toDays);
252+
onChange?.(toYear + '-' + (toMonth + 1) + '-' + toDays);
244253
};
245254
const goCurrentDay = (day: number) => {
246255
setCurrentDays(day);

0 commit comments

Comments
 (0)