import React, { useState } from "react"; import { View, Text, TouchableOpacity, StyleSheet, ScrollView, } from "react-native"; import type { CalendarTheme } from "../types/theme"; export interface TimePickerProps { value?: { hours: number; minutes: number; seconds?: number }; onChange: (hours: number, minutes: number, seconds?: number) => void; timeFormat?: "12h" | "24h"; minuteInterval?: number; showSeconds?: boolean; theme?: CalendarTheme; } export function TimePicker({ value = { hours: 0, minutes: 0, seconds: 0 }, onChange, timeFormat = "24h", minuteInterval = 1, showSeconds = false, theme, }: TimePickerProps) { const [selectedHours, setSelectedHours] = useState(value.hours); const [selectedMinutes, setSelectedMinutes] = useState(value.minutes); const [selectedSeconds, setSelectedSeconds] = useState(value.seconds || 0); const [period, setPeriod] = useState<"AM" | "PM">( value.hours >= 12 ? "PM" : "AM" ); const maxHours = timeFormat === "12h" ? 12 : 24; const hours = Array.from({ length: maxHours }, (_, i) => timeFormat === "12h" ? i + 1 : i ); const minutes = Array.from( { length: 60 / minuteInterval }, (_, i) => i * minuteInterval ); const seconds = Array.from({ length: 60 }, (_, i) => i); const handleHourSelect = (hour: number) => { let actualHour = hour; if (timeFormat === "12h") { actualHour = period === "PM" ? hour === 12 ? 12 : hour + 12 : hour === 12 ? 0 : hour; } setSelectedHours(actualHour); onChange( actualHour, selectedMinutes, showSeconds ? selectedSeconds : undefined ); }; const handleMinuteSelect = (minute: number) => { setSelectedMinutes(minute); onChange(selectedHours, minute, showSeconds ? selectedSeconds : undefined); }; const handleSecondSelect = (second: number) => { setSelectedSeconds(second); onChange(selectedHours, selectedMinutes, second); }; const handlePeriodToggle = () => { const newPeriod = period === "AM" ? "PM" : "AM"; setPeriod(newPeriod); const newHours = newPeriod === "PM" ? selectedHours < 12 ? selectedHours + 12 : selectedHours : selectedHours >= 12 ? selectedHours - 12 : selectedHours; setSelectedHours(newHours); onChange( newHours, selectedMinutes, showSeconds ? selectedSeconds : undefined ); }; const displayHour = timeFormat === "12h" ? selectedHours === 0 ? 12 : selectedHours > 12 ? selectedHours - 12 : selectedHours : selectedHours; return ( {hours.map((hour) => ( handleHourSelect(hour)} > {String(hour).padStart(2, "0")} ))} {minutes.map((minute) => ( handleMinuteSelect(minute)} > {String(minute).padStart(2, "0")} ))} {showSeconds && ( {seconds.map((second) => ( handleSecondSelect(second)} > {String(second).padStart(2, "0")} ))} )} {timeFormat === "12h" && ( AM PM )} ); } const styles = StyleSheet.create({ container: { padding: 16, }, pickers: { flexDirection: "row", justifyContent: "center", height: 200, }, column: { flex: 1, marginHorizontal: 4, }, periodColumn: { width: 60, marginHorizontal: 4, }, timeCell: { padding: 12, alignItems: "center", borderRadius: 8, marginVertical: 2, }, periodCell: { padding: 12, alignItems: "center", borderRadius: 8, marginVertical: 2, flex: 1, }, selectedTime: { backgroundColor: "#007AFF", }, timeText: { fontSize: 16, fontWeight: "500", }, });