Merge branch 'main' of https://git.ambigrat.com/shenyan/physical-expo
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
import { ThemedText } from "@/components/themed-text";
|
import { ThemedText } from "@/components/themed-text";
|
||||||
|
import { IconSymbol } from "@/components/ui/icon-symbol";
|
||||||
import { Colors } from "@/constants/theme";
|
import { Colors } from "@/constants/theme";
|
||||||
import { useTheme } from "@/context/ThemeContext";
|
import { useTheme } from "@/context/ThemeContext";
|
||||||
import React, { useMemo, useState } from "react";
|
import React, { useMemo, useState } from "react";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
import { Modal, Pressable, StyleSheet, View } from "react-native";
|
import { Modal, Pressable, StyleSheet, View } from "react-native";
|
||||||
|
import { SafeAreaView } from "react-native-safe-area-context";
|
||||||
|
|
||||||
interface CalendarModalProps {
|
interface CalendarModalProps {
|
||||||
visible: boolean;
|
visible: boolean;
|
||||||
@@ -18,11 +21,24 @@ export function CalendarModal({
|
|||||||
onSelectDate,
|
onSelectDate,
|
||||||
}: CalendarModalProps) {
|
}: CalendarModalProps) {
|
||||||
const { theme } = useTheme();
|
const { theme } = useTheme();
|
||||||
|
const { i18n } = useTranslation();
|
||||||
const isDark = theme === "dark";
|
const isDark = theme === "dark";
|
||||||
const bg = isDark ? "#1C1C1E" : "#FFFFFF";
|
const bg = isDark ? "#1C1C1E" : "#FFFFFF";
|
||||||
|
const textColor = isDark ? "#FFFFFF" : "#000000";
|
||||||
|
const subTextColor = isDark ? "#8E8E93" : "#8E8E93";
|
||||||
|
|
||||||
const [currentMonth, setCurrentMonth] = useState(new Date(selectedDate));
|
const [currentMonth, setCurrentMonth] = useState(new Date(selectedDate));
|
||||||
|
|
||||||
|
const today = useMemo(() => new Date(), []);
|
||||||
|
const minDate = useMemo(
|
||||||
|
() => new Date(today.getFullYear() - 1, today.getMonth(), 1),
|
||||||
|
[today]
|
||||||
|
);
|
||||||
|
const maxDate = useMemo(
|
||||||
|
() => new Date(today.getFullYear() + 1, today.getMonth(), 1),
|
||||||
|
[today]
|
||||||
|
);
|
||||||
|
|
||||||
const daysInMonth = useMemo(() => {
|
const daysInMonth = useMemo(() => {
|
||||||
const year = currentMonth.getFullYear();
|
const year = currentMonth.getFullYear();
|
||||||
const month = currentMonth.getMonth();
|
const month = currentMonth.getMonth();
|
||||||
@@ -35,29 +51,49 @@ export function CalendarModal({
|
|||||||
return days;
|
return days;
|
||||||
}, [currentMonth]);
|
}, [currentMonth]);
|
||||||
|
|
||||||
const weekDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
const weekDays = i18n.language.startsWith("zh")
|
||||||
|
? ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]
|
||||||
|
: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||||
|
|
||||||
// Add empty slots for start of month
|
// Add empty slots for start of month
|
||||||
const startDay = daysInMonth[0]?.getDay() || 0;
|
const startDay = daysInMonth[0]?.getDay() || 0;
|
||||||
const blanks = Array(startDay).fill(null);
|
const blanks = Array(startDay).fill(null);
|
||||||
|
|
||||||
|
// Calculate padding cells to keep 6 rows (42 cells total)
|
||||||
|
const totalCells = 42;
|
||||||
|
const paddingBlanks = useMemo(() => {
|
||||||
|
const spaceLeft = totalCells - (blanks.length + daysInMonth.length);
|
||||||
|
return spaceLeft > 0 ? Array(spaceLeft).fill(null) : [];
|
||||||
|
}, [blanks.length, daysInMonth.length]);
|
||||||
|
|
||||||
const handleDayPress = (date: Date) => {
|
const handleDayPress = (date: Date) => {
|
||||||
onSelectDate(date);
|
onSelectDate(new Date(date));
|
||||||
onClose();
|
onClose();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const monthTitle = currentMonth.toLocaleString(i18n.language, {
|
||||||
|
month: "long",
|
||||||
|
year: "numeric",
|
||||||
|
});
|
||||||
|
|
||||||
|
const canGoBack = currentMonth > minDate;
|
||||||
|
const canGoForward = currentMonth < maxDate;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal
|
||||||
visible={visible}
|
visible={visible}
|
||||||
transparent
|
transparent
|
||||||
animationType="fade"
|
animationType="slide"
|
||||||
onRequestClose={onClose}
|
onRequestClose={onClose}
|
||||||
>
|
>
|
||||||
<Pressable style={styles.overlay} onPress={onClose}>
|
<View style={styles.overlay}>
|
||||||
<View style={[styles.calendarContainer, { backgroundColor: bg }]}>
|
<Pressable style={styles.dismissArea} onPress={onClose} />
|
||||||
{/* Header */}
|
<View style={[styles.sheet, { backgroundColor: bg }]}>
|
||||||
|
<SafeAreaView edges={["bottom"]}>
|
||||||
<View style={styles.header}>
|
<View style={styles.header}>
|
||||||
<Pressable
|
<Pressable
|
||||||
|
disabled={!canGoBack}
|
||||||
|
style={[styles.navBtn, !canGoBack && { opacity: 0.2 }]}
|
||||||
onPress={() =>
|
onPress={() =>
|
||||||
setCurrentMonth(
|
setCurrentMonth(
|
||||||
new Date(
|
new Date(
|
||||||
@@ -68,15 +104,17 @@ export function CalendarModal({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<ThemedText style={styles.navText}>{"<"}</ThemedText>
|
<IconSymbol name="chevron-back" size={24} color={textColor} />
|
||||||
</Pressable>
|
</Pressable>
|
||||||
<ThemedText type="subtitle">
|
|
||||||
{currentMonth.toLocaleString("default", {
|
<ThemedText type="subtitle" style={styles.titleText}>
|
||||||
month: "long",
|
{monthTitle}
|
||||||
year: "numeric",
|
|
||||||
})}
|
|
||||||
</ThemedText>
|
</ThemedText>
|
||||||
|
|
||||||
|
<View style={styles.headerRight}>
|
||||||
<Pressable
|
<Pressable
|
||||||
|
disabled={!canGoForward}
|
||||||
|
style={[styles.navBtn, !canGoForward && { opacity: 0.2 }]}
|
||||||
onPress={() =>
|
onPress={() =>
|
||||||
setCurrentMonth(
|
setCurrentMonth(
|
||||||
new Date(
|
new Date(
|
||||||
@@ -87,11 +125,18 @@ export function CalendarModal({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<ThemedText style={styles.navText}>{">"}</ThemedText>
|
<IconSymbol
|
||||||
|
name="chevron-forward"
|
||||||
|
size={24}
|
||||||
|
color={textColor}
|
||||||
|
/>
|
||||||
|
</Pressable>
|
||||||
|
<Pressable style={styles.closeBtn} onPress={onClose}>
|
||||||
|
<IconSymbol name="close" size={24} color={subTextColor} />
|
||||||
</Pressable>
|
</Pressable>
|
||||||
</View>
|
</View>
|
||||||
|
</View>
|
||||||
|
|
||||||
{/* Week Headers */}
|
|
||||||
<View style={styles.weekRow}>
|
<View style={styles.weekRow}>
|
||||||
{weekDays.map((day) => (
|
{weekDays.map((day) => (
|
||||||
<ThemedText key={day} style={styles.weekDayText}>
|
<ThemedText key={day} style={styles.weekDayText}>
|
||||||
@@ -100,7 +145,6 @@ export function CalendarModal({
|
|||||||
))}
|
))}
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
{/* Days Grid */}
|
|
||||||
<View style={styles.daysGrid}>
|
<View style={styles.daysGrid}>
|
||||||
{blanks.map((_, index) => (
|
{blanks.map((_, index) => (
|
||||||
<View key={`blank-${index}`} style={styles.dayCell} />
|
<View key={`blank-${index}`} style={styles.dayCell} />
|
||||||
@@ -110,6 +154,10 @@ export function CalendarModal({
|
|||||||
date.getDate() === selectedDate.getDate() &&
|
date.getDate() === selectedDate.getDate() &&
|
||||||
date.getMonth() === selectedDate.getMonth() &&
|
date.getMonth() === selectedDate.getMonth() &&
|
||||||
date.getFullYear() === selectedDate.getFullYear();
|
date.getFullYear() === selectedDate.getFullYear();
|
||||||
|
|
||||||
|
const isToday =
|
||||||
|
new Date().toDateString() === date.toDateString();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Pressable
|
<Pressable
|
||||||
key={date.toISOString()}
|
key={date.toISOString()}
|
||||||
@@ -117,14 +165,16 @@ export function CalendarModal({
|
|||||||
styles.dayCell,
|
styles.dayCell,
|
||||||
isSelected && {
|
isSelected && {
|
||||||
backgroundColor: Colors[theme].tint,
|
backgroundColor: Colors[theme].tint,
|
||||||
borderRadius: 20,
|
borderRadius: 12,
|
||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
onPress={() => handleDayPress(date)}
|
onPress={() => handleDayPress(date)}
|
||||||
>
|
>
|
||||||
<ThemedText
|
<ThemedText
|
||||||
style={[
|
style={[
|
||||||
|
styles.dayText,
|
||||||
isSelected && { color: "#fff", fontWeight: "bold" },
|
isSelected && { color: "#fff", fontWeight: "bold" },
|
||||||
|
!isSelected && isToday && { color: Colors[theme].tint },
|
||||||
]}
|
]}
|
||||||
>
|
>
|
||||||
{date.getDate()}
|
{date.getDate()}
|
||||||
@@ -132,13 +182,14 @@ export function CalendarModal({
|
|||||||
</Pressable>
|
</Pressable>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
{/* Add padding blanks to keep constant height (6 rows) */}
|
||||||
|
{paddingBlanks.map((_, index) => (
|
||||||
|
<View key={`pad-blank-${index}`} style={styles.dayCell} />
|
||||||
|
))}
|
||||||
|
</View>
|
||||||
|
</SafeAreaView>
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
<Pressable style={styles.closeBtn} onPress={onClose}>
|
|
||||||
<ThemedText>Close</ThemedText>
|
|
||||||
</Pressable>
|
|
||||||
</View>
|
</View>
|
||||||
</Pressable>
|
|
||||||
</Modal>
|
</Modal>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -146,49 +197,65 @@ export function CalendarModal({
|
|||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
overlay: {
|
overlay: {
|
||||||
flex: 1,
|
flex: 1,
|
||||||
backgroundColor: "rgba(0,0,0,0.5)",
|
backgroundColor: "rgba(0,0,0,0.4)",
|
||||||
justifyContent: "center",
|
justifyContent: "flex-end",
|
||||||
padding: 20,
|
|
||||||
},
|
},
|
||||||
calendarContainer: {
|
dismissArea: {
|
||||||
borderRadius: 16,
|
flex: 1,
|
||||||
padding: 20,
|
},
|
||||||
|
sheet: {
|
||||||
|
borderTopLeftRadius: 20,
|
||||||
|
borderTopRightRadius: 20,
|
||||||
|
paddingHorizontal: 16,
|
||||||
|
paddingTop: 12,
|
||||||
|
paddingBottom: 20,
|
||||||
},
|
},
|
||||||
header: {
|
header: {
|
||||||
flexDirection: "row",
|
flexDirection: "row",
|
||||||
justifyContent: "space-between",
|
justifyContent: "space-between",
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
marginBottom: 20,
|
marginBottom: 20,
|
||||||
|
height: 44,
|
||||||
},
|
},
|
||||||
navText: {
|
titleText: {
|
||||||
fontSize: 20,
|
fontSize: 18,
|
||||||
padding: 10,
|
fontWeight: "600",
|
||||||
|
},
|
||||||
|
headerRight: {
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "center",
|
||||||
|
gap: 12,
|
||||||
|
},
|
||||||
|
navBtn: {
|
||||||
|
padding: 8,
|
||||||
|
},
|
||||||
|
closeBtn: {
|
||||||
|
padding: 8,
|
||||||
},
|
},
|
||||||
weekRow: {
|
weekRow: {
|
||||||
flexDirection: "row",
|
flexDirection: "row",
|
||||||
justifyContent: "space-around",
|
justifyContent: "space-around",
|
||||||
marginBottom: 10,
|
marginBottom: 16,
|
||||||
},
|
},
|
||||||
weekDayText: {
|
weekDayText: {
|
||||||
opacity: 0.5,
|
color: "#8E8E93",
|
||||||
width: 30,
|
width: 44,
|
||||||
textAlign: "center",
|
textAlign: "center",
|
||||||
fontSize: 12,
|
fontSize: 14,
|
||||||
},
|
},
|
||||||
daysGrid: {
|
daysGrid: {
|
||||||
flexDirection: "row",
|
flexDirection: "row",
|
||||||
flexWrap: "wrap",
|
flexWrap: "wrap",
|
||||||
|
paddingBottom: 20,
|
||||||
},
|
},
|
||||||
dayCell: {
|
dayCell: {
|
||||||
width: "14.28%", // 100% / 7
|
width: "14.28%",
|
||||||
aspectRatio: 1,
|
aspectRatio: 1,
|
||||||
justifyContent: "center",
|
justifyContent: "center",
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
marginBottom: 5,
|
marginBottom: 4,
|
||||||
},
|
},
|
||||||
closeBtn: {
|
dayText: {
|
||||||
alignItems: "center",
|
fontSize: 16,
|
||||||
marginTop: 20,
|
|
||||||
padding: 10,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user