Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(DatePanel): stop displaying an entire line of dates for the … #856

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
}
Loading