index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3. import Theme from '../local-theme/index'
  4. import { View, Text, TouchableOpacity, StyleSheet, Image } from 'react-native'
  5. import select from './res/img/checkBox.png'
  6. import unselect from './res/img/unCheckBox.png'
  7. import Icon from '../Iconfont/Iconfont'
  8. export default class Checkbox extends React.Component {
  9. constructor(props) {
  10. super(props)
  11. this.state = {
  12. isChecked: props.checked,
  13. }
  14. }
  15. static propTypes = {
  16. checked: PropTypes.bool,
  17. isRight: PropTypes.bool,
  18. size: PropTypes.number,
  19. label: PropTypes.any,
  20. labelStyle: PropTypes.object,
  21. checkBoxColor: PropTypes.string,
  22. onChange: PropTypes.func,
  23. }
  24. static defaultProps = {
  25. checked: false,
  26. isRight: false,
  27. size: 25,
  28. checkBoxColor: Theme.baseMap.brandColor,
  29. onChange: () => {},
  30. }
  31. componentWillReceiveProps = nextProps => {
  32. this.setState({ isChecked: nextProps.checked })
  33. }
  34. checkboxOnpress() {
  35. this.setState({ isChecked: !this.state.isChecked })
  36. this.props.onChange(!this.state.isChecked)
  37. }
  38. render() {
  39. let selectIcon = [
  40. 'icon-icon-danxuankuang-xuanzhong',
  41. 'icon-icon-danxuankuang-weixuanzhong',
  42. ]
  43. if (this.props.shape == 'square') {
  44. selectIcon = ['icon-icon-Checkbox-i', 'icon-icon-Checkbox']
  45. }
  46. return (
  47. <TouchableOpacity
  48. style={[
  49. styles.container,
  50. {
  51. width: this.state.layoutwidth,
  52. marginTop: Theme.baseMap.spacing.h_spacing_sm,
  53. },
  54. ]}
  55. onPress={() => this.checkboxOnpress()}
  56. >
  57. {this.props.isRight ? (
  58. typeof this.props.label == 'string' ? (
  59. <View
  60. style={[
  61. styles.checkbox,
  62. {
  63. paddingRight: Theme.baseMap.spacing.h_spacing_sm,
  64. },
  65. ]}
  66. >
  67. <Text style={[styles.text, this.props.labelStyle]}>
  68. {this.props.label}
  69. </Text>
  70. </View>
  71. ) : (
  72. this.props.label
  73. )
  74. ) : null}
  75. <View style={styles.checkbox}>
  76. {this.state.isChecked ? (
  77. // <Image
  78. // source={select}
  79. // style={{
  80. // tintColor: this.props.checkBoxColor,
  81. // width: this.props.size,
  82. // height: this.props.size
  83. // }}
  84. // />
  85. <Icon
  86. name={selectIcon[0]}
  87. size={this.props.size}
  88. color={'#E70013'}
  89. // color={this.props.checkBoxColor}
  90. />
  91. ) : (
  92. <Icon
  93. name={selectIcon[1]}
  94. size={this.props.size}
  95. color={'#CCCCCC'}
  96. />
  97. // <Image
  98. // source={unselect}
  99. // style={{
  100. // tintColor: this.props.checkBoxColor,
  101. // width: this.props.size,
  102. // height: this.props.size
  103. // }}
  104. // />
  105. )}
  106. </View>
  107. {this.props.isRight == false ? (
  108. typeof this.props.label == 'string' ? (
  109. <View
  110. style={[
  111. styles.checkbox,
  112. {
  113. paddingLeft: Theme.baseMap.spacing.h_spacing_sm,
  114. },
  115. ]}
  116. >
  117. <Text style={[styles.text, this.props.labelStyle]}>
  118. {this.props.label}
  119. </Text>
  120. </View>
  121. ) : (
  122. this.props.label
  123. )
  124. ) : null}
  125. </TouchableOpacity>
  126. )
  127. }
  128. }
  129. const styles = StyleSheet.create({
  130. container: {
  131. flexDirection: 'row',
  132. },
  133. checkbox: {
  134. justifyContent: 'center',
  135. alignItems: 'center',
  136. alignSelf: 'center',
  137. },
  138. text: {
  139. flex: 1,
  140. },
  141. })