scratch-www/src/components/forms/form.jsx

48 lines
1.4 KiB
React
Raw Normal View History

2018-01-30 09:54:45 -05:00
var classNames = require('classnames');
var Formsy = require('formsy-react');
var omit = require('lodash.omit');
var React = require('react');
var validations = require('./validations.jsx').validations;
2018-01-30 09:54:45 -05:00
for (var validation in validations) {
Formsy.addValidationRule(validation, validations[validation]);
}
2018-01-30 09:54:45 -05:00
var Form = React.createClass({
getDefaultProps: function () {
return {
noValidate: true,
onChange: function () {}
};
},
getInitialState: function () {
return {
allValues: {}
};
2018-01-30 09:54:45 -05:00
},
onChange: function (currentValues, isChanged) {
this.setState({allValues: omit(currentValues, 'all')});
this.props.onChange(currentValues, isChanged);
2018-01-30 09:54:45 -05:00
},
render: function () {
var classes = classNames(
'form',
this.props.className
);
return (
2018-01-30 09:54:45 -05:00
<Formsy.Form {... this.props} className={classes} ref="formsy" onChange={this.onChange}>
{React.Children.map(this.props.children, function (child) {
if (!child) return child;
if (child.props.name === 'all') {
return React.cloneElement(child, {value: this.state.allValues});
2018-01-30 09:54:45 -05:00
} else {
return child;
}
2018-01-30 09:54:45 -05:00
}.bind(this))}
2016-06-23 15:10:51 -04:00
</Formsy.Form>
);
}
2018-01-30 09:54:45 -05:00
});
module.exports = Form;