Skip to content

Commit

Permalink
refactor(DatePanel): stop displaying an entire line of dates for the …
Browse files Browse the repository at this point in the history
…next month
  • Loading branch information
Lansana DIOMANDE committed Aug 6, 2024
1 parent 8ea9dbd commit 080a25b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/PickerPanel/DatePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as React from 'react';
import type { PanelMode, SharedPanelProps } from '../../interface';
import {
formatValue,
getNumberOfWeeksInMonth,
getWeekStartDate,
isSameDate,
isSameMonth,
Expand Down Expand Up @@ -50,6 +51,13 @@ export default function DatePanel<DateType extends object = any>(props: DatePane
const baseDate = getWeekStartDate(locale.locale, generateConfig, monthStartDate);
const month = generateConfig.getMonth(pickerValue);

const numberOfWeeksInMonth = getNumberOfWeeksInMonth(
generateConfig,
pickerValue,
weekFirstDay,
locale.locale,
);

// =========================== PrefixColumn ===========================
const showPrefixColumn = showWeek === undefined ? isWeek : showWeek;
const prefixColumn = showPrefixColumn
Expand Down Expand Up @@ -199,7 +207,7 @@ export default function DatePanel<DateType extends object = any>(props: DatePane
titleFormat={locale.fieldDateFormat}
{...props}
colNum={WEEK_DAY_COUNT}
rowNum={6}
rowNum={numberOfWeeksInMonth}
baseDate={baseDate}
// Header
headerCells={headerCells}
Expand Down
25 changes: 25 additions & 0 deletions src/utils/dateUtil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GenerateConfig } from '../generate';
import type { CustomFormat, InternalMode, Locale, NullableDateType } from '../interface';
import { differenceInCalendarWeeks, lastDayOfMonth, startOfMonth } from 'date-fns';

export const WEEK_DAY_COUNT = 7;

Expand Down Expand Up @@ -270,3 +271,27 @@ export function fillTime<DateType>(

return tmpDate;
}

type WeekStartsOnType = 0 | 3 | 1 | 4 | 2 | 5 | 6;

export function getNumberOfWeeksInMonth<DateType>(
generateConfig: GenerateConfig<DateType>,
date: DateType,
weekFirstDay: number,
locale: string,
) {
const _date = new Date(
generateConfig.getYear(date),
generateConfig.getMonth(date),
generateConfig.getDate(date),
);

return (
differenceInCalendarWeeks(lastDayOfMonth(_date), startOfMonth(_date), {
weekStartsOn: weekFirstDay as WeekStartsOnType,
locale: {
code: locale,
},
}) + 1
);
}

0 comments on commit 080a25b

Please sign in to comment.