Skip to content
Merged
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ $
!.yarn/releases
!.yarn/sdks
!.yarn/versions
*storybook.log
*storybook.log

# local planning docs
plan/
12 changes: 5 additions & 7 deletions src/components/commons/datePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import Calendar from "react-calendar";
import "react-calendar/dist/Calendar.css";
import "./calendar.css";
import { isSelectableCalendarDate } from "./datePicker.utils";

interface DatePickerProps {
date: Date | undefined;
onChangeDate: (date: Date) => void;
}

const DatePicker = ({ date, onChangeDate }: DatePickerProps) => {
const isPastDate = (date) => {
const today = new Date();
today.setHours(0, 0, 0, 0);

return date < today;
};
const isDisabledDate = (date: Date) => !isSelectableCalendarDate(date);

return (
<Calendar
Expand All @@ -22,7 +19,8 @@ const DatePicker = ({ date, onChangeDate }: DatePickerProps) => {
locale="ko-KR"
className="custom-calendar"
formatDay={(_, date) => date.getDate().toString()}
tileClassName={({ date }) => (isPastDate(date) ? "past-date" : "")}
tileDisabled={({ date }) => isDisabledDate(date)}
tileClassName={({ date }) => (isDisabledDate(date) ? "past-date" : "")}
/>
);
};
Expand Down
8 changes: 8 additions & 0 deletions src/components/commons/datePicker/calendar.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,11 @@

pointer-events: none;
}

/* 비활성(오늘/과거) 타일: 커서/배경 명시 */
.custom-calendar .react-calendar__tile:disabled {
color: #939393;

background-color: transparent;
cursor: not-allowed;
}
11 changes: 11 additions & 0 deletions src/components/commons/datePicker/datePicker.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import dayjs from "dayjs";

/**
* 캘린더에서 선택 가능한 날짜인지 판정한다.
* 오늘과 과거 날짜는 선택 불가, 내일(내일 00:00) 이상만 선택 가능.
*/
export const isSelectableCalendarDate = (date: Date): boolean => {
const startOfTomorrow = dayjs().add(1, "day").startOf("day");

return !dayjs(date).isBefore(startOfTomorrow);
};
47 changes: 47 additions & 0 deletions test/unit/components/DatePicker.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { render } from "@testing-library/react";

// jsdom CSS 파서가 react-calendar 스타일시트를 파싱하다 깨지므로 CSS import를 무력화한다.
vi.mock("react-calendar/dist/Calendar.css", () => ({}));
vi.mock("../../../src/components/commons/datePicker/calendar.css", () => ({}));

import DatePicker from "../../../src/components/commons/datePicker/DatePicker";

describe("DatePicker", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 7, 31, 14, 30, 0)); // 2026-08-31
});

afterEach(() => {
vi.useRealTimers();
});

it("오늘 날짜 타일은 비활성(disabled) 상태로 렌더된다", () => {
const { container } = render(<DatePicker date={undefined} onChangeDate={() => {}} />);

const todayTile = container.querySelector(".react-calendar__tile--now");
expect(todayTile).not.toBeNull();
expect(todayTile).toBeDisabled();
});

it("과거 날짜 타일은 비활성(disabled) 상태로 렌더된다", () => {
const { container } = render(<DatePicker date={undefined} onChangeDate={() => {}} />);

// 이번 달 첫날(8/1)은 오늘(8/31) 이전이므로 반드시 비활성
const firstDayTile = container.querySelector(
".react-calendar__month-view__days .react-calendar__tile:not(.react-calendar__month-view__days__day--neighboringMonth)"
);
expect(firstDayTile).not.toBeNull();
expect(firstDayTile).toBeDisabled();
});

it("과거/오늘 비활성 타일이 최소 1개 이상 존재한다", () => {
const { container } = render(<DatePicker date={undefined} onChangeDate={() => {}} />);

const disabledTiles = container.querySelectorAll(
".react-calendar__month-view__days .react-calendar__tile[disabled]"
);
expect(disabledTiles.length).toBeGreaterThan(0);
});
});
39 changes: 39 additions & 0 deletions test/unit/components/datePicker.utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { isSelectableCalendarDate } from "../../../src/components/commons/datePicker/datePicker.utils";

describe("isSelectableCalendarDate", () => {
beforeEach(() => {
// 2026-08-31 14:30 로 시스템 시간 고정
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 7, 31, 14, 30, 0));
});

afterEach(() => {
vi.useRealTimers();
});

it("오늘 날짜는 선택할 수 없다 (자정으로 표현된 오늘)", () => {
const today = new Date(2026, 7, 31, 0, 0, 0);
expect(isSelectableCalendarDate(today)).toBe(false);
});

it("오늘 날짜는 시각과 무관하게 선택할 수 없다", () => {
const todayLater = new Date(2026, 7, 31, 23, 59, 59);
expect(isSelectableCalendarDate(todayLater)).toBe(false);
});

it("과거 날짜는 선택할 수 없다", () => {
const yesterday = new Date(2026, 7, 30, 0, 0, 0);
expect(isSelectableCalendarDate(yesterday)).toBe(false);
});

it("내일 날짜는 선택할 수 있다", () => {
const tomorrow = new Date(2026, 8, 1, 0, 0, 0);
expect(isSelectableCalendarDate(tomorrow)).toBe(true);
});

it("먼 미래 날짜는 선택할 수 있다", () => {
const future = new Date(2026, 11, 25, 0, 0, 0);
expect(isSelectableCalendarDate(future)).toBe(true);
});
});
Loading