react-native - 从 React-native 的列表视图中选择单个复选框
问题描述
我只想选择一个复选框,而不是多个。
如果我一一选择两个复选框,则应取消选中先前选择的复选框。
在我下面的代码中,我可以选择多个复选框。
import React ,{Component} from "react";
import CircleCheckBox, {LABEL_POSITION} from "react-native-circle-checkbox";
class Select_Delivery_Option extends React.Component {
constructor(props) {
super(props);
const ds = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2
});
this.state = {
check_data:[],
dataSource: ds.cloneWithRows([]),
checked:false,
isLoading:false,
};
}
//I had call The componentDidMount for json Data here and bind it in Data source;
render() {
return ();
}
_renderRow(rowData: string, sectionID: number, rowID: number) {
return (
<View style={{ flex:1,flexDirection:'column',backgroundColor:'#FFF'}}>
<View style={{ flex:1,flexDirection:'row',backgroundColor:'#FFF'}}>
<View style={{flexDirection:'column',margin:10}}>
{rowData.adbHomeAddress}
<CircleCheckBox
checked={rowData.checked}
onToggle={()=>this._onPressRow(rowID, rowData,rowData.checked)}
labelPosition={LABEL_POSITION.LEFT}
label={rowData.Address1 +" ,\n "+ rowData.Address2 +",\n"+rowData.ctiName+", "+rowData.staName+", "+rowData.ctrName+","+rowData.adbZip+"."}
innerColor="#C72128"
outerColor="#C72128"
styleLabel={{color:'#000',marginLeft:10}}
/>
</View>
</View>
</View>
);
}
_onPressRow = (rowID,rowData,checked) => {
const {check_data,filter} = this.state;
console.log('rowdata',rowData);
console.log('rowid',rowID);
console.log('checked',checked);
rowData.checked = !rowData.checked;
var dataClone = this.state.check_data;
dataClone[rowID] = rowData;
this.setState({check_data: dataClone });
}
}
链接到使用的 CircleCheckBox 组件:https ://github.com/paramoshkinandrew/ReactNativeCircleCheckbox
解决方案
我有同样的要求,浪费时间寻找解决方案。最终,我能够自己解决问题。
在下面发布我的答案,我在示例中使用了钩子,如果有人想要基于类的解决方案,请告诉我。
const checkboxComponent = () => {
const [checkboxValue, setCheckboxValue] = React.useState([
{ label: 'Customer', value: 'customer', checked: false },
{ label: 'Merchant', value: 'merchant', checked: false },
{ label: 'None', value: 'none', checked: false },
])
const checkboxHandler = (value, index) => {
const newValue = checkboxValue.map((checkbox, i) => {
if (i !== index)
return {
...checkbox,
checked: false,
}
if (i === index) {
const item = {
...checkbox,
checked: !checkbox.checked,
}
return item
}
return checkbox
})
setCheckboxValue(newValue)
}
return (
<View>
{checkboxValue.map((checkbox, i) => (
<View style={styles.checkboxContainer} key={i}>
<CheckBox
value={checkbox.checked}
onValueChange={(value) => checkboxHandler(value, i)}
/>
<Text style={styles.label}>{checkbox.label}</Text>
</View>
))}
</View>
)
}
export default checkboxComponent
推荐阅读
- deep-learning - 解释用另一个模型作为参数实例化模型的概念?VGGFace(model='resnet50', include_top=False)
- unix - 如何将身份文件与 rsync 和隧道一起使用?
- list - 输入'列表
' 不是类型 'List 的子类型 >' of '函数结果' - java - 如何在 Context xml 文件中定义 Tomcat 中的 UserDatabase?
- javascript - 根据 ES6 中的值从数组中删除重复项
- reactjs - 如何等待子组件渲染
- couchbase - Couchbase 6.5.1 和 Bloom 过滤器
- python - Python 多处理和共享内存 - 为什么我会得到不同的结果?
- python - 在多元随机森林分类模型中使用 CSR 矩阵
- python - 情节,Python。我可以根据日期时间对象绘制垂直线吗?