我是反应本机的新手,并尝试在视图中添加复选框,但我无法在反应本机视图中获得复选框。
提前谢谢。
import React from 'react';
import {View, CheckBox } from 'react-native';
export default class SimpleCheckBox extends React.Component{
constructor(){
super();
}
render(){
return(
<View>
<CheckBox value={true} onValueChange={() => console.log("value might change")}/>
</View>
)
}
}
CheckBox
仅在 0.49版本中添加到React-Native中,并且仅适用于Android。这意味着,如果您正在为 iOS 开发或无法升级应用程序版本 - 您将需要使用自定义复选框组件。
您可以在以下位置查看此新版本引入的所有更改: https://github.com/facebook/react-native/releases/tag/v0.49.0
import React, { Component } from 'react'
import styled from 'styled-components'
import { TouchableOpacity, View } from 'react-native'
const CustomCheckBox = styled(View)`
height: 24px;
width: 24px;
background: ${props => (props.checkBoxActive ? '#448ccb' : 'transparent')};
border-radius: 0px;
position: relative;
justify-content: center;
margin: 0px 8px 0 0;
border: solid 1px #e1e4e5;
`
const CheckIcon = styled(View)`
border-radius: 0px;
align-self: center;
transform: rotate(-30deg);
`
/*==============================
Custom checkbox styled
===============================*/
const CheckIconWrapper = styled(View)`
position: relative;
left: 2px;
top: -2px;
`
const CheckIconVertical = styled(View)`
height: 5px;
width: 2px;
background: ${props => (props.checkBoxActive ? '#fff' : 'transparent')};
`
const CheckIconHorizontal = styled(View)`
height: 2px;
width: 16px;
background: ${props => (props.checkBoxActive ? '#fff' : 'transparent')};
`
class CheckBox extends Component {
state = {
checkBox: false
}
render() {
return (
<TouchableOpacity
onPress={() => {
this.setState({ checkBox: !this.state.checkBox })
}}>
<CustomCheckBox checkBoxActive={this.state.checkBox}>
<CheckIcon>
<CheckIconWrapper>
<CheckIconVertical checkBoxActive={this.state.checkBox} />
<CheckIconHorizontal checkBoxActive={this.state.checkBox} />
</CheckIconWrapper>
</CheckIcon>
</CustomCheckBox>
</TouchableOpacity>
)
}
}
export default CheckBox