import CheckBox from '@react-native-community/checkbox'; import React, { useState, type ReactNode } from 'react'; import { Text, TouchableOpacity, View, type StyleProp, type TextStyle, type ViewStyle, } from 'react-native'; interface CheckBoxModel { children?: CheckBoxModel[]; value: string; label: string; icon?: ReactNode; } interface Props { item: CheckBoxModel; onSelect: (val: string) => void; selected: string[]; expandedIcon?: ReactNode; unExpandedIcon?: ReactNode; checkedColor?: string; unCheckedColor?: string; textStyle?: StyleProp; labelColor?: string; styles?: StyleProp; } const CheckboxItem = (props: Props) => { const { item, onSelect, selected, expandedIcon, unExpandedIcon, checkedColor, unCheckedColor, textStyle, styles, labelColor, } = props; const [collapsed, setCollapsed] = useState(false); const handleCheck = (item: CheckBoxModel) => { if (item.children && item.children.length > 0) { item.children.forEach((val) => handleCheck(val)); } else { onSelect(item.value); } }; const calcChildLength = (item: CheckBoxModel) => { let total = 0; if (item.children) { item.children.forEach((child) => { total += calcChildLength(child); }); } else { total = +1; } return total; }; const countSelectedItems = (item: CheckBoxModel) => { let total = 0; if (item.children) { item.children.forEach((val) => (total += countSelectedItems(val))); } else { total += selected.includes(item.value) ? 1 : 0; } return total; }; const renderCheckbox = (item: CheckBoxModel) => { let count = 0; let totalChild = 0; if (item.children) { item.children.forEach((val) => { count += countSelectedItems(val); totalChild += calcChildLength(val); }); } else { count += selected.includes(item.value) ? 1 : 0; totalChild = 1; } return ( 0 && count > 0 && count < totalChild ? checkedColor ?? '#1abc9c' : unCheckedColor ?? '#bdc3c7' : unCheckedColor ?? '#bdc3c7', }} onValueChange={() => handleCheck(item)} value={ item.children ? count === totalChild ? true : false : selected.includes(item.value) } /> ); }; return ( {item.children && item.children.length > 0 ? ( setCollapsed(!collapsed)}> {collapsed ? ( expandedIcon ? ( expandedIcon ) : ( <> ) ) : unExpandedIcon ? ( unExpandedIcon ) : ( <> )} ) : ( )} handleCheck(item)} style={{ flexDirection: 'row', flex: 1, alignItems: 'center' }} > {renderCheckbox(item)} {item.icon && item.icon} {item.label} {collapsed && ( {item.children && item.children.length > 0 && item.children.map((chil) => ( onSelect(val)} selected={selected} unCheckedColor={unCheckedColor} expandedIcon={expandedIcon} checkedColor={checkedColor} unExpandedIcon={unExpandedIcon} textStyle={textStyle} styles={styles} labelColor={labelColor} /> ))} )} ); }; export default CheckboxItem;