Skip to content

Commit 2378e84

Browse files
committed
fix: deselect date with undefined options.selectedDate
1 parent 44033f1 commit 2378e84

File tree

10 files changed

+322
-277
lines changed

10 files changed

+322
-277
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@
5151
},
5252
"devDependencies": {
5353
"@testing-library/dom": "9.3.1",
54-
"@testing-library/jest-dom": "6.1.2",
54+
"@testing-library/jest-dom": "6.1.3",
5555
"@testing-library/react": "14.0.0",
5656
"@testing-library/user-event": "14.4.3",
5757
"@tuplo/shell": "1.2.0",
5858
"@types/jest": "29.5.4",
59-
"@typescript-eslint/parser": "6.6.0",
59+
"@typescript-eslint/parser": "6.7.0",
6060
"@vitejs/plugin-react": "4.0.4",
61-
"@vitest/coverage-v8": "0.34.3",
61+
"@vitest/coverage-v8": "0.34.4",
6262
"esbuild": "0.19.2",
63-
"eslint": "8.48.0",
63+
"eslint": "8.49.0",
6464
"eslint-config-airbnb-base": "15.0.0",
6565
"eslint-config-prettier": "9.0.0",
6666
"eslint-config-react-app": "7.0.1",
@@ -71,9 +71,9 @@
7171
"prettier": "3.0.3",
7272
"react": "18.2.0",
7373
"react-dom": "18.2.0",
74-
"tsx": "3.12.8",
74+
"tsx": "3.12.10",
7575
"typescript": "5.2.2",
7676
"vite": "4.4.9",
77-
"vitest": "0.34.3"
77+
"vitest": "0.34.4"
7878
}
7979
}

src/helpers.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
isValidDate,
1212
padAdjacentMonthDays,
1313
} from "./helpers";
14-
import type { IDay } from "./use-calendar.d";
14+
import { type IDay } from "./use-calendar.d";
1515

1616
describe("use-calendar helpers", () => {
1717
const dateNowSpy = vi

src/helpers.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as df from "./date-fns";
2-
import type {
3-
IDay,
4-
IEvent,
5-
IMonth,
6-
IUseCalendarOptions,
7-
IWeek,
2+
import {
3+
type IDay,
4+
type IEvent,
5+
type IMonth,
6+
type IUseCalendarOptions,
7+
type IWeek,
88
} from "./use-calendar.d";
99

1010
export function isValidDate(d: string | number | Date | undefined) {
@@ -41,7 +41,7 @@ export function getDayEvents(args: IGetDayEventsArgs) {
4141
);
4242
}
4343

44-
interface INewDayArgs {
44+
interface IGetNewDayArgs {
4545
availableDates?: Date[];
4646
date: Date;
4747
events?: IEvent[];
@@ -51,7 +51,7 @@ interface INewDayArgs {
5151
isAdjacentMonth?: boolean;
5252
}
5353

54-
export function getNewDay(args: INewDayArgs): IDay {
54+
export function getNewDay(args: IGetNewDayArgs): IDay {
5555
const {
5656
availableDates,
5757
date,
@@ -145,7 +145,7 @@ export function padAdjacentMonthDays(args: IPadAdjacentMonthDaysArgs) {
145145
return newWeek;
146146
}
147147

148-
interface IGetWeekArgs {
148+
interface IGetWeeksArgs {
149149
availableDates?: Date[];
150150
events?: IEvent[];
151151
firstDayOfWeek: number;
@@ -156,7 +156,7 @@ interface IGetWeekArgs {
156156
year: number;
157157
}
158158

159-
export function getWeeks(args: IGetWeekArgs): IWeek[] {
159+
export function getWeeks(args: IGetWeeksArgs): IWeek[] {
160160
const {
161161
availableDates,
162162
events,

src/index.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
import { act, renderHook } from "@testing-library/react";
33
import { vi } from "vitest";
44

5-
import { useCalendar } from "./index";
6-
import type { IDay, IEvent, IGetDayPropsReturns } from "./use-calendar.d";
5+
import {
6+
useCalendar,
7+
type IEvent,
8+
type IDay,
9+
type IGetDayPropsReturns,
10+
} from "./index";
711

812
describe("use-calendar", () => {
913
const dateNowSpy = vi

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useMemo, useState } from "react";
1+
import { useEffect, useMemo, useState } from "react";
22

33
import * as df from "./date-fns";
44
import {
@@ -12,7 +12,10 @@ import {
1212
buildGetPrevNextMonthProps,
1313
buildGetPrevNextYearProps,
1414
} from "./props";
15-
import type { ICalendarProps, IUseCalendarOptions } from "./use-calendar.d";
15+
import {
16+
type ICalendarProps,
17+
type IUseCalendarOptions,
18+
} from "./use-calendar.d";
1619

1720
export type {
1821
ICalendarProps,
@@ -44,6 +47,15 @@ export function useCalendar(
4447
selected || new Date(Date.now())
4548
);
4649

50+
useEffect(
51+
() => {
52+
if (s?.getTime() === selected?.getTime()) return;
53+
setSelected(s);
54+
},
55+
// eslint-disable-next-line react-hooks/exhaustive-deps
56+
[s?.getTime()]
57+
);
58+
4759
let monthsToDisplay = options?.monthsToDisplay || 1;
4860
if (monthsToDisplay === Infinity) {
4961
const d1 = options?.minDate || df.getFirstDayOfMonth(visibleMonth);

src/props.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi } from "vitest";
22

33
import { buildGetPrevNextMonthProps, buildGetDayProps } from "./props";
4-
import type { IMonth } from "./use-calendar.d";
4+
import { type IMonth } from "./use-calendar.d";
55

66
const commonProps = {
77
role: "button",

src/props.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type {
2-
IDay,
3-
IGetPrevNextPropsReturns,
4-
IGetDayPropsOptions,
5-
IMonth,
1+
import {
2+
type IDay,
3+
type IGetPrevNextPropsReturns,
4+
type IGetDayPropsOptions,
5+
type IMonth,
66
} from "./use-calendar.d";
77
import * as df from "./date-fns";
88

src/react/index.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@ describe("useCalendar UI", () => {
5555
expect(onDateSelectedSpy).toHaveBeenCalledTimes(0);
5656
});
5757
});
58+
59+
describe("selectedDate", () => {
60+
it("selects a date by default", async () => {
61+
const selectedDate = new Date("2022-07-12");
62+
render(<Calendar selectedDate={selectedDate} />);
63+
64+
expect(screen.getByText("12")).toHaveAttribute("aria-selected", "true");
65+
});
66+
});
5867
});

src/react/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { IUseCalendarOptions } from "../index";
2-
import { useCalendar } from "../index";
1+
import { useCalendar, type IUseCalendarOptions } from "../index";
32

43
function Calendar(props?: Partial<IUseCalendarOptions>) {
54
const { months, getDayProps, getPrevMonthProps, getNextMonthProps } =

0 commit comments

Comments
 (0)