Skip to content

Instantly share code, notes, and snippets.

@matsilva
Last active September 10, 2015 16:44
Show Gist options
  • Save matsilva/f2a0965ee6a3fe277460 to your computer and use it in GitHub Desktop.
Save matsilva/f2a0965ee6a3fe277460 to your computer and use it in GitHub Desktop.
Checkbox in React
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;
@matsilva
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment