// components/Select.tsx import { MainColor } from "@/constants/color-palet"; import { TEXT_SIZE_MEDIUM } from "@/constants/constans-value"; import React, { useState } from "react"; import { View, Text, Pressable, Modal, FlatList, StyleSheet, TouchableOpacity, } 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 onChange: (value: string | number) => void; }; const SelectCustom: React.FC = ({ label, placeholder = "Pilih opsi", data, value, required = false, // <-- default false onChange, }) => { const [modalVisible, setModalVisible] = useState(false); const selectedItem = data.find((item) => item.value === value); const hasError = required && value === null; // <-- check if empty and required return ( {label && ( {label} {required && *} )} setModalVisible(true)} > {selectedItem?.label || placeholder} setModalVisible(false)} > String(item.value)} renderItem={({ item }) => ( { onChange(item.value); setModalVisible(false); }} > {item.label} )} /> {/* Optional Error Message */} {hasError && ( Harap pilih salah satu )} ); }; export default SelectCustom; const styles = StyleSheet.create({ container: { marginBottom: 16, }, label: { fontSize: TEXT_SIZE_MEDIUM, marginBottom: 4, color: MainColor.white, fontWeight: "500", }, requiredIndicator: { color: "red", fontWeight: "bold", }, input: { borderWidth: 1, borderColor: "#ccc", padding: 12, borderRadius: 8, minHeight: 48, justifyContent: "center", backgroundColor: MainColor.white, }, inputError: { borderColor: "red", }, text: { fontSize: TEXT_SIZE_MEDIUM, }, placeholder: { fontSize: TEXT_SIZE_MEDIUM, color: MainColor.placeholder, }, modalOverlay: { flex: 1, backgroundColor: "rgba(0,0,0,0.3)", justifyContent: "center", alignItems: "center", }, modalContent: { width: "80%", maxHeight: 300, backgroundColor: "white", borderRadius: 8, overflow: "hidden", }, option: { padding: 16, borderBottomWidth: 1, borderBottomColor: "#eee", }, errorMessage: { marginTop: 4, fontSize: 12, color: "red", }, });