group.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React from 'react'
  2. import {
  3. View,
  4. Text,
  5. TouchableOpacity,
  6. StyleSheet,
  7. ListView,
  8. } from 'react-native'
  9. import PropTypes from 'prop-types'
  10. import Checkbox from './index'
  11. export default class CheckboxGroup extends React.Component {
  12. constructor(props) {
  13. super(props)
  14. this.ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 })
  15. this.state = {
  16. CheckboxData: props.CheckboxData,
  17. totalCheck: false,
  18. }
  19. }
  20. static PropTypes = {
  21. isRight: PropTypes.bool,
  22. size: PropTypes.number,
  23. checkBoxColor: PropTypes.string,
  24. checkKey: PropTypes.string,
  25. renderLabel: PropTypes.func,
  26. CheckboxData: PropTypes.array,
  27. }
  28. static defaultProps = {
  29. CheckboxData: [],
  30. }
  31. singleCheck(val, bool) {
  32. val[this.props.checkKey] = bool
  33. let checkArr = []
  34. for (let data of this.state.CheckboxData) {
  35. checkArr.push(data[this.props.checkKey])
  36. }
  37. if (checkArr.indexOf(false) == -1) {
  38. this.setState({ totalCheck: true })
  39. } else {
  40. this.setState({ totalCheck: false })
  41. }
  42. }
  43. renderRow(data) {
  44. return (
  45. <Checkbox
  46. checked={data[this.props.checkKey]}
  47. isRight={this.props.isRight}
  48. size={this.props.size}
  49. checkBoxColor={this.props.checkBoxColor}
  50. onChange={bool => this.singleCheck(data, bool)}
  51. label={this.props.renderLabel(data)}
  52. />
  53. )
  54. }
  55. totalChecked(bool) {
  56. const mock = this.state.CheckboxData
  57. for (let val of mock) {
  58. if (bool) {
  59. val[this.props.checkKey] = true
  60. } else {
  61. val[this.props.checkKey] = false
  62. }
  63. }
  64. this.setState({
  65. CheckboxData: mock,
  66. totalCheck: !this.state.totalCheck,
  67. })
  68. }
  69. render() {
  70. return (
  71. <View>
  72. <ListView
  73. dataSource={this.ds.cloneWithRows(this.props.CheckboxData)}
  74. renderRow={this.renderRow.bind(this)}
  75. />
  76. <Checkbox
  77. checked={this.state.totalCheck}
  78. onChange={bool => this.totalChecked(bool)}
  79. />
  80. </View>
  81. )
  82. }
  83. }
  84. const styles = StyleSheet.create({
  85. container: {
  86. flexDirection: 'row',
  87. },
  88. checkbox: {
  89. justifyContent: 'center',
  90. alignItems: 'center',
  91. },
  92. text: {
  93. flex: 1,
  94. },
  95. })