Skip to content

Instantly share code, notes, and snippets.

@javereec
Forked from syllog1sm/form.jsx
Last active August 29, 2015 14:27
Show Gist options
  • Save javereec/bfc1b90f6b37fb28f72d to your computer and use it in GitHub Desktop.
Save javereec/bfc1b90f6b37fb28f72d to your computer and use it in GitHub Desktop.
react-bootstrap Form component
/** @jsx React.DOM */
import React from './react-es6';
import classSet from './react-es6/lib/cx';
import BootstrapMixin from './BootstrapMixin';
var Form = React.createClass({
mixins: [BootstrapMixin],
propTypes: {
onSubmit: React.PropTypes.func,
},
getDefaultProps: function() {
return {bsClass: "form"};
},
onSubmit: function(e) {
e.preventDefault();
if (e.target.type == 'submit')
var v = this.getValues();
this.props.onSubmit(v.values, v.checked, v.validation);
},
render: function(){
var className = "form";
return this.transferPropsTo(
<form onClick={this.onSubmit} className="form" role="form" action="#">
{this.props.children}
</form>
);
},
getValues: function() {
var values = {};
var checked = {};
var validation = {};
var child;
for (var i = 0; i < this.props.children.length; i++) {
child = this.props.children[i];
if (child.getValue)
values[child.props.name] = child.getValue();
if (child.getChecked)
checked[child.props.name] = child.getChecked();
if (child.validationState) {
validation[child.props.name] = child.validationState();
}
}
return {
'values': values,
'checked': checked,
'validation': validation
};
}
});
export default = Form;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment