// DateTimeInput.tsx import { AccentColor, MainColor } from "@/constants/color-palet"; import { GStyles } from "@/styles/global-styles"; import { Ionicons } from "@expo/vector-icons"; import DateTimePicker, { DateTimePickerEvent, } from "@react-native-community/datetimepicker"; import dayjs from "dayjs"; import React, { useState, useRef } from "react"; import { Button, StyleProp, Text, View, ViewStyle, Keyboard, TouchableOpacity, Modal, } from "react-native"; import ClickableCustom from "../Clickable/ClickableCustom"; import TextCustom from "../Text/TextCustom"; interface DateTimeInputProps { // Main value?: DateTimePickerEvent | Date | null; mode?: "date" | "time"; onChange: (selectedDate: DateTimePickerEvent | Date | null) => void; maximumDate?: Date; minimumDate?: Date; // Main label?: string; required?: boolean; disabled?: boolean; iconLeft?: React.ReactNode; style?: StyleProp; borderRadius?: number; externalError?: string; internalError?: string; containerStyle?: StyleProp; } const DateTimeInput_IOS: React.FC = ({ // Main value, mode, onChange, maximumDate, minimumDate, // Main label, required, disabled, iconLeft, style, borderRadius = 8, externalError, internalError, containerStyle, }) => { const [show, setShow] = useState(false); const [selectedDate, setSelectedDate] = useState( value as any, ); // State sementara untuk menyimpan nilai yang dipilih const [tempSelectedDate, setTempSelectedDate] = useState( value as any, ); const handleConfirm = (event: any, date?: Date) => { if (event.type === "set" && date !== undefined) { // Hanya perbarui state sementara, bukan state utama setTempSelectedDate(date); } }; const handlePress = () => { // Sembunyikan keyboard sebelum menampilkan date picker Keyboard.dismiss(); // Set state sementara ke nilai saat ini setTempSelectedDate(selectedDate); setShow(!show); }; // Fungsi untuk menangani klik di luar area picker const handleOutsidePress = () => { if (show) { setShow(false); } }; return ( <> !disabled && handlePress()} > {label && ( {label} {required && *} )} {selectedDate ? ( {dayjs(selectedDate).format("DD-MM-YYYY HH:mm")} ) : ( "Pilih tanggal" )} {externalError || (internalError && ( {externalError || internalError} ))} setShow(false)} > true} // Mencegah event bubbling ke TouchableOpacity induk onResponderRelease={() => {}} // Handler kosong untuk mencegah event bubbling > {/* Pilih Tanggal & Waktu setShow(false)}> */} { setShow(false); // Kembalikan ke nilai sebelumnya jika batal setTempSelectedDate(selectedDate); }} style={{ flex: 1, alignItems: "center", justifyContent: "center", padding: 12, borderRadius: 10, backgroundColor: MainColor.placeholder, }} > Batal { // Simpan nilai yang dipilih ke state utama hanya saat OK ditekan setSelectedDate(tempSelectedDate); onChange(tempSelectedDate as any); setShow(false); }} style={{ flex: 1, alignItems: "center", justifyContent: "center", padding: 12, borderRadius: 10, backgroundColor: MainColor.darkblue, }} > OK ); }; export default DateTimeInput_IOS;