React Native 自定义radio 单选or多选
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了React Native 自定义radio 单选or多选,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2770字,纯文字阅读大概需要4分钟。
内容图文
前段时间一直在做react native开发,下面介绍下自定义自定义radio的封装。
1 import React, { PureComponent } from ‘react‘; 2 import PropTypes from ‘prop-types‘; 3 import { View, Text, StyleSheet, TouchableOpacity, Image, Dimensions } from ‘react-native‘; 4 import uniq from ‘lodash/uniq‘; 5 import includes from ‘lodash/includes‘; 6 import difference from ‘lodash/difference‘; 7 8 const selected = require(‘../images/programme/radioSelect.png‘) 9 const unselected = require(‘../images/programme/radioUnSelect.png‘) 1011 export default class Radio extends PureComponent { 12 handleSelect = (option, index) => { 13 const { onSelect, type, value } = this.props 14 const valueAfterSelected = toggleValue(option, type, value) 1516 onSelect && onSelect(valueAfterSelected, index) 17 } 1819 render() { 20 const { options, value, type } = this.props; 2122return ( 23 <View style={{ flexDirection: ‘row‘, flexWrap: ‘wrap‘ }} > 24 { 25 options.map((option, index) => ( 26 <View key={index} style={styles.main}> 27 <TouchableOpacity onPress={() => this.handleSelect(option, index)}> 28 <Image 29 style={{ 30 marginLeft: 5, 31 marginRight: 10, 32 width: 15, 33 height: 15, 34 }} 35 source={isSelected(option, type, value) ? selected : unselected} 36 /> 37 </TouchableOpacity> 38 <Text style={styles.text}>{option.label}</Text> 39 </View> 40 )) 41 } 42 </View> 43 ); 44 } 45} 4647function isSelected(option, type, value) { 48if (type === ‘radio‘) { 49return option.value === value 50 } else { 51return (value || []).includes(option.value) 52 } 53} 5455function toggleValue(option, type, value) { 56if (type === ‘checkbox‘) { 57return !includes(value, option.value) ? uniq([...(value || []), option.value]) : difference(value, [option.value]) 58 } else { 59return option.value 60 } 61} 6263 const { width } = Dimensions.get(‘window‘); 64 const styles = StyleSheet.create({ 65 main: { 66 flexDirection: ‘row‘, 67 marginLeft: width == 320 ? 8 : 10, 68 marginRight: width == 320 ? 5 : 10, 69 marginTop: 5, 70 marginBottom: 5, 71 justifyContent: ‘flex-start‘, 72 alignItems: ‘center‘, 73 }, 7475 text: { 76 marginLeft: 0, 77 fontSize: 13, 78 color: ‘#333‘, 79 }, 80}); 8182 MoreRadio.propTypes = { 83 type: PropTypes.string, 84 options: PropTypes.array, 85} 8687 MoreRadio.defaultProps = { 88 type: ‘radio‘, 89 }
1 单选 2 <CustomRadio 3 type="radio" 4 onSelect={this.customRadio.bind(this)} 5 options={[{ value: 1, label: ‘报备‘ }]} 6 value={this.state.auditType} 7 />
1 多选 2 <CustomRadio 3 type="checkbox" 4 onSelect={this.customRadio.bind(this)} 5 options={[{ value: 1, label: ‘报备‘ }, { value: 2, label: ‘审批‘ }]} 6 value={this.state.productChannel} 7 />
原文:https://www.cnblogs.com/woshidameinv/p/10314861.html
内容总结
以上是互联网集市为您收集整理的React Native 自定义radio 单选or多选全部内容,希望文章能够帮你解决React Native 自定义radio 单选or多选所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。