2018-07-26 17:07:48 +02:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Components, registerComponent } from 'meteor/vulcan:core';
|
|
|
|
|
|
|
|
class FormNestedObject extends PureComponent {
|
2018-09-16 11:48:56 +09:00
|
|
|
render() {
|
2018-09-23 08:13:09 +09:00
|
|
|
const FormComponents = this.props.formComponents;
|
2018-09-16 11:48:56 +09:00
|
|
|
//const value = this.getCurrentValue()
|
|
|
|
// do not pass FormNested's own value, input and inputProperties props down
|
2018-10-23 15:02:57 +02:00
|
|
|
const properties = _.omit(
|
|
|
|
this.props,
|
|
|
|
'value',
|
|
|
|
'input',
|
|
|
|
'inputProperties',
|
|
|
|
'nestedInput'
|
|
|
|
);
|
2018-09-16 11:48:56 +09:00
|
|
|
const { errors } = this.props;
|
|
|
|
// only keep errors specific to the nested array (and not its subfields)
|
2018-10-23 15:02:57 +02:00
|
|
|
const nestedObjectErrors = errors.filter(
|
|
|
|
error => error.path && error.path === this.props.path
|
|
|
|
);
|
2018-09-16 11:48:56 +09:00
|
|
|
const hasErrors = nestedObjectErrors && nestedObjectErrors.length;
|
|
|
|
return (
|
2018-10-23 15:02:57 +02:00
|
|
|
<div
|
|
|
|
className={`form-group row form-nested ${
|
|
|
|
hasErrors ? 'input-error' : ''
|
|
|
|
}`}
|
|
|
|
>
|
2018-09-16 11:48:56 +09:00
|
|
|
<label className="control-label col-sm-3">{this.props.label}</label>
|
|
|
|
<div className="col-sm-9">
|
2018-10-23 15:02:57 +02:00
|
|
|
<FormComponents.FormNestedItem
|
|
|
|
{...properties}
|
|
|
|
path={`${this.props.path}`}
|
|
|
|
/>
|
|
|
|
{hasErrors ? (
|
|
|
|
<FormComponents.FieldErrors errors={nestedObjectErrors} />
|
|
|
|
) : null}
|
2018-09-16 11:48:56 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-07-26 17:07:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FormNestedObject.propTypes = {
|
2018-09-16 11:48:56 +09:00
|
|
|
currentValues: PropTypes.object,
|
|
|
|
path: PropTypes.string,
|
|
|
|
label: PropTypes.string,
|
2018-10-23 15:02:57 +02:00
|
|
|
errors: PropTypes.array.isRequired
|
2018-07-26 17:07:48 +02:00
|
|
|
};
|
|
|
|
|
2018-09-16 11:48:56 +09:00
|
|
|
module.exports = FormNestedObject;
|
2018-07-26 17:07:48 +02:00
|
|
|
|
|
|
|
registerComponent('FormNestedObject', FormNestedObject);
|