import { ThemedText } from "@/components/themed-text"; import { Colors } from "@/constants/theme"; import { useTheme } from "@/context/ThemeContext"; import React, { useMemo, useState } from "react"; import { Modal, Pressable, StyleSheet, View } from "react-native"; interface CalendarModalProps { visible: boolean; onClose: () => void; selectedDate: Date; onSelectDate: (date: Date) => void; } export function CalendarModal({ visible, onClose, selectedDate, onSelectDate, }: CalendarModalProps) { const { theme } = useTheme(); const isDark = theme === "dark"; const bg = isDark ? "#1C1C1E" : "#FFFFFF"; const [currentMonth, setCurrentMonth] = useState(new Date(selectedDate)); const daysInMonth = useMemo(() => { const year = currentMonth.getFullYear(); const month = currentMonth.getMonth(); const date = new Date(year, month, 1); const days = []; while (date.getMonth() === month) { days.push(new Date(date)); date.setDate(date.getDate() + 1); } return days; }, [currentMonth]); const weekDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; // Add empty slots for start of month const startDay = daysInMonth[0]?.getDay() || 0; const blanks = Array(startDay).fill(null); const handleDayPress = (date: Date) => { onSelectDate(date); onClose(); }; return ( {/* Header */} setCurrentMonth( new Date( currentMonth.getFullYear(), currentMonth.getMonth() - 1, 1 ) ) } > {"<"} {currentMonth.toLocaleString("default", { month: "long", year: "numeric", })} setCurrentMonth( new Date( currentMonth.getFullYear(), currentMonth.getMonth() + 1, 1 ) ) } > {">"} {/* Week Headers */} {weekDays.map((day) => ( {day} ))} {/* Days Grid */} {blanks.map((_, index) => ( ))} {daysInMonth.map((date) => { const isSelected = date.getDate() === selectedDate.getDate() && date.getMonth() === selectedDate.getMonth() && date.getFullYear() === selectedDate.getFullYear(); return ( handleDayPress(date)} > {date.getDate()} ); })} Close ); } const styles = StyleSheet.create({ overlay: { flex: 1, backgroundColor: "rgba(0,0,0,0.5)", justifyContent: "center", padding: 20, }, calendarContainer: { borderRadius: 16, padding: 20, }, header: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", marginBottom: 20, }, navText: { fontSize: 20, padding: 10, }, weekRow: { flexDirection: "row", justifyContent: "space-around", marginBottom: 10, }, weekDayText: { opacity: 0.5, width: 30, textAlign: "center", fontSize: 12, }, daysGrid: { flexDirection: "row", flexWrap: "wrap", }, dayCell: { width: "14.28%", // 100% / 7 aspectRatio: 1, justifyContent: "center", alignItems: "center", marginBottom: 5, }, closeBtn: { alignItems: "center", marginTop: 20, padding: 10, }, });