export default function App() {
const windShield = [
{ key: 0000, txt: 'Ok', isChecked: false, isImage: true },
{ key: 0001, txt: 'Pitted', isChecked: false, isImage: false },
{ key: 0002, txt: 'Cracked', isChecked: false, isImage: false },
{ key: 0003, txt: 'Star (S)', isChecked: false, isImage: false },
];
const handleChange = (key: number) => {
let temp = checked?.windShield?.map((item) => {
if (key === item.key) {
return { ...item, isChecked: !item.isChecked };
}
return { ...item, isChecked: false };
});
setChecked(prevState => {
return {...prevState, windShield: temp}
});
};
return (
<div>
<View style={{flexDirection: 'row', marginHorizontal: 8, flexWrap: 'wrap', flex: 1}}>
{checked?.windShield?.map((item, index)=>{
return(
<View key={item.txt} style={{flexDirection: 'row', alignItems: 'center', flexWrap: 'wrap', width: '50%'}}>
<Checkbox
status={item?.isChecked ? 'checked': 'unchecked'}
onPress={() => {
handleChange(item?.key)
}}
/>
<Text>{item?.txt}</Text>
</View>
)
})}
</View>
</div>
);
}
when user is select OK (true) other option set false but when user click on any other with (ok option will false)
like when i click on first option and other 3 option act like radio button but other three option act like multi select
2
Answers
If it was me, I would handle it like this:
Try to modify
handleChange
function as follows: