Last active
September 10, 2015 16:44
-
-
Save matsilva/f2a0965ee6a3fe277460 to your computer and use it in GitHub Desktop.
Checkbox in React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require('react'); | |
var PropTypes = React.PropTypes; | |
var CheckBox = React.createClass({ | |
propTypes: { | |
addClass: PropTypes.string, | |
text: PropTypes.string, | |
onCheck: PropTypes.func.isRequired, | |
}, | |
getInitialState: function() { | |
return { | |
isChecked: false, | |
}; | |
}, | |
updateCheckState: function () { | |
this.setState({ | |
isChecked: !this.state.isChecked, | |
}) | |
this.props.onCheck(this.state.isChecked); | |
}, | |
render: function() { | |
var checkboxIcon = "fa-square-o"; | |
if (this.state.isChecked) { | |
checkboxIcon = "fa-check-square-o"; | |
} | |
return ( | |
<p className={this.props.addClass}> | |
<a href="javascript:void(0);" onClick={this.updateCheckState}><i className={"fa " + checkboxIcon} /></a> {this.props.text || ""} | |
</p> | |
); | |
} | |
}); | |
module.exports = CheckBox; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: https://jsfiddle.net/05goztut/