|
| 1 | +import * as React from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + Body1, |
| 5 | + FluentProvider, |
| 6 | + IdPrefixProvider, |
| 7 | + webLightTheme, |
| 8 | +} from '@fluentui/react-components'; |
| 9 | +import { addDays, startOfMonth, startOfWeek } from 'date-fns'; |
| 10 | +import { useEffect, useRef, useState } from 'react'; |
| 11 | + |
| 12 | +import { Day } from './Day'; |
| 13 | +import DayView from './DayView'; |
| 14 | +import { ECalendarViews } from './models/ECalendarViews'; |
| 15 | +import { ICalendarControlProps } from './models/ICalendarControlProps'; |
| 16 | +import { ICalendarDay } from './models/ICalendarDay'; |
| 17 | +import { IEvent } from './models/IEvents'; |
| 18 | +import { Stack } from '@nuvemerudita/react-controls'; |
| 19 | +import Toolbar from './Toolbar'; |
| 20 | +import WeekView from './WeekView'; |
| 21 | +import strings from 'ControlStrings'; |
| 22 | +import { useCalendar } from './hooks/useCalendar'; |
| 23 | +import { useCalendarStyles } from './hooks/useCalendarStyles'; |
| 24 | + |
| 25 | +interface CalendarState { |
| 26 | + currentDate: Date; |
| 27 | +} |
| 28 | + |
| 29 | +const daysOfWeek: string[] = [ |
| 30 | + strings.CalendarControlDayOfWeekSunday, |
| 31 | + strings.CalendarControlDayOfWeekMonday, |
| 32 | + strings.CalendarControlDayOfWeekTuesday, |
| 33 | + strings.CalendarControlDayOfWeekWednesday, |
| 34 | + strings.CalendarControlDayOfWeekThursday, |
| 35 | + strings.CalendarControlDayOfWeekFriday, |
| 36 | + strings.CalendarControlDayOfWeekSaturday, |
| 37 | +]; |
| 38 | + |
| 39 | +export const Calendar: React.FC<ICalendarControlProps> = ( |
| 40 | + props: ICalendarControlProps |
| 41 | +) => { |
| 42 | + const { styles } = useCalendarStyles(props as never); |
| 43 | + const calendarRef = useRef<HTMLDivElement>(null); |
| 44 | + const [rowHeight, setRowHeight] = useState<number | null>(null); |
| 45 | + const { |
| 46 | + events, |
| 47 | + height = 800, |
| 48 | + theme, |
| 49 | + onDayChange, |
| 50 | + onMonthChange, |
| 51 | + onWeekChange, |
| 52 | + onViewChange, |
| 53 | + } = props; |
| 54 | + const rowHeightRef = useRef<number>(0); |
| 55 | + |
| 56 | + // Default current date |
| 57 | + const [currentDate, setCurrentDate] = useState<CalendarState['currentDate']>( |
| 58 | + new Date() |
| 59 | + ); |
| 60 | + // Default view |
| 61 | + const [selectedView, setSelectedView] = useState<ECalendarViews>( |
| 62 | + ECalendarViews.Month |
| 63 | + ); |
| 64 | + // Get month calendar |
| 65 | + const { getMonthCalendar } = useCalendar( |
| 66 | + events, |
| 67 | + Intl.DateTimeFormat().resolvedOptions().timeZone |
| 68 | + ); |
| 69 | + // Get calendar days |
| 70 | + const getCalendarDays = React.useCallback((date: Date): ICalendarDay[] => { |
| 71 | + const month = date.getMonth(); |
| 72 | + const firstDayOfMonth = startOfMonth(date); |
| 73 | + const firstDayOfWeek = startOfWeek(firstDayOfMonth); |
| 74 | + const totalDisplayedDays = 42; |
| 75 | + // Generate calendar days |
| 76 | + const calendarDays = Array.from( |
| 77 | + { length: totalDisplayedDays }, |
| 78 | + (_, index): ICalendarDay => { |
| 79 | + const relativeDay = addDays(firstDayOfWeek, index); |
| 80 | + return { |
| 81 | + day: relativeDay.getDate(), |
| 82 | + currentMonth: relativeDay.getMonth() === month, |
| 83 | + date: relativeDay, |
| 84 | + }; |
| 85 | + } |
| 86 | + ); |
| 87 | + return calendarDays; |
| 88 | + }, []); |
| 89 | + |
| 90 | + // Handle month change |
| 91 | + const handleMonthChange = React.useCallback( |
| 92 | + (date: Date): void => { |
| 93 | + if (onMonthChange) { |
| 94 | + onMonthChange(date); |
| 95 | + } |
| 96 | + setCurrentDate(date); |
| 97 | + }, |
| 98 | + [currentDate] |
| 99 | + ); |
| 100 | + // Handle day change |
| 101 | + const handleDayChange = React.useCallback( |
| 102 | + (date: Date): void => { |
| 103 | + if (onDayChange) { |
| 104 | + onDayChange(date); |
| 105 | + } |
| 106 | + setCurrentDate(date); |
| 107 | + }, |
| 108 | + [currentDate] |
| 109 | + ); |
| 110 | + // Handle week change |
| 111 | + const handleWeekChange = React.useCallback( |
| 112 | + (date: Date): void => { |
| 113 | + if (onWeekChange) { |
| 114 | + onWeekChange(date); |
| 115 | + } |
| 116 | + setCurrentDate(date); |
| 117 | + }, |
| 118 | + [currentDate] |
| 119 | + ); |
| 120 | + // Handle view change |
| 121 | + const handleViewChange = React.useCallback( |
| 122 | + (view: ECalendarViews): void => { |
| 123 | + if (onViewChange) { |
| 124 | + onViewChange(view); |
| 125 | + } |
| 126 | + setSelectedView(view); |
| 127 | + }, |
| 128 | + [selectedView] |
| 129 | + ); |
| 130 | + // Get events for the day |
| 131 | + const getEventsForDay = React.useCallback( |
| 132 | + (dateObj: ICalendarDay): IEvent[] => { |
| 133 | + const { date } = dateObj; |
| 134 | + const monthEvents = getMonthCalendar( |
| 135 | + currentDate.getFullYear(), |
| 136 | + currentDate.getMonth() |
| 137 | + ); |
| 138 | + const dayString = date.toISOString().split('T')[0]; // Get date in YYYY-MM-DD format |
| 139 | + return monthEvents[dayString]?.flatMap((slot) => slot) || []; |
| 140 | + }, |
| 141 | + [currentDate] |
| 142 | + ); |
| 143 | + // Resize observer |
| 144 | + useEffect(() => { |
| 145 | + if (calendarRef.current) { |
| 146 | + const firstDataColumnCell = calendarRef.current.querySelector( |
| 147 | + `.${styles.calendarWrapper} > div:nth-child(8)` |
| 148 | + ) as HTMLDivElement; |
| 149 | + |
| 150 | + if (firstDataColumnCell) { |
| 151 | + setRowHeight(firstDataColumnCell.offsetHeight); |
| 152 | + rowHeightRef.current = firstDataColumnCell.offsetHeight; |
| 153 | + } |
| 154 | + } |
| 155 | + }, [calendarRef.current]); |
| 156 | + // Toolbar component |
| 157 | + |
| 158 | + // Render Month View |
| 159 | + const RenderMonthView = React.useCallback(() => { |
| 160 | + return ( |
| 161 | + <Stack |
| 162 | + height={height} |
| 163 | + width={'100%'} |
| 164 | + verticalAlign="start" |
| 165 | + horizontalAlign="center" |
| 166 | + > |
| 167 | + <div className={styles.calendarWrapper} ref={calendarRef}> |
| 168 | + {daysOfWeek.map((day) => ( |
| 169 | + <Body1 key={day} className={styles.dayHeader}> |
| 170 | + {day} |
| 171 | + </Body1> |
| 172 | + ))} |
| 173 | + {getCalendarDays(currentDate).map((dateObj, index) => { |
| 174 | + const { day, currentMonth } = dateObj; |
| 175 | + const events = currentMonth ? getEventsForDay(dateObj) : []; |
| 176 | + return ( |
| 177 | + <Day |
| 178 | + key={index} |
| 179 | + day={day} |
| 180 | + date={dateObj.date} |
| 181 | + currentMonth={dateObj.currentMonth} |
| 182 | + events={events} |
| 183 | + columnHeight={rowHeight || 0} |
| 184 | + /> |
| 185 | + ); |
| 186 | + })} |
| 187 | + </div> |
| 188 | + </Stack> |
| 189 | + ); |
| 190 | + }, [currentDate, getCalendarDays, getEventsForDay, rowHeight, styles]); |
| 191 | + // Render Week View |
| 192 | + const RenderWeekView = React.memo(() => { |
| 193 | + return ( |
| 194 | + <> |
| 195 | + {selectedView === ECalendarViews.Week && ( |
| 196 | + <WeekView events={events} currentDay={currentDate} height={height} /> |
| 197 | + )} |
| 198 | + </> |
| 199 | + ); |
| 200 | + }); |
| 201 | + // Render Day View |
| 202 | + const RenderDayView = React.memo(() => { |
| 203 | + return ( |
| 204 | + <> |
| 205 | + {selectedView === ECalendarViews.Day && ( |
| 206 | + <DayView currentDay={currentDate} events={events} height={height} /> |
| 207 | + )} |
| 208 | + </> |
| 209 | + ); |
| 210 | + }); |
| 211 | + |
| 212 | + // Render content |
| 213 | + const RenderContent = React.useCallback(() => { |
| 214 | + switch (selectedView) { |
| 215 | + case ECalendarViews.Month: |
| 216 | + return <RenderMonthView />; |
| 217 | + case ECalendarViews.Week: |
| 218 | + return <RenderWeekView />; |
| 219 | + case ECalendarViews.Day: |
| 220 | + return <RenderDayView />; |
| 221 | + default: |
| 222 | + return <RenderMonthView />; |
| 223 | + } |
| 224 | + }, [selectedView, RenderMonthView, RenderWeekView, RenderDayView]); |
| 225 | + |
| 226 | + return ( |
| 227 | + <> |
| 228 | + <IdPrefixProvider value="calendarControl-"> |
| 229 | + <FluentProvider theme={theme ?? webLightTheme}> |
| 230 | + <Stack height={'100%'} verticalAlign="start"> |
| 231 | + <Toolbar |
| 232 | + selectedView={selectedView} |
| 233 | + onSelectedView={handleViewChange} |
| 234 | + currentDate={currentDate} |
| 235 | + setCurrentDate={setCurrentDate} |
| 236 | + onWeekChange={handleWeekChange} |
| 237 | + onMonthChange={handleMonthChange} |
| 238 | + onDayChange={handleDayChange} |
| 239 | + /> |
| 240 | + <RenderContent /> |
| 241 | + </Stack> |
| 242 | + </FluentProvider> |
| 243 | + </IdPrefixProvider> |
| 244 | + </> |
| 245 | + ); |
| 246 | +}; |
| 247 | + |
| 248 | +export default Calendar; |
0 commit comments