Skip to content

Commit 9aee31e

Browse files
committed
refactor: rename dayOfMonth/dayOfWeek to monthDay/weekDay
1 parent 64cb51b commit 9aee31e

6 files changed

Lines changed: 34 additions & 37 deletions

File tree

packages/react-use-calendar-component/src/app/components/CalendarCell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default function CalendarCell({
44
monthStatus,
55
isSelected,
66
isToday,
7-
dayOfMonth,
7+
monthDay: dayOfMonth,
88
selectThisDate,
99
}: DateCellInfo) {
1010
const title = isToday ? 'today' : undefined;

packages/react-use-calendar-component/src/lib/hooks/useCalendarComponent.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import useDisplayedDate from './useDisplayedDate';
1414
export default function useCalendarComponent<S extends SelectType = 'single'>({
1515
initialDisplayedDate = new Date(),
1616
selectType,
17-
value,
17+
value: userValue,
1818
onChange = noop,
1919
}: UseCalendarOptions<S> = {}) {
2020
const {
@@ -25,14 +25,14 @@ export default function useCalendarComponent<S extends SelectType = 'single'>({
2525
changeDisplayedYear,
2626
changeDisplayedMonth,
2727
} = useDisplayedDate(initialDisplayedDate);
28-
const [internalValue, setInternalValue] = useState(value);
28+
const [internalValue, setInternalValue] = useState(userValue);
2929
const selectedDates = (() => {
3030
if (selectType === 'multiple') {
31-
if (value) return value as Date[];
31+
if (userValue) return userValue as Date[];
3232
if (internalValue) return internalValue as Date[];
3333
}
3434

35-
if (value) return [value] as Date[];
35+
if (userValue) return [userValue] as Date[];
3636
if (internalValue) return [internalValue] as Date[];
3737

3838
return [];
@@ -51,7 +51,7 @@ export default function useCalendarComponent<S extends SelectType = 'single'>({
5151
const {
5252
year: dateYear,
5353
month: dateMonth,
54-
dayOfMonth,
54+
monthDay,
5555
...info
5656
} = getDateInfoByIndex(year, month, cellIndex);
5757
const selectThisDate = (options: SelectDateOptions = {}) => {
@@ -60,7 +60,7 @@ export default function useCalendarComponent<S extends SelectType = 'single'>({
6060
setDisplayedYear(dateYear);
6161
setDisplayedMonth(dateMonth);
6262
}
63-
const selectedDate = new Date(dateYear, dateMonth, dayOfMonth);
63+
const selectedDate = new Date(dateYear, dateMonth, monthDay);
6464
if (selectType === 'multiple') {
6565
const newDates = selectedDates.filter(
6666
date => !isSameDate(date, selectedDate)
@@ -77,12 +77,12 @@ export default function useCalendarComponent<S extends SelectType = 'single'>({
7777
};
7878

7979
return {
80-
key: `${dateYear}-${dateMonth}-${dayOfMonth}`,
80+
key: `${dateYear}-${dateMonth}-${monthDay}`,
8181
year: dateYear,
8282
month: dateMonth,
83-
dayOfMonth: dayOfMonth,
83+
monthDay,
8484
isSelected: !!selectedDates.find(date =>
85-
isSameDate(date, new Date(dateYear, dateMonth, dayOfMonth))
85+
isSameDate(date, new Date(dateYear, dateMonth, monthDay))
8686
),
8787
selectThisDate,
8888
...info,

packages/react-use-calendar-component/src/lib/types.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,15 @@ export type UseCalendarOptions<S extends SelectType = 'single'> = {
1717
export interface DateInfo {
1818
year: number;
1919
month: number;
20-
dayOfMonth: number;
21-
dayOfWeek: number;
20+
monthDay: number;
21+
weekDay: number;
2222
isToday: boolean;
2323
monthStatus: 'current' | 'next' | 'previous';
2424
}
2525

26-
export interface DateCellInfo {
26+
export interface DateCellInfo extends DateInfo {
2727
key: string;
28-
year: number;
29-
month: number;
30-
dayOfMonth: number;
31-
dayOfWeek: number;
32-
isToday: boolean;
3328
isSelected: boolean;
34-
monthStatus: 'current' | 'next' | 'previous';
3529
selectThisDate: (options?: SelectDateOptions) => void;
3630
}
3731

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export function getDateInfoByIndex(
5555
return {
5656
year: dateYear,
5757
month: dateMonth,
58-
dayOfWeek: index % 7,
59-
dayOfMonth,
58+
weekDay: index % 7,
59+
monthDay: dayOfMonth,
6060
monthStatus,
6161
isToday: isToday(dateYear, dateMonth, dayOfMonth),
6262
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './getNumberOfDays';
22
export * from './isToday';
33
export * from './locale';
4-
export * from './getDateInfo';
4+
export * from './getDateInfoByIndex';
55
export * from './noop';
66
export * from './isSameDate';

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ describe('getDateInfoByIndex', () => {
1111

1212
it('should determine the date is at the previous month', () => {
1313
previousIndexes.forEach((value, index) => {
14-
const { year, month, dayOfMonth, monthStatus } = getDateInfoByIndex(
15-
testYear,
16-
testMonth,
17-
value
18-
);
14+
const {
15+
year,
16+
month,
17+
monthDay: dayOfMonth,
18+
monthStatus,
19+
} = getDateInfoByIndex(testYear, testMonth, value);
1920
expect(year).toBe(testYear);
2021
expect(month).toBe(testMonth - 1);
2122
expect(dayOfMonth).toBe(25 + index);
@@ -24,11 +25,12 @@ describe('getDateInfoByIndex', () => {
2425
});
2526
it('should determine the date is at the current month', () => {
2627
currentIndexes.forEach((value, index) => {
27-
const { year, month, dayOfMonth, monthStatus } = getDateInfoByIndex(
28-
testYear,
29-
testMonth,
30-
value
31-
);
28+
const {
29+
year,
30+
month,
31+
monthDay: dayOfMonth,
32+
monthStatus,
33+
} = getDateInfoByIndex(testYear, testMonth, value);
3234
expect(year).toBe(testYear);
3335
expect(month).toBe(testMonth);
3436
expect(dayOfMonth).toBe(1 + index);
@@ -37,11 +39,12 @@ describe('getDateInfoByIndex', () => {
3739
});
3840
it('should determine the date is at the next month', () => {
3941
nextIndexes.forEach((value, index) => {
40-
const { year, month, dayOfMonth, monthStatus } = getDateInfoByIndex(
41-
testYear,
42-
testMonth,
43-
value
44-
);
42+
const {
43+
year,
44+
month,
45+
monthDay: dayOfMonth,
46+
monthStatus,
47+
} = getDateInfoByIndex(testYear, testMonth, value);
4548
expect(year).toBe(testYear);
4649
expect(month).toBe(testMonth + 1);
4750
expect(dayOfMonth).toBe(1 + index);

0 commit comments

Comments
 (0)