import { Ionicons } from "@expo/vector-icons"; import DateTimePicker, { type DateTimePickerEvent, } from "@react-native-community/datetimepicker"; import { useState } from "react"; import { Modal, Platform, Pressable, StyleSheet, Text, View } from "react-native"; import { fonts, radii, spacing } from "@/constants/theme"; import { useAppTheme } from "@/contexts/ThemeContext"; import { formatDate, formatDateTime } from "@/lib/format"; type DateTimeFieldProps = { label: string; value: Date; mode?: "date" | "datetime"; maximumDate?: Date; minimumDate?: Date; onChange: (date: Date) => void; }; export function DateTimeField({ label, value, mode = "datetime", maximumDate = new Date(), minimumDate, onChange, }: DateTimeFieldProps) { const { colors, isDark } = useAppTheme(); const [open, setOpen] = useState(false); const [draft, setDraft] = useState(value); function openPicker() { setDraft(value); setOpen(true); } 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 ( {label} [ styles.trigger, { borderColor: colors.border, backgroundColor: colors.cardGlass, }, pressed && styles.triggerPressed, ]} > {mode === "date" ? formatDate(value) : formatDateTime(value)} {Platform.OS === "ios" ? ( setOpen(false)}> setOpen(false)}> event.stopPropagation()} > setOpen(false)}> Cancel {label} { applyDate(draft); setOpen(false); }} > Done ) : open ? ( ) : null} ); } const styles = StyleSheet.create({ wrapper: { gap: spacing.xs, alignSelf: "stretch", width: "100%", }, label: { fontSize: 13, fontFamily: fonts.bodyMedium, }, trigger: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", alignSelf: "stretch", width: "100%", borderWidth: 1, borderRadius: radii.lg, paddingHorizontal: spacing.md, minHeight: 48, paddingVertical: spacing.sm, }, triggerPressed: { opacity: 0.92, }, value: { fontSize: 15, fontFamily: fonts.body, flex: 1, }, backdrop: { flex: 1, justifyContent: "flex-end", backgroundColor: "rgba(0,0,0,0.45)", }, sheet: { borderTopLeftRadius: radii.lg, borderTopRightRadius: radii.lg, paddingBottom: spacing.lg, }, sheetHeader: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: spacing.md, paddingVertical: spacing.md, borderBottomWidth: 1, }, sheetTitle: { fontSize: 15, fontFamily: fonts.bodySemiBold, }, sheetAction: { fontSize: 15, fontFamily: fonts.bodyMedium, }, });