import { AccentColor, MainColor } from "@/constants/color-palet"; import { MaterialIcons } from "@expo/vector-icons"; // Bisa diganti dengan ikon lain import React, { useContext } from "react"; import { Animated, Text, TouchableOpacity, View } from "react-native"; import { checkboxStyles } from "./checkbox-styles"; // Context untuk Group interface CheckboxGroupContextType { value: (string | number)[]; onChange: (value: (string | number)[]) => void; disabled?: boolean; } const CheckboxGroupContext = React.createContext(null); // Tipe props // Tambahkan prop baru: groupValueKey interface CheckboxProps { label?: string | React.ReactNode; description?: string; error?: string; value?: boolean; // controlled value (untuk standalone) onChange?: (checked: boolean) => void; disabled?: boolean; size?: number; color?: string; style?: object; component?: React.ReactNode; // Prop tambahan untuk Group valueKey?: string | number; // nilai unik untuk identifikasi di group } const CheckboxCustom: React.FC = ({ label, description, error, value: controlledValue, onChange, disabled: propDisabled, size = 20, color = MainColor.yellow, style, component, valueKey, }) => { // const [uncontrolledChecked, setUncontrolledChecked] = useState(false); // const isChecked = controlledValue ?? uncontrolledChecked; // const scaleValue = new Animated.Value(isChecked ? 1 : 0); const group = useContext(CheckboxGroupContext); const isInsideGroup = !!group && valueKey !== undefined; // Jika di dalam group, gunakan logika group const isChecked = isInsideGroup ? group.value.includes(valueKey!) : controlledValue ?? false; const disabled = propDisabled || (isInsideGroup && group.disabled); const scaleValue = new Animated.Value(isChecked ? 1 : 0); const toggle = () => { if (disabled) return; if (isInsideGroup) { const newValue = isChecked ? group.value.filter((v) => v !== valueKey) : [...group.value, valueKey!]; group.onChange(newValue); } else if (onChange) { onChange(!controlledValue); } }; const styles = checkboxStyles({ size, color, disabled: disabled as boolean, error: !!error, }); return ( {isChecked && ( )} {component} {(label || description) && ( {label ? ( {label} ) : null} {description ? ( {description} ) : null} {error ? {error} : null} )} ); }; export default CheckboxCustom; // Export context agar bisa digunakan export { CheckboxGroupContext };