mirror of
https://github.com/vale981/Vulcan
synced 2025-03-09 20:16:39 -04:00
78 lines
2.4 KiB
JavaScript
78 lines
2.4 KiB
JavaScript
import React, { PureComponent } from 'react';
|
||
import PropTypes from 'prop-types';
|
||
import { Components, registerComponent } from 'meteor/vulcan:core';
|
||
import Button from 'react-bootstrap/lib/Button';
|
||
|
||
const FormNestedItem = ({ isDeleted, nestedFields, name, subDocument, removeItem, ...props }) => {
|
||
return (
|
||
<div className={`form-nested-item ${isDeleted ? 'form-nested-item-deleted' : ''}`}>
|
||
<div className="form-nested-item-inner">
|
||
{nestedFields.map((field, i) => {
|
||
// note: default value to '' to avoid uncontrolled component error
|
||
const value = subDocument && subDocument[field.name] || '';
|
||
return <Components.FormComponent key={i} {...props} {...field} value={value} />;
|
||
})}
|
||
</div>
|
||
<div className="form-nested-item-remove">
|
||
<Button
|
||
bsStyle="danger"
|
||
onClick={() => {
|
||
removeItem(name);
|
||
}}
|
||
>
|
||
✖️
|
||
</Button>
|
||
</div>
|
||
<div className="form-nested-item-deleted-overlay"/>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
registerComponent('FormNestedItem', FormNestedItem);
|
||
|
||
class FormNested extends PureComponent {
|
||
|
||
addItem = () => {
|
||
this.props.updateCurrentValues({[`${this.props.name}.${this.props.value.length}`] : {}});
|
||
};
|
||
|
||
removeItem = index => {
|
||
this.props.updateCurrentValues({[`${this.props.name}.${index}`] : null});
|
||
};
|
||
|
||
/*
|
||
|
||
Go through this.context.deletedValues and see if any value matches both the current field
|
||
and the given index (ex: if we want to know if the second address is deleted, we
|
||
look for the presence of 'addresses.1')
|
||
*/
|
||
isDeleted = index => {
|
||
return this.context.deletedValues.includes(`${this.props.name}.${index}`);
|
||
};
|
||
|
||
render() {
|
||
return (
|
||
<div className="form-group row form-nested">
|
||
<label className="control-label col-sm-3">{this.props.label}</label>
|
||
<div className="col-sm-9">
|
||
{this.props.value &&
|
||
this.props.value.map((subDocument, i) => (
|
||
!this.isDeleted(i) && <FormNestedItem key={i} itemIndex={i} {...this.props} subDocument={subDocument} removeItem={() => {this.removeItem(i)}} />
|
||
))}
|
||
<Button
|
||
bsStyle="success"
|
||
onClick={this.addItem}
|
||
>
|
||
➕
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
}
|
||
|
||
FormNested.contextTypes = {
|
||
deletedValues: PropTypes.array
|
||
}
|
||
|
||
registerComponent('FormNested', FormNested);
|