Skip to content

Commit 45d6a69

Browse files
committed
feat: 캘린더 메인 - 일정클릭시 해당날짜의 일정리스트 노출
1 parent b4db6bb commit 45d6a69

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ function App() {
2626
</svg>
2727
</label>
2828
</div>
29-
<div className="flex-1">
30-
<h1 className="flex-end min-w-40 rounded bg-base-200 p-2 text-center text-sm">개인 일정 캘린더</h1>
29+
<div className="flex-1 justify-end">
30+
<h1 className="min-w-40 rounded bg-base-200 p-2 text-center text-sm">개인 일정 캘린더</h1>
3131
</div>
3232
</div>
3333
</div>

src/components/common/Calendar.tsx

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import FullCalendar from '@fullcalendar/react';
2+
import { EventClickArg } from '@fullcalendar/core';
23
import dayGridPlugin from '@fullcalendar/daygrid';
34
import interactionPlugin from '@fullcalendar/interaction';
45
import { useRef, useState, useEffect } from 'react';
56

7+
type Event = {
8+
title: string;
9+
start: Date | string;
10+
}
11+
interface EventCardsProps {
12+
events: Event[];
13+
date: Date | string | null;
14+
}
15+
616
const events = [
717
{ title: 'Meeting', start: new Date() },
818
{ title: 'Meeting', start: '2024-05-08' },
@@ -13,10 +23,28 @@ const events = [
1323
export function Calendar() {
1424
const [calendarHeight, setCalendarHeight] = useState<string | number>('auto');
1525
const calendarRef = useRef<FullCalendar | null>(null);
26+
const [selectedEvents, setSelectedEvents] = useState<Event[]>([]);
27+
const [selectedDate, setSelectedDate] = useState<Date | null>(null);
28+
29+
const handleDateClick = (clickInfo: EventClickArg) => {
30+
if(clickInfo.event.start) {
31+
const clickStartDate = new Date(clickInfo.event.start);
32+
setSelectedDate(clickStartDate);
33+
34+
const clickedStartDate = new Date(clickInfo.event.start).toDateString();
35+
setSelectedEvents(events.filter(event =>
36+
new Date(event.start).toDateString() === clickedStartDate
37+
));
38+
} else {
39+
console.log("not available");
40+
}
41+
}
42+
1643
const handlePrev = () => {
1744
const calendarApi = calendarRef?.current?.getApi();
1845
if (calendarApi) {
1946
calendarApi.prev();
47+
setSelectedDate(null);
2048
} else {
2149
console.error('Calendar API is not available.');
2250
}
@@ -26,6 +54,7 @@ export function Calendar() {
2654
const calendarApi = calendarRef?.current?.getApi();
2755
if (calendarApi) {
2856
calendarApi.next();
57+
setSelectedDate(null);
2958
} else {
3059
console.error('Calendar API is not available.');
3160
}
@@ -83,8 +112,9 @@ export function Calendar() {
83112
plugins={[dayGridPlugin, interactionPlugin]}
84113
initialView="dayGridMonth"
85114
events={events}
115+
eventClick={handleDateClick}
86116
dayMaxEvents={2} //Max개수까지보이고 나머지는 more
87-
navLinks={true} // 날짜/주 이름을 클릭하여 뷰를 변경할 수 있습니다.
117+
//navLinks={true} // 날짜/주 이름을 클릭하여 뷰를 변경할 수 있습니다.
88118
editable={true} // 이벤트를 수정할 수 있습니다.
89119
eventContent={renderEventContent}
90120
contentHeight={calendarHeight}
@@ -106,14 +136,11 @@ export function Calendar() {
106136
right: 'nextButton',
107137
}}
108138
customButtons={{
109-
prevButton: {
110-
click: handlePrev,
111-
},
112-
nextButton: {
113-
click: handleNext,
114-
},
139+
prevButton: { click: handlePrev },
140+
nextButton: { click: handleNext },
115141
}}
116142
/>
143+
{selectedDate && <EventCards events={selectedEvents} date={selectedDate} />}
117144
</div>
118145
);
119146
}
@@ -135,3 +162,33 @@ function renderEventContent(eventInfo: EventInfo) {
135162
</>
136163
);
137164
}
165+
166+
167+
function EventCards({ events, date }: EventCardsProps) {
168+
if (!date) {
169+
return <div>No date provided</div>; // date가 null인 경우 처리
170+
}
171+
172+
const formattedDate = new Date(date).toLocaleDateString('ko-KR', {
173+
year: 'numeric',
174+
month: '2-digit',
175+
day: '2-digit'
176+
}).replace(/\. /g,".").slice(0, -1);
177+
console.log(formattedDate)
178+
179+
return (
180+
<div>
181+
<h2>{formattedDate}</h2>
182+
<div className="flex overflow-x-auto">
183+
{events.map((event, index) => (
184+
<div key={index} className="min-w-[200px] m-2 p-2 bg-blue-200 text-white">
185+
<h3>{event.title}</h3>
186+
<p>{new Date(event.start).toLocaleTimeString()}</p>
187+
</div>
188+
))}
189+
</div>
190+
191+
</div>
192+
193+
);
194+
}

src/styles/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
@tailwind components;
33
@tailwind utilities;
44

5-
65
.userInvite {
76
display: flex;
87
justify-content: center;
@@ -68,3 +67,4 @@
6867
/* 캘린더 - 일정컬러 */
6968
.fc .fc-h-event {
7069
border: var(--fc-event-text-color);
70+
}

0 commit comments

Comments
 (0)