Skip to content

Commit 080a25b

Browse files
author
Lansana DIOMANDE
committed
refactor(DatePanel): stop displaying an entire line of dates for the next month
1 parent 8ea9dbd commit 080a25b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/PickerPanel/DatePanel/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as React from 'react';
33
import type { PanelMode, SharedPanelProps } from '../../interface';
44
import {
55
formatValue,
6+
getNumberOfWeeksInMonth,
67
getWeekStartDate,
78
isSameDate,
89
isSameMonth,
@@ -50,6 +51,13 @@ export default function DatePanel<DateType extends object = any>(props: DatePane
5051
const baseDate = getWeekStartDate(locale.locale, generateConfig, monthStartDate);
5152
const month = generateConfig.getMonth(pickerValue);
5253

54+
const numberOfWeeksInMonth = getNumberOfWeeksInMonth(
55+
generateConfig,
56+
pickerValue,
57+
weekFirstDay,
58+
locale.locale,
59+
);
60+
5361
// =========================== PrefixColumn ===========================
5462
const showPrefixColumn = showWeek === undefined ? isWeek : showWeek;
5563
const prefixColumn = showPrefixColumn
@@ -199,7 +207,7 @@ export default function DatePanel<DateType extends object = any>(props: DatePane
199207
titleFormat={locale.fieldDateFormat}
200208
{...props}
201209
colNum={WEEK_DAY_COUNT}
202-
rowNum={6}
210+
rowNum={numberOfWeeksInMonth}
203211
baseDate={baseDate}
204212
// Header
205213
headerCells={headerCells}

src/utils/dateUtil.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { GenerateConfig } from '../generate';
22
import type { CustomFormat, InternalMode, Locale, NullableDateType } from '../interface';
3+
import { differenceInCalendarWeeks, lastDayOfMonth, startOfMonth } from 'date-fns';
34

45
export const WEEK_DAY_COUNT = 7;
56

@@ -270,3 +271,27 @@ export function fillTime<DateType>(
270271

271272
return tmpDate;
272273
}
274+
275+
type WeekStartsOnType = 0 | 3 | 1 | 4 | 2 | 5 | 6;
276+
277+
export function getNumberOfWeeksInMonth<DateType>(
278+
generateConfig: GenerateConfig<DateType>,
279+
date: DateType,
280+
weekFirstDay: number,
281+
locale: string,
282+
) {
283+
const _date = new Date(
284+
generateConfig.getYear(date),
285+
generateConfig.getMonth(date),
286+
generateConfig.getDate(date),
287+
);
288+
289+
return (
290+
differenceInCalendarWeeks(lastDayOfMonth(_date), startOfMonth(_date), {
291+
weekStartsOn: weekFirstDay as WeekStartsOnType,
292+
locale: {
293+
code: locale,
294+
},
295+
}) + 1
296+
);
297+
}

0 commit comments

Comments
 (0)