Vulcan/packages/vulcan-forms/lib/components/FormNestedObject.jsx

75 lines
2.1 KiB
React
Raw Normal View History

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
2018-11-14 11:38:41 +01:00
import { registerComponent, mergeWithComponents } from 'meteor/vulcan:core';
// Replaceable layout
const FormNestedObjectLayout = ({ hasErrors, label, content }) => (
<div
className={`form-group row form-nested ${hasErrors ? 'input-error' : ''}`}
>
<label className="control-label col-sm-3">{label}</label>
<div className="col-sm-9">{content}</div>
</div>
);
FormNestedObjectLayout.propTypes = {
hasErrors: PropTypes.bool,
label: PropTypes.node,
content: PropTypes.node
};
registerComponent({
name: 'FormNestedObjectLayout',
component: FormNestedObjectLayout
});
class FormNestedObject extends PureComponent {
2018-09-16 11:48:56 +09:00
render() {
const FormComponents = mergeWithComponents(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 (
<FormComponents.FormNestedObjectLayout
hasErros={hasErrors}
label={this.props.label}
content={[
2018-10-23 15:02:57 +02:00
<FormComponents.FormNestedItem
key="form-nested-item"
2018-10-23 15:02:57 +02:00
{...properties}
path={`${this.props.path}`}
/>,
hasErrors ? (
<FormComponents.FieldErrors
key="form-nested-errors"
errors={nestedObjectErrors}
/>
) : null
]}
/>
2018-09-16 11:48:56 +09:00
);
}
}
FormNestedObject.propTypes = {
2018-09-16 11:48:56 +09:00
currentValues: PropTypes.object,
path: PropTypes.string,
label: PropTypes.string,
errors: PropTypes.array.isRequired,
formComponents: PropTypes.object
};
2018-09-16 11:48:56 +09:00
module.exports = FormNestedObject;
registerComponent('FormNestedObject', FormNestedObject);