react native 多选按钮
在做项目的时候有一个需求,是可以选择多个条件的,特地在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',
     },
 })
react native 多选按钮的更多相关文章
- [RN] React Native 获取验证码 按钮
		
React Native 获取验证码 按钮 效果如图: 实现方法: 一.获取验证码 按钮组件 封装 CountDownButton.js "use strict"; import ...
 - react native 键盘遮挡按钮点击事件
		
在做项目的时候,我遇到一个很奇怪的问题,我先描述一下问题,在InputText输入内完成以后,点击按钮进行下一步的操作的时候,第一次点击的时候,按钮没有响应,第二次点击的时候才会响应.这样对用户体验有 ...
 - react native  底部按钮切换
		
在react native 中底部按钮的切换 主要的是运用的是<TabBarNavigator/>这个组件,具体的代码实现如下: render() { return ( <T ...
 - 如何用 React Native 创建一个iOS APP?(三)
		
前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...
 - [RN] React Native 实现 多选标签
		
React Native 实现 多选标签 效果如下: 实现代码: import React, {Component} from 'react'; import {Button, StyleSheet, ...
 - 史上最全Windows版本搭建安装React Native环境配置
		
史上最全Windows版本搭建安装React Native环境配置 配置过React Native 环境的都知道,在Windows React Native环境配置有很多坑要跳,为了帮助新手快速无误的 ...
 - React Native:使用 JavaScript 构建原生应用
		
[转载] 本篇为联合翻译,译者:寸志,范洪春,kmokidd,姜天意 数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生 ...
 - react native 入门实践
		
上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.希望 ...
 - React Native填坑之旅--Navigation篇
		
React Native的导航有两种,一种是iOS和Android通用的叫做Navigator,一种是支持iOS的叫做NavigatorIOS.我们这里只讨论通用的Navigator.会了Naviga ...
 
随机推荐
- Git Stash紧急处理问题,需要切分支
			
在开发过程中,大家都遇到过bug,并且有些bug是需要紧急修复的. 当开发人员遇到这样的问题时,首先想到的是我新切一个分支,把它修复了,再合并到master上. 当时问题来了,你当前正在开发的分支上面 ...
 - Codeforces Round #354 (Div. 2) ABCD
			
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
 - 兼容好的JS图片上传预览代码
			
转 : http://www.codefans.net/articles/1395.shtml 兼容好的JS图片上传预览代码 (谷歌,IE11) <html xmlns="http:/ ...
 - 【Java之对象清理】finalize()的用途
			
Java允许在类中定义一个名为finalize()的方法.它的工作原理是:一旦垃圾回收器准备好释放对象占用的存储空间,将首先调用其finalize()方法.并且在下一次垃圾回收动作发生时,才会真正回收 ...
 - Android——SharedPreferences
			
SharedPreferences是一种轻型的Android数据存储方式,用来保存应用的一些常用配置. 实现SharedPreferences存储的步骤如下: 1.根据Context获取SharedP ...
 - js事件技巧方法整合
			
window.resizeTo(800,600); //js设置浏览器窗口尺寸 window.open (function(){ resizeTo(640,480);//设置浏览器窗口尺寸 moveT ...
 - ssh配置git clone简易流程
			
1. 生成密钥 ssh-keygen -t rsa -C "jaynaruto@qq.com" //如果只有一对密钥,建议不要修改默认的密钥名称,即一直按回车即可 此命令会在你当前 ...
 - 《转载》Spring MVC之@RequestBody, @ResponseBody 详解
			
引言: 接上一篇文章讲述处理@RequestMapping的方法参数绑定之后,详细介绍下@RequestBody.@ResponseBody的具体用法和使用时机: 简介: @RequestBody 作 ...
 - Python 之旅
			
Python2 之旅: https://funhacks.net/explore-python/ <Python Cookbook>第三版 PYTHON3 http://pyt ...
 - html JS  打开本地程序及文件
			
在网页打开本地应用程序示例: 一.在本地注册表自定义协议:以自定义调用Viso为例 1.在HKEY_CLASSES_ROOT下添加项ZVISIO. 2.修改ZVISIO项下的"(默认)&qu ...