mirror of
https://github.com/vale981/Vulcan
synced 2025-03-08 19:11:38 -05:00

- Moved UI portions of FormComponent to FormComponentInner.jsx in vulcan-ui-bootstrap - Added user alert when the user navigates to another route while there are unsaved changed in the form (disabled by default) - Added setting forms.warnUnsavedChanges and SmartForm property warnUnsavedChanges to enable user alert - Added optional Revert button in FormSubmits to allow the user to discard any changes to the form; this is activated by passing a "revertCallback" property to SmartForm (which can be empty: () => {}) - Added two functions that form components can access in the child context: refetchForm() to refetch the document from the database (in case it was updated by a background process), isChanged() to determine if there are any unsaved changes - For any phrases I have added to en_US.js I also added it to es_ES.js and fr_FR.js with the comment // TODO: translate - Updated Form.clearForm and Form.mutationSuccessCallback so that the user can continue working on the document after submitting it - The form now scrolls the flash message into view when a submit results in errors - Fixed bugs in FormComponent.shouldComponentUpdate() and Form.getDocument() - Fixed bug in FormComponent.handleChange() - number fields could not be cleared, only set to 0 - Fixed a bug in FormComponent.getValue() - it returned the initial value of a checkbox even after it was set to false, and a number even after it was set to 0
126 lines
3.6 KiB
JavaScript
126 lines
3.6 KiB
JavaScript
import React, { PureComponent } from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { Components } from 'meteor/vulcan:core';
|
|
import classNames from 'classnames';
|
|
import { registerComponent } from 'meteor/vulcan:core';
|
|
|
|
class FormGroup extends PureComponent {
|
|
constructor(props) {
|
|
super(props);
|
|
this.toggle = this.toggle.bind(this);
|
|
this.renderHeading = this.renderHeading.bind(this);
|
|
this.state = {
|
|
collapsed: props.startCollapsed || false,
|
|
};
|
|
}
|
|
|
|
toggle() {
|
|
this.setState({
|
|
collapsed: !this.state.collapsed,
|
|
});
|
|
}
|
|
|
|
renderHeading() {
|
|
return (
|
|
<div className="form-section-heading" onClick={this.toggle}>
|
|
<h3 className="form-section-heading-title">{this.props.label}</h3>
|
|
<span className="form-section-heading-toggle">
|
|
{this.state.collapsed ? <Components.IconRight height={16} width={16}/> : <Components.IconDown height={16} width={16} />}
|
|
</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// if at least one of the fields in the group has an error, the group as a whole has an error
|
|
hasErrors = () => _.some(this.props.fields, field => {
|
|
return !!this.props.errors.filter(error => error.path === field.path).length
|
|
});
|
|
|
|
render() {
|
|
|
|
return (
|
|
<div className="form-section">
|
|
{this.props.name === 'default' ? null : this.renderHeading()}
|
|
<div className={classNames({ 'form-section-collapsed': this.state.collapsed && !this.hasErrors() })}>
|
|
{this.props.fields.map(field => (
|
|
<Components.FormComponent
|
|
key={field.name}
|
|
{...field}
|
|
errors={this.props.errors}
|
|
throwError={this.props.throwError}
|
|
currentValues={this.props.currentValues}
|
|
updateCurrentValues={this.props.updateCurrentValues}
|
|
deletedValues={this.props.deletedValues}
|
|
addToDeletedValues={this.props.addToDeletedValues}
|
|
clearFieldErrors={this.props.clearFieldErrors}
|
|
formType={this.props.formType}
|
|
currentUser={this.props.currentUser}
|
|
/>
|
|
))}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
|
|
FormGroup.propTypes = {
|
|
name: PropTypes.string,
|
|
label: PropTypes.string,
|
|
order: PropTypes.number,
|
|
fields: PropTypes.array.isRequired,
|
|
errors: PropTypes.array.isRequired,
|
|
throwError: PropTypes.func.isRequired,
|
|
currentValues: PropTypes.object.isRequired,
|
|
updateCurrentValues: PropTypes.func.isRequired,
|
|
deletedValues: PropTypes.array.isRequired,
|
|
addToDeletedValues: PropTypes.func.isRequired,
|
|
clearFieldErrors: PropTypes.func.isRequired,
|
|
formType: PropTypes.string.isRequired,
|
|
currentUser: PropTypes.object,
|
|
};
|
|
|
|
registerComponent('FormGroup', FormGroup);
|
|
|
|
const IconRight = ({ width = 24, height = 24 }) => (
|
|
<svg
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
width={width}
|
|
height={height}
|
|
viewBox="0 0 24 24"
|
|
>
|
|
<polyline
|
|
fill="none"
|
|
stroke="#000"
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeMiterlimit="10"
|
|
points="5.5,23.5 18.5,12 5.5,0.5"
|
|
id="Outline_Icons"
|
|
/>
|
|
<rect fill="none" width="24" height="24" id="Frames-24px" />
|
|
</svg>
|
|
);
|
|
|
|
registerComponent('IconRight', IconRight);
|
|
|
|
const IconDown = ({ width = 24, height = 24 }) => (
|
|
<svg
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
width={width}
|
|
height={height}
|
|
viewBox="0 0 24 24"
|
|
>
|
|
<polyline
|
|
fill="none"
|
|
stroke="#000"
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeMiterlimit="10"
|
|
points="0.501,5.5 12.001,18.5 23.501,5.5"
|
|
id="Outline_Icons"
|
|
/>
|
|
<rect fill="none" width="24" height="24" id="Frames-24px" />
|
|
</svg>
|
|
);
|
|
|
|
registerComponent('IconDown', IconDown);
|