簡體   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