|
1 |
| -/** |
2 |
| -* react-native-check-box |
3 |
| -* Checkbox component for react native, it works on iOS and Android |
4 |
| -* https://github.com/crazycodeboy/react-native-check-box |
5 |
| -* Email:crazycodeboy@gmail.com |
6 |
| -* Blog:http://jiapenghui.com |
7 |
| -* @flow |
8 |
| -*/ |
| 1 | + /** |
| 2 | + * react-native-check-box |
| 3 | + * Checkbox component for react native, it works on iOS and Android |
| 4 | + * https://github.com/crazycodeboy/react-native-check-box |
| 5 | + * Email:crazycodeboy@gmail.com |
| 6 | + * Blog:http://jiapenghui.com |
| 7 | + * @flow |
| 8 | + */ |
9 | 9 |
|
10 | 10 | import React, {Component} from 'react';
|
11 | 11 | import {
|
12 |
| - StyleSheet, |
13 |
| - View, |
14 |
| - Image, |
15 |
| - Text, |
16 |
| - TouchableHighlight |
| 12 | + StyleSheet, |
| 13 | + View, |
| 14 | + Image, |
| 15 | + Text, |
| 16 | + TouchableHighlight |
17 | 17 | } from 'react-native'
|
18 | 18 |
|
19 | 19 |
|
20 | 20 | export default class CheckBox extends Component {
|
21 |
| - constructor(props) { |
22 |
| - super(props); |
23 |
| - this.state = { |
24 |
| - isChecked: this.props.isChecked, |
25 |
| - } |
26 |
| - } |
| 21 | + constructor(props) { |
| 22 | + super(props); |
| 23 | + this.state = { |
| 24 | + isChecked: this.props.isChecked, |
| 25 | + } |
| 26 | + } |
27 | 27 |
|
28 |
| - static propTypes = { |
29 |
| - ...View.propTypes, |
30 |
| - leftText: React.PropTypes.string, |
31 |
| - leftTextView: React.PropTypes.element, |
32 |
| - rightText: React.PropTypes.string, |
33 |
| - leftTextStyle: React.PropTypes.object, |
34 |
| - rightTextView: React.PropTypes.string, |
35 |
| - rightTextStyle: React.PropTypes.object, |
36 |
| - checkedImage: React.PropTypes.element, |
37 |
| - unCheckedImage: React.PropTypes.element, |
38 |
| - onClick: React.PropTypes.func.isRequired, |
39 |
| - isChecked: React.PropTypes.bool |
| 28 | + static propTypes = { |
| 29 | + ...View.propTypes, |
| 30 | + leftText: React.PropTypes.string, |
| 31 | + leftTextView: React.PropTypes.element, |
| 32 | + rightText: React.PropTypes.string, |
| 33 | + leftTextStyle: React.PropTypes.object, |
| 34 | + rightTextView: React.PropTypes.string, |
| 35 | + rightTextStyle: React.PropTypes.object, |
| 36 | + checkedImage: React.PropTypes.element, |
| 37 | + unCheckedImage: React.PropTypes.element, |
| 38 | + onClick: React.PropTypes.func.isRequired, |
| 39 | + isChecked: React.PropTypes.bool |
40 | 40 |
|
41 |
| - } |
42 |
| - static defaultProps = { |
43 |
| - isChecked: false, |
44 |
| - leftTextStyle: {}, |
45 |
| - rightTextStyle: {} |
46 |
| - } |
| 41 | + } |
| 42 | + static defaultProps = { |
| 43 | + isChecked: false, |
| 44 | + leftTextStyle: {}, |
| 45 | + rightTextStyle: {} |
| 46 | + } |
47 | 47 |
|
48 |
| - _renderLeft() { |
49 |
| - if (this.props.leftTextView)return this.props.leftTextView; |
50 |
| - if (!this.props.leftText)return null; |
51 |
| - return ( |
52 |
| - <Text style={[styles.leftText, this.props.leftTextStyle]}>{this.props.leftText}</Text> |
53 |
| - ) |
54 |
| - } |
55 |
| - _renderRight() { |
56 |
| - if (this.props.rightTextView)return this.props.rightTextView; |
57 |
| - if (!this.props.rightText)return null; |
58 |
| - return ( |
59 |
| - <Text style={[styles.rightText, this.props.rightTextStyle]}>{this.props.rightText}</Text> |
60 |
| - ) |
61 |
| - } |
| 48 | + _renderLeft() { |
| 49 | + if (this.props.leftTextView)return this.props.leftTextView; |
| 50 | + if (!this.props.leftText)return null; |
| 51 | + return ( |
| 52 | + <Text style={[styles.leftText, this.props.leftTextStyle]}>{this.props.leftText}</Text> |
| 53 | + ) |
| 54 | + } |
| 55 | + _renderRight() { |
| 56 | + if (this.props.rightTextView)return this.props.rightTextView; |
| 57 | + if (!this.props.rightText)return null; |
| 58 | + return ( |
| 59 | + <Text style={[styles.rightText, this.props.rightTextStyle]}>{this.props.rightText}</Text> |
| 60 | + ) |
| 61 | + } |
62 | 62 |
|
63 |
| - _renderImage() { |
64 |
| - if (this.state.isChecked) { |
65 |
| - return this.props.checkedImage ? this.props.checkedImage : this.genCheckedImage(); |
66 |
| - } else { |
67 |
| - return this.props.unCheckedImage ? this.props.unCheckedImage : this.genCheckedImage(); |
68 |
| - } |
69 |
| - } |
| 63 | + _renderImage() { |
| 64 | + if (this.state.isChecked) { |
| 65 | + return this.props.checkedImage ? this.props.checkedImage : this.genCheckedImage(); |
| 66 | + } else { |
| 67 | + return this.props.unCheckedImage ? this.props.unCheckedImage : this.genCheckedImage(); |
| 68 | + } |
| 69 | + } |
70 | 70 |
|
71 |
| - genCheckedImage() { |
72 |
| - var source = this.state.isChecked ? require('./img/ic_check_box.png') : require('./img/ic_check_box_outline_blank.png'); |
| 71 | + genCheckedImage() { |
| 72 | + var source = this.state.isChecked ? require('./img/ic_check_box.png') : require('./img/ic_check_box_outline_blank.png'); |
73 | 73 |
|
74 |
| - return ( |
75 |
| - <Image source={source}/> |
76 |
| - ) |
77 |
| - } |
| 74 | + return ( |
| 75 | + <Image source={source}/> |
| 76 | + ) |
| 77 | + } |
78 | 78 |
|
79 |
| - onClick() { |
80 |
| - this.setState({ |
81 |
| - isChecked: !this.state.isChecked |
82 |
| - }) |
83 |
| - this.props.onClick(); |
84 |
| - } |
| 79 | + onClick() { |
| 80 | + this.setState({ |
| 81 | + isChecked: !this.state.isChecked |
| 82 | + }) |
| 83 | + this.props.onClick(); |
| 84 | + } |
85 | 85 |
|
86 |
| - render() { |
87 |
| - return ( |
88 |
| - <TouchableHighlight |
89 |
| - style={this.props.style} |
90 |
| - onPress={()=>this.onClick()} |
91 |
| - underlayColor='transparent' |
92 |
| - > |
93 |
| - <View style={styles.container}> |
94 |
| - {this._renderLeft()} |
95 |
| - {this._renderImage()} |
96 |
| - {this._renderRight()} |
97 |
| - </View> |
98 |
| - </TouchableHighlight> |
99 |
| - ) |
100 |
| - } |
| 86 | + render() { |
| 87 | + return ( |
| 88 | + <TouchableHighlight |
| 89 | + style={this.props.style} |
| 90 | + onPress={()=>this.onClick()} |
| 91 | + underlayColor='transparent' |
| 92 | + > |
| 93 | + <View style={styles.container}> |
| 94 | + {this._renderLeft()} |
| 95 | + {this._renderImage()} |
| 96 | + {this._renderRight()} |
| 97 | + </View> |
| 98 | + </TouchableHighlight> |
| 99 | + ) |
| 100 | + } |
101 | 101 | }
|
102 | 102 | const styles = StyleSheet.create({
|
103 |
| - container: { |
104 |
| - flexDirection: 'row', |
105 |
| - alignItems: 'center' |
106 |
| - }, |
107 |
| - leftText: { |
108 |
| - flex: 1, |
109 |
| - }, |
110 |
| - rightText: { |
111 |
| - flex: 1, |
112 |
| - marginLeft: 10 |
113 |
| - } |
| 103 | + container: { |
| 104 | + flexDirection: 'row', |
| 105 | + alignItems: 'center' |
| 106 | + }, |
| 107 | + leftText: { |
| 108 | + flex: 1, |
| 109 | + }, |
| 110 | + rightText: { |
| 111 | + flex: 1, |
| 112 | + marginLeft: 10 |
| 113 | + } |
114 | 114 | })
|
0 commit comments