import DateTimePicker, { type DateTimePickerEvent, } from "@react-native-community/datetimepicker"; import { Ionicons } from "@expo/vector-icons"; import { useState } from "react"; import { Modal, Platform, Pressable, StyleSheet, Text, View, type StyleProp, type ViewStyle, } from "react-native"; import { fonts, radii } from "@/constants/theme"; import { useAppTheme } from "@/contexts/ThemeContext"; import { formatShortDate } from "@/lib/format"; type CompactDateFieldProps = { value: Date; onChange: (date: Date) => void; style?: StyleProp; maximumDate?: Date; minimumDate?: Date; }; export function CompactDateField({ value, onChange, style, maximumDate = new Date(2100, 0, 1), minimumDate, }: CompactDateFieldProps) { const { colors, isDark } = useAppTheme(); const [open, setOpen] = useState(false); const [draft, setDraft] = useState(value); function applyDate(next: Date) { const clamped = next.getTime() > maximumDate.getTime() ? maximumDate : minimumDate && next.getTime() < minimumDate.getTime() ? minimumDate : next; onChange(clamped); } function handleChange(event: DateTimePickerEvent, selected?: Date) { if (Platform.OS === "android") { setOpen(false); if (event.type === "set" && selected) applyDate(selected); return; } if (selected) setDraft(selected); } return ( <> { setDraft(value); setOpen(true); }} style={({ pressed }) => [ styles.trigger, { borderColor: colors.border, backgroundColor: colors.cardGlass, }, pressed && styles.pressed, style, ]} > {formatShortDate(value)} {Platform.OS === "ios" ? ( setOpen(false)}> setOpen(false)}> event.stopPropagation()} > setOpen(false)}> Cancel Date { applyDate(draft); setOpen(false); }} > Done ) : open ? ( ) : null} ); } const styles = StyleSheet.create({ trigger: { minHeight: 36, borderWidth: 1, borderRadius: radii.md, flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: 8, gap: 2, }, pressed: { opacity: 0.9, }, value: { flex: 1, fontFamily: fonts.body, fontSize: 12, }, backdrop: { flex: 1, justifyContent: "flex-end", backgroundColor: "rgba(0,0,0,0.45)", }, sheet: { borderTopLeftRadius: radii.lg, borderTopRightRadius: radii.lg, }, sheetHeader: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: 16, paddingVertical: 12, borderBottomWidth: 1, }, sheetTitle: { fontFamily: fonts.bodySemiBold, fontSize: 15, }, action: { fontFamily: fonts.bodyMedium, fontSize: 15, }, });