// components/Select.tsx import { GStyles } from "@/styles/global-styles"; import React, { useState } from "react"; import { FlatList, Modal, Pressable, StyleProp, Text, TouchableOpacity, View, ViewStyle, } from "react-native"; type SelectItem = { label: string; value: string | number; }; type SelectProps = { label?: string; placeholder?: string; data: SelectItem[]; value?: string | number | null; required?: boolean; // <-- new prop disabled?: boolean; // <-- tambahkan prop disabled onChange: (value: string | number) => void; borderRadius?: number; styleContainer?: StyleProp; allowClear?: boolean; // <-- new prop clearLabel?: string; // default: "Kosongkan" }; const SelectCustom: React.FC = ({ label, placeholder = "Pilih opsi", data, value, required = false, // <-- default false disabled = false, // <-- default false onChange, borderRadius = 8, styleContainer, allowClear = true, // bisa dimatikan jika tidak perlu clearLabel = "Hapus Pilihan", }) => { const [modalVisible, setModalVisible] = useState(false); const selectedItem = data.find((item) => item.value === value); const hasError = required && value === null; // <-- check if empty and required // Gabungkan opsi clear di atas daftar const renderData = allowClear ? [...data, // { label: clearLabel, value: "__clear__" } ] : data; return ( {label && ( {label} {required && *} )} {/* Input Container */} !disabled && setModalVisible(true)} > {selectedItem?.label || placeholder} {/* Tombol Clear (Hanya muncul jika ada nilai terpilih & allowClear aktif) */} {!disabled && allowClear && value !== null && value !== undefined && ( onChange(null as any)} // null dikirim sebagai value > × )} setModalVisible(false)} > String(item.value)} renderItem={({ item }) => { if (item.value === "__clear__") { return ( { onChange(null as any); // kosongkan nilai setModalVisible(false); }} > {item.label} ); } return ( { onChange(item.value); setModalVisible(false); }} > {item.label} ); }} /> {/* Optional Error Message */} {hasError && ( Harap pilih salah satu )} ); }; export default SelectCustom;