在做项目的时候有一个需求,是可以选择多个条件的,特地在Github上找了一些案例,发现没有什么合适的,于是自己根据这些案例,改装一下,封装了一个合适自己的。先看我封装的代码。

 import React, {Component} from 'react';
import {
StyleSheet,
View,
Image,
Text,
TouchableHighlight,
} from 'react-native' export default class CheckBox extends Component {
constructor(props) {
super(props);
this.state = {
isChecked: this.props.isChecked,
}
} /**
* propTypes是React内部提供的校验器,如果通过props传过的数据与之不匹配,则会抛出异常。
*
*/
static propTypes = {
...View.propTypes,
leftText: React.PropTypes.string,
leftTextView: React.PropTypes.element,
rightText: React.PropTypes.string,
leftTextStyle: Text.propTypes.style,
rightTextView: React.PropTypes.element,
rightTextStyle: Text.propTypes.style,
checkedImage: React.PropTypes.element,
unCheckedImage: React.PropTypes.element,
onClick: React.PropTypes.func.isRequired,
isChecked: React.PropTypes.bool } /**
* 如果没有通过props传过来数据,则默认的是这样
* @type
*/
static defaultProps = {
isChecked: false,
leftTextStyle: {},
rightTextStyle: {}
} /**
* 左边文字
*/
_renderLeft() {
if (this.props.leftTextView) {
return this.props.leftTextView;
} if (!this.props.leftText) {
return null;
}
return (
<Text style={[styles.leftText, this.props.leftTextStyle]}>{this.props.leftText}</Text>
)
} /**
* 右边的文字
* @returns {*}
* @private
*/
_renderRight() {
if (this.props.rightTextView) {
return this.props.rightTextView;
}
if (!this.props.rightText) {
return null;
}
return (
<Text style={[styles.rightText, this.props.rightTextStyle]}>{this.props.rightText}</Text>
)
} /**
* 选中和为选中的图片按钮样式
* @returns {*}
* @private
*/
_renderImage() {
if (this.state.isChecked) {
return this.props.checkedImage ? this.props.checkedImage : this.genCheckedImage();
} else {
return this.props.unCheckedImage ? this.props.unCheckedImage : this.genCheckedImage();
}
} genCheckedImage() {
var source = this.state.isChecked ? require('./img/ic_check_box.png') : require('./img/ic_check_box_outline_blank.png');
return (
<Image source={source}/>
)
} onClick() {
this.setState({
isChecked: !this.state.isChecked
})
this.props.onClick();
} render() {
return (
<TouchableHighlight
style={this.props.style}
onPress={()=>this.onClick()}
underlayColor='transparent'
>
<View style={styles.container}>
{this._renderLeft()}
{this._renderImage()}
{this._renderRight()}
</View>
</TouchableHighlight>
)
}
} const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center'
},
leftText: {
flex: 1,
},
rightText: {
flex: 1,
marginLeft: 10
}
})

基本上这些需要的属性都是通过props来传递过来的。

用法也比较简单:

 import React, {Component} from 'react';
import {
StyleSheet,
ScrollView,
View,
} from 'react-native' import keys from './keys.json'
import Toast from 'react-native-easy-toast' export default class example extends Component {
constructor(props) {
super(props);
this.state = {
dataArray: []
}
} componentDidMount() {
this.loadData();
} loadData() {
this.setState({
dataArray: keys
})
} onClick(data) {
data.checked = !data.checked;
let msg=data.checked? 'you checked ':'you unchecked '
this.toast.show(msg+data.name);
} renderView() {
if (!this.state.dataArray || this.state.dataArray.length === 0)return;
var len = this.state.dataArray.length;
var views = [];
for (var i = 0, l = len - 2; i < l; i += 2) {
views.push(
<View key={i}>
<View style={styles.item}>
{this.renderCheckBox(this.state.dataArray[i])}
{this.renderCheckBox(this.state.dataArray[i + 1])}
</View>
<View style={styles.line}/>
</View>
)
}
views.push(
<View key={len - 1}>
<View style={styles.item}>
{len % 2 === 0 ? this.renderCheckBox(this.state.dataArray[len - 2]) : null}
{this.renderCheckBox(this.state.dataArray[len - 1])}
</View>
</View>
)
return views; } renderCheckBox(data) {
var leftText = data.name;
return (
<CheckBox
style={{flex: 1, padding: 10}}
onClick={()=>this.onClick(data)}
isChecked={data.checked}
leftText={leftText}
/>);
} render() {
return (
<View style={styles.container}>
<ScrollView>
{this.renderView()}
</ScrollView>
<Toast ref={e=>{this.toast=e}}/>
</View>
)
} } const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f3f2f2',
marginTop:30
},
item: {
flexDirection: 'row',
},
line: {
flex: 1,
height: 0.3,
backgroundColor: 'darkgray',
},
})

最新文章

  1. 匹夫细说C#:不是“栈类型”的值类型,从生命周期聊存储位置
  2. 洛谷P1196 银河英雄传说[带权并查集]
  3. (36)老版和新版API调用
  4. JS之setAttribute和getAttribute
  5. mysql中bigint、int、mediumint、smallint 和 tinyint的取值范围
  6. Shiro权限控制框架
  7. .NET设计模式(7):创建型模式专题总结(Creational Pattern)
  8. Echarts使用随笔(1)-Echarts中markPoint的使用(静态、动态)-effect
  9. SmartQQ二维码登陆接口分析
  10. js刷新页面不回到顶部
  11. 购物篮算法的理解-基于R的应用
  12. 如何用tomcat实现类似weblogic那样的热部署方式
  13. Tumblr:我们是如何从 PHP 5 升级到 PHP 7 的
  14. Excel中如何截取字符串中指定字符后的部分字符
  15. 欢迎来到Python世界
  16. SQL操作json类型数据的函数
  17. func_get_args函数
  18. OpenStack 安装:keystone服务
  19. float导致出现大面积空白
  20. Linux CPU信息和使用情况查看(CentOS)

热门文章

  1. 第一次做socket的一些心得
  2. vi 的使用
  3. 取代 Windows Search
  4. django(一)
  5. Linux学习之六--unZip/Zip的安装及使用
  6. 如何添加商*通新对话快捷链接?不用js代码
  7. SQL Server 积累
  8. Latex使用整理
  9. MySql: log 位置
  10. 利用chrome插件批量读取浏览器页面内容并写入数据库