2018-01-30 11:53:12 -05:00
|
|
|
const bindAll = require('lodash.bindall');
|
|
|
|
const classNames = require('classnames');
|
|
|
|
const Formsy = require('formsy-react');
|
|
|
|
const omit = require('lodash.omit');
|
|
|
|
const PropTypes = require('prop-types');
|
|
|
|
const React = require('react');
|
2016-05-12 17:42:04 -04:00
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
const validations = require('./validations.jsx').validations;
|
|
|
|
|
|
|
|
for (const validation in validations) {
|
2016-05-12 17:42:04 -04:00
|
|
|
Formsy.addValidationRule(validation, validations[validation]);
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
class Form extends React.Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
bindAll(this, [
|
|
|
|
'handleChange'
|
|
|
|
]);
|
|
|
|
this.state = {
|
2016-07-01 11:43:18 -04:00
|
|
|
allValues: {}
|
|
|
|
};
|
2018-01-30 11:53:12 -05:00
|
|
|
}
|
|
|
|
handleChange (currentValues, isChanged) {
|
2016-07-01 11:43:18 -04:00
|
|
|
this.setState({allValues: omit(currentValues, 'all')});
|
|
|
|
this.props.onChange(currentValues, isChanged);
|
2018-01-30 11:53:12 -05:00
|
|
|
}
|
|
|
|
render () {
|
2016-05-12 17:42:04 -04:00
|
|
|
return (
|
2018-03-08 10:04:58 -05:00
|
|
|
<Formsy.default
|
2018-01-30 11:53:12 -05:00
|
|
|
className={classNames('form', this.props.className)}
|
|
|
|
ref={form => {
|
|
|
|
this.formsy = form;
|
|
|
|
}}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
{...this.props}
|
|
|
|
>
|
|
|
|
{React.Children.map(this.props.children, child => {
|
2018-03-08 10:04:58 -05:00
|
|
|
if (!child) return null;
|
2016-07-05 12:05:54 -04:00
|
|
|
if (child.props.name === 'all') {
|
|
|
|
return React.cloneElement(child, {value: this.state.allValues});
|
|
|
|
}
|
2018-01-30 11:53:12 -05:00
|
|
|
return child;
|
|
|
|
})}
|
2018-03-08 10:04:58 -05:00
|
|
|
</Formsy.default>
|
2016-05-12 17:42:04 -04:00
|
|
|
);
|
|
|
|
}
|
2018-01-30 11:53:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Form.propTypes = {
|
|
|
|
children: PropTypes.node,
|
|
|
|
className: PropTypes.string,
|
|
|
|
onChange: PropTypes.func
|
|
|
|
};
|
|
|
|
|
|
|
|
Form.defaultProps = {
|
|
|
|
noValidate: true,
|
|
|
|
onChange: function () {}
|
|
|
|
};
|
2016-05-12 17:42:04 -04:00
|
|
|
|
|
|
|
module.exports = Form;
|