import { PlaceholderColor } from "@/constants/color-palet"; import { GStyles } from "@/styles/global-styles"; import Ionicons from "@expo/vector-icons/Ionicons"; import React, { useState } from "react"; import { TextInput as RNTextInput, StyleProp, Text, TouchableOpacity, View, ViewStyle, useColorScheme } from "react-native"; type IconType = React.ReactNode | string; type Props = { iconLeft?: IconType; iconRight?: IconType; label?: string; required?: boolean; error?: string; secureTextEntry?: boolean; fontColor?: string; disabled?: boolean; borderRadius?: number; style?: StyleProp; maxLength?: number; containerStyle?: StyleProp; } & Omit, "style">; const TextInputCustom = ({ iconLeft, iconRight, label, required = false, error: externalError = "", secureTextEntry = false, fontColor = "#000", disabled = false, borderRadius = 8, style, keyboardType, onChangeText, maxLength, containerStyle, ...rest }: Props) => { const [isPasswordVisible, setIsPasswordVisible] = useState(false); const [internalError, setInternalError] = useState(""); // Helper untuk render ikon const renderIcon = (icon: IconType) => { if (!icon) return null; return typeof icon === "string" ? ( {icon} ) : ( icon ); }; // Validasi email jika keyboardType = email-address const handleTextChange = (text: string) => { if (keyboardType === "email-address") { const isValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(text); if (!isValid) { setInternalError("Masukkan email yang valid"); } else { setInternalError(""); } } // Panggil onChangeText eksternal jika ada if (onChangeText) { onChangeText(text); } }; const colorScheme = useColorScheme(); const theme = PlaceholderColor[colorScheme || "light"]; return ( {label && ( {label} {required && *} )} {iconLeft && ( {renderIcon(iconLeft)} )} {secureTextEntry && ( setIsPasswordVisible((prev) => !prev)} style={GStyles.inputIcon} > )} {iconRight && ( {renderIcon(iconRight)} )} {/* Prioritaskan error eksternal */} {externalError || internalError ? ( {externalError || internalError} ) : null} ); }; export default TextInputCustom;