Skip to content

Commit eb42475

Browse files
committed
Make checkbox stateless
1 parent 5cc0193 commit eb42475

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ rightTextView | React.PropTypes.element | true | | Custom right TextView
9494
rightTextStyle | Text.propTypes.style | true | | Custom right Text style
9595
checkedImage | React.PropTypes.element | true | Default image | Custom checked Image
9696
unCheckedImage | React.PropTypes.element | true | Default image | Custom unchecked Image
97-
isChecked | React.PropTypes.bool | true | false | Initialization checkbox checked
97+
isChecked | React.PropTypes.bool | false | false | checkbox checked state
9898
onClick | React.PropTypes.func.isRequired | false | | callback function
9999

100100
## Contribution

index.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,10 @@ import {
1414
Image,
1515
Text,
1616
TouchableHighlight
17-
} from 'react-native'
17+
} from 'react-native';
1818

1919

2020
export default class CheckBox extends Component {
21-
constructor(props) {
22-
super(props);
23-
this.state = {
24-
isChecked: this.props.isChecked,
25-
}
26-
}
27-
2821
static propTypes = {
2922
...View.propTypes,
3023
leftText: React.PropTypes.string,
@@ -36,7 +29,7 @@ export default class CheckBox extends Component {
3629
checkedImage: React.PropTypes.element,
3730
unCheckedImage: React.PropTypes.element,
3831
onClick: React.PropTypes.func.isRequired,
39-
isChecked: React.PropTypes.bool
32+
isChecked: React.PropTypes.bool.isRequired
4033

4134
}
4235
static defaultProps = {
@@ -50,44 +43,37 @@ export default class CheckBox extends Component {
5043
if (!this.props.leftText)return null;
5144
return (
5245
<Text style={[styles.leftText, this.props.leftTextStyle]}>{this.props.leftText}</Text>
53-
)
46+
);
5447
}
5548
_renderRight() {
5649
if (this.props.rightTextView)return this.props.rightTextView;
5750
if (!this.props.rightText)return null;
5851
return (
5952
<Text style={[styles.rightText, this.props.rightTextStyle]}>{this.props.rightText}</Text>
60-
)
53+
);
6154
}
6255

6356
_renderImage() {
64-
if (this.state.isChecked) {
57+
if (this.props.isChecked) {
6558
return this.props.checkedImage ? this.props.checkedImage : this.genCheckedImage();
6659
} else {
6760
return this.props.unCheckedImage ? this.props.unCheckedImage : this.genCheckedImage();
6861
}
6962
}
7063

7164
genCheckedImage() {
72-
var source = this.state.isChecked ? require('./img/ic_check_box.png') : require('./img/ic_check_box_outline_blank.png');
65+
var source = this.props.isChecked ? require('./img/ic_check_box.png') : require('./img/ic_check_box_outline_blank.png');
7366

7467
return (
7568
<Image source={source}/>
76-
)
77-
}
78-
79-
onClick() {
80-
this.setState({
81-
isChecked: !this.state.isChecked
82-
})
83-
this.props.onClick();
69+
);
8470
}
8571

8672
render() {
8773
return (
8874
<TouchableHighlight
8975
style={this.props.style}
90-
onPress={()=>this.onClick()}
76+
onPress={this.props.onClick}
9177
underlayColor='transparent'
9278
>
9379
<View style={styles.container}>
@@ -96,7 +82,7 @@ export default class CheckBox extends Component {
9682
{this._renderRight()}
9783
</View>
9884
</TouchableHighlight>
99-
)
85+
);
10086
}
10187
}
10288
const styles = StyleSheet.create({
@@ -111,4 +97,4 @@ const styles = StyleSheet.create({
11197
flex: 1,
11298
marginLeft: 10
11399
}
114-
})
100+
});

0 commit comments

Comments
 (0)