繁体   English   中英

我必须在 React native 中添加多项选择单选按钮

[英]I have to add multiple choice radio button in React native

每个项目都有两个单选按钮,一个是是,一个是否。我已经为来自 Flatlist (API) 的每个项目添加了单选按钮。 错误是当我 select 四个项目前面的 8 个单选按钮之一时,仅选择了 8 个单选按钮之一。

这是我的 expo 代码,您可以编辑expo 零食代码

目前我的屏幕显示这种预期的行为

https://snack.expo.dev/YNxLxt8pu

 import React, { useEffect, useState } from 'react'; import { View, Text, TouchableOpacity, StyleSheet, Image, ScrollView, FlatList, SafeAreaView, RefreshControl, Vibration, ActivityIndicator, } from 'react-native'; import { Card, TextInput, RadioButton } from 'react-native-paper'; const auditParam = [ { id: 1, name: 'Category 1', parent_id: 0, status: 'active', audit_sub_category: [ { id: 15, name: 'a Sub 2a', parent_id: 1, status: 'active', }, { id: 16, name: 'a Sub 2abc', parent_id: 1, status: 'active', }, ], }, { id: 4, name: 'Category 2b', parent_id: 0, status: 'active', audit_sub_category: [ { id: 5, name: '2b Sub 1', parent_id: 4, status: 'active', }, { id: 12, name: '2b Sub 3', parent_id: 4, status: 'active', }, ], }, { id: 6, name: 'Category 3c', parent_id: 0, status: 'active', audit_sub_category: [ { id: 7, name: '3c Sub 1', parent_id: 6, status: 'active', }, { id: 10, name: '3c Sub 2', parent_id: 6, status: 'active', }, ], }, ]; export default function App() { const getState = () => { let objData = {}; auditParam.map((data) => { data?.audit_sub_category.map((newData) => { let obj = { id:newData?.id, sel:false } objData[newData.id] = null }) }) return objData } const [cat,setCat] = useState(getState()); const onPress = (index,value) => { const existing = {...cat}; existing[index] = value setCat(existing) }; const onRadiochange = (index, value) => { const existing = {...cat}; existing[index] = value setCat(existing) }; const renderItem = ({ item }) => { let items = []; if (item.audit_sub_category) { items = item.audit_sub_category.map((audit_sub_category) => { const index = audit_sub_category.id; return ( <> <View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', width: 280, }}> <Text>{audit_sub_category.name}</Text> <RadioButton.Group > <View style={styles.singleRadioButtonContainer}> <Text>Yes</Text> <RadioButton color="#5d86d7" value="1" // key={index} status={ cat[index] === true? 'checked': 'unchecked' } onPress={() => { onPress(index,true); }} /> </View> <View style={styles.singleRadioButtonContainer}> <Text>No</Text> <RadioButton color="#5d86d7" value="0" key={index} status={ cat[index] === false? 'checked': 'unchecked' } onPress={() => { onPress(index,false); }} /> </View> </RadioButton.Group> </View> </> ); }); } return ( <ScrollView> <Text style={styles.textStyle}>{item.name}</Text> <View style={{ flexDirection: 'row', justifyContent: 'space-evenly', marginHorizontal: 20, }}> <Text style={{ alignSelf: 'center' }}>{items}</Text> </View> </ScrollView> ); }; return ( <SafeAreaView style={styles.container}> <FlatList style={styles.container} // data={data} data={auditParam} renderItem={renderItem} keyExtractor={(item, index) => index.toString()} /> </SafeAreaView> ); } const styles = StyleSheet.create({ container: { flex: 2, backgroundColor: 'white', }, textStyle: { marginHorizontal: 20, marginTop: 10, color: 'black', fontWeight: '600', }, singleRadioButtonContainer: { flexDirection: 'row', alignItems: 'center', marginRight: 10, }, });

希望能帮助到你在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM