Skip to content

Commit

Permalink
fix(DateRangePicker): 引入isoWeek解决跨年周计算问题
Browse files Browse the repository at this point in the history
  • Loading branch information
huangchen1031 committed Dec 10, 2024
1 parent ebe7006 commit 653f723
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/date-picker/base/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useMemo } from 'react';
import classNames from 'classnames';
import dayjs from 'dayjs';
import isoWeek from 'dayjs/plugin/isoWeek';
import { useLocaleReceiver } from '../../locale/LocalReceiver';
import useConfig from '../../hooks/useConfig';
import DatePickerCell from './Cell';
Expand All @@ -8,6 +10,8 @@ import { SinglePanelProps } from '../panel/SinglePanel';
import { PanelContentProps } from '../panel/PanelContent';
import { parseToDayjs } from '../../_common/js/date-picker/format';

dayjs.extend(isoWeek);

export interface DatePickerTableProps
extends Pick<TdDatePickerProps, 'mode' | 'firstDayOfWeek' | 'format'>,
Pick<SinglePanelProps, 'onCellClick' | 'onCellMouseEnter' | 'onCellMouseLeave'>,
Expand Down Expand Up @@ -62,8 +66,17 @@ const DatePickerTable = (props: DatePickerTableProps) => {
if (Array.isArray(value)) {
if (!value.length) return {};

const targetYear = targetDayjs.year();
const targetWeek = targetDayjs.week();
/**
* 原来的计算结果:
* dayjs('2024-12-30').year(); // 2024
* dayjs('2024-12-30').week(); // 1
*
* 引入 isoWeek 插件后, 使用 isoWeekYear 和 isoWeek 方法:
* dayjs('2024-12-30').isoWeekYear(); // 2025
* dayjs('2024-12-30').isoWeek(); // 1
*/
const targetYear = targetDayjs.isoWeekYear();
const targetWeek = targetDayjs.isoWeek();
const isActive =
(targetYear === valueYearWeek.startYear && targetWeek === valueYearWeek.startWeek) ||
(targetYear === valueYearWeek.endYear && targetWeek === valueYearWeek.endWeek);
Expand Down

0 comments on commit 653f723

Please sign in to comment.