Skip to content

Commit cf0cff7

Browse files
committed
fix: include the min
1 parent bcebe67 commit cf0cff7

4 files changed

Lines changed: 105 additions & 61 deletions

File tree

packages/react-use-calendar-component/src/lib/utils/getDateInfoByIndex.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function getDateInfoByIndex(
2626
// If this offset is larger than or equal to the `numberOfDays`, means that the date at the current index is in the next month.
2727
// Otherwise means that the date at the current index is at the target month.
2828
const offset = index - weekDayOfFirstDay;
29-
const dayOfMonth =
29+
const monthDay =
3030
(offset < 0 ? prevNumberOfDays + offset : offset % numberOfDays) + 1;
3131
const monthOffset = offset < 0 ? -1 : offset >= numberOfDays ? 1 : 0;
3232
const { dateYear, dateMonth } = (() => {
@@ -56,8 +56,8 @@ export function getDateInfoByIndex(
5656
year: dateYear,
5757
month: dateMonth,
5858
weekDay: index % 7,
59-
monthDay: dayOfMonth,
59+
monthDay,
6060
monthStatus,
61-
isToday: isToday(dateYear, dateMonth, dayOfMonth),
61+
isToday: isToday(dateYear, dateMonth, monthDay),
6262
};
6363
}

packages/react-use-calendar-component/src/lib/utils/isExcludedDate.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type { ExcludedDates } from '../types';
22
import { isSameDate } from './isSameDate';
33

4-
function largerThan(a: Date, b: Date) {
5-
return a.getTime() > b.getTime();
4+
function lessThan(a: Date, b: Date) {
5+
return a.getTime() < b.getTime();
66
}
77

88
export function isExcludedDate(
99
date: Date,
1010
{ min, max, arbitrary }: ExcludedDates
1111
) {
12-
if (min && !largerThan(date, min)) return true;
13-
if (max && largerThan(date, max)) return true;
12+
if (min && lessThan(date, min)) return true;
13+
if (max && !lessThan(date, max)) return true;
1414
if (arbitrary && !!arbitrary.find(d => isSameDate(d, date))) return true;
1515

1616
return false;

packages/react-use-calendar-component/src/test/getDateInfoByIndex.test.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { act, renderHook } from '@testing-library/react';
2+
import { useState } from 'react';
3+
4+
import useCalendarComponent from '../lib';
5+
import type { ExcludedDates } from '../lib/types';
6+
7+
// use 2022-10-01 to test
8+
const now = new Date(2022, 9, 1);
9+
const nowYear = now.getFullYear();
10+
const nowMonth = now.getMonth();
11+
12+
describe('useCalendarComponent', () => {
13+
it('should generate correct calendar cell', () => {
14+
jest.useFakeTimers({ now });
15+
const { result } = renderHook(() => useCalendarComponent());
16+
const cells = result.current.getDateCellInfos();
17+
const previousCells = cells.splice(0, 6);
18+
const currentCells = cells.splice(0, 31);
19+
const nextCells = cells;
20+
21+
previousCells.forEach(({ year, month, monthDay, monthStatus }, index) => {
22+
expect(year).toBe(nowYear);
23+
expect(month).toBe(nowMonth - 1);
24+
expect(monthDay).toBe(25 + index);
25+
expect(monthStatus).toBe('previous');
26+
});
27+
currentCells.forEach(({ year, month, monthDay, monthStatus }, index) => {
28+
expect(year).toBe(nowYear);
29+
expect(month).toBe(nowMonth);
30+
expect(monthDay).toBe(1 + index);
31+
expect(monthStatus).toBe('current');
32+
});
33+
nextCells.forEach(({ year, month, monthDay, monthStatus }, index) => {
34+
expect(year).toBe(nowYear);
35+
expect(month).toBe(nowMonth + 1);
36+
expect(monthDay).toBe(1 + index);
37+
expect(monthStatus).toBe('next');
38+
});
39+
});
40+
41+
it('should select the date cell correctly with single mode', () => {
42+
jest.useFakeTimers({ now });
43+
const { result } = renderHook(() => {
44+
const [value, setValue] = useState(now);
45+
return { ...useCalendarComponent({ value, onChange: setValue }), value };
46+
});
47+
const cells = result.current.getDateCellInfos();
48+
// 2022-10-01
49+
expect(cells[6]).toMatchObject({
50+
isToday: true,
51+
isSelected: true,
52+
isExcluded: false,
53+
});
54+
expect(result.current.value).toEqual(now);
55+
56+
// select the date at previous month
57+
act(() => {
58+
// 2022-09-25
59+
cells[0]?.selectThisDate();
60+
});
61+
expect(result.current.value).toEqual(new Date(2022, 8, 25));
62+
// select the date at next month
63+
act(() => {
64+
// 2022-11-05
65+
cells.at(-1)?.selectThisDate();
66+
});
67+
expect(result.current.value).toEqual(new Date(2022, 10, 5));
68+
});
69+
70+
it('should distinguish whether a date is excluded', () => {
71+
jest.useFakeTimers({ now });
72+
const excludedDates: ExcludedDates = {
73+
min: now,
74+
max: new Date(2022, 10, 1),
75+
arbitrary: [new Date(2022, 9, 15)],
76+
};
77+
const { result } = renderHook(
78+
excludedDates => useCalendarComponent({ excludedDates }),
79+
{
80+
initialProps: excludedDates,
81+
}
82+
);
83+
const cells = result.current.getDateCellInfos();
84+
const previousCells = cells.splice(0, 6);
85+
const currentCells = cells.splice(0, 31);
86+
const nextCells = cells;
87+
previousCells.forEach(({ isExcluded }) => {
88+
expect(isExcluded).toBe(true);
89+
});
90+
currentCells.forEach(({ monthDay, isExcluded }) => {
91+
if (monthDay === 15) expect(isExcluded).toBe(true);
92+
else expect(isExcluded).toBe(false);
93+
});
94+
nextCells.forEach(({ isExcluded }) => {
95+
expect(isExcluded).toBe(true);
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)