import { isToday, format, getDaysInMonth, startOfMonth, getDay, } from "date-fns"; import { zhTW } from "date-fns/locale"; import DayCell from "../DayCell"; import { eventColors, typeColors, DateColorsConfig } from "../../Const/colors"; import style from "./style.module.scss"; type Prop = { monthDate: Date; dateColorsConfig: DateColorsConfig[]; isSimple?: boolean; // 是否為簡單模式(只顯示icon,不顯示文字) onClick: (date: Date) => void; }; function MonthView({ monthDate, dateColorsConfig, isSimple, onClick }: Prop) { const daysInMonth = getDaysInMonth(monthDate); const firstDayOfMonth = getDay(startOfMonth(monthDate)); const days = Array.from({ length: daysInMonth }, (_, idx) => idx + 1); const month = monthDate.getMonth(); const year = monthDate.getFullYear(); const getColorsForDate = (date: Date) => { const key = format(date, "yyyy-MM-dd"); const config = dateColorsConfig.find((item) => item.date === key) || { events: [], types: [], stringEvent: {}, }; const dashColors = config.events ?.map((eventCode) => eventColors[eventCode]) .filter(Boolean); const dotColors = config.types ?.map((typeCode) => typeColors[typeCode]) .filter(Boolean); const stringEvent = config.stringEvent; // 取得該日期事件文字 return { dashColors, dotColors, stringEvent }; }; const getLastRowCellsCount = () => { const lastRowDayCount = daysInMonth - (7 - firstDayOfMonth + (Math.ceil((daysInMonth - (7 - firstDayOfMonth)) / 7) - 1) * 7); return 7 - lastRowDayCount; }; return (
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
|---|---|---|---|---|---|---|
| ))} {days.slice(0, 7 - firstDayOfMonth).map((day) => { const date = new Date(year, month, day); const { dashColors, dotColors, stringEvent } = getColorsForDate(date); return ( | ||||||
| */} {weekIdx === Math.ceil((daysInMonth - (7 - firstDayOfMonth)) / 7) - 1 && Array(getLastRowCellsCount()) .fill(null) .map((_, idx) => | )} |