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
22 changes: 20 additions & 2 deletions src/components/calendar/CalendarCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ import { cn } from '@/lib/utils';

interface CalendarCellProps {
date: number;
isCurrentMonth?: boolean;
isToday?: boolean;
goals?: Goal[];
onClick?: (date: number, goals: Goal[], event: React.MouseEvent) => void;
className?: string;
}

const CalendarCell = ({ date, goals = [], onClick, className }: CalendarCellProps) => {
const CalendarCell = ({
date,
isCurrentMonth = true,
isToday = false,
goals = [],
onClick,
className,
}: CalendarCellProps) => {
const hasGoals = goals.length > 0;
const firstGoal = goals[0];
const goalColorName = firstGoal ? getGoalBackgroundColorClass(firstGoal.color) : null;
Expand All @@ -26,10 +35,19 @@ const CalendarCell = ({ date, goals = [], onClick, className }: CalendarCellProp
className={cn(
'rounded-4 flex h-40 w-full min-w-40 cursor-pointer flex-col items-center p-1 transition md:max-h-44 md:max-w-84 lg:h-40 lg:max-w-85 lg:min-w-71',
hasGoals && 'hover:bg-tertiary-01',
!isCurrentMonth && 'opacity-40',
className,
)}
>
<span className="text-body-m-16 text-text-03 h-20 self-center">{date}</span>
<span
className={cn(
'text-body-m-16 h-20 self-center',
isCurrentMonth ? 'text-text-03' : 'text-text-04',
isToday && 'text-body-b-16',
)}
>
{date}
</span>

{hasGoals && (
<div
Expand Down
35 changes: 31 additions & 4 deletions src/components/calendar/CalendarGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,45 @@ interface CalendarGridProps {
const CalendarGrid = forwardRef<HTMLDivElement, CalendarGridProps>(({ data, onCellClick }, ref) => {
const { firstDay, daysInMonth, groupedGoals } = useCalendarData(data);
const calendarCells = [];
const TOTAL_CELLS = 42;

// 이전 달의 날짜 계산
const today = new Date();
const [currentYear, currentMonth] = data.month.split('-').map(Number);

const prevMonth = new Date(currentYear, currentMonth - 1, 0);
const prevMonthDays = prevMonth.getDate();

// 빈 셀 채우기 (월 시작 전 빈 공간)
for (let i = 0; i < firstDay; i++) {
calendarCells.push(<div key={`empty-${i}`} />);
const date = prevMonthDays - firstDay + i + 1;
calendarCells.push(<CalendarCell key={`prev-${date}`} date={date} isCurrentMonth={false} />);
}

// 날짜 셀 채우기
for (let date = 1; date <= daysInMonth; date++) {
const goalsOfTheDay = groupedGoals[date] || [];
const isToday =
today.getDate() === date &&
today.getMonth() + 1 === currentMonth &&
today.getFullYear() === currentYear;

calendarCells.push(
<CalendarCell
key={date}
date={date}
goals={goalsOfTheDay}
onClick={onCellClick}
isCurrentMonth={true}
isToday={isToday}
/>,
);
}

// 다음 달의 날짜 계산
let nextMonthDate = 1;
const remainingCells = TOTAL_CELLS - (firstDay + daysInMonth);
for (let i = 0; i < remainingCells; i++) {
calendarCells.push(
<CalendarCell key={date} date={date} goals={goalsOfTheDay} onClick={onCellClick} />,
<CalendarCell key={`next-${nextMonthDate}`} date={nextMonthDate++} isCurrentMonth={false} />,
);
}

Expand Down