@vs-form/vs-form
Version:
A schema-based form generator component for React using material-ui
107 lines (101 loc) • 4.33 kB
JavaScript
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = require('react');
var React__default = _interopDefault(React);
require('lodash/capitalize');
require('lodash/cloneDeep');
require('lodash/get');
require('lodash/has');
require('lodash/isArray');
require('lodash/isDate');
require('lodash/isBoolean');
require('lodash/isEmpty');
require('lodash/isFunction');
require('lodash/isInteger');
require('lodash/isNull');
require('lodash/isNumber');
require('lodash/isObject');
require('lodash/isPlainObject');
require('lodash/isRegExp');
require('lodash/isString');
var isUndefined = _interopDefault(require('lodash/isUndefined'));
require('lodash/merge');
require('lodash/set');
require('lodash/toInteger');
require('lodash/toNumber');
require('lodash/trimEnd');
require('lodash/uniq');
require('lodash/debounce');
require('lodash/throttle');
var FormControl = _interopDefault(require('@material-ui/core/FormControl'));
var FormHelperText = _interopDefault(require('@material-ui/core/FormHelperText'));
var FormLabel = _interopDefault(require('@material-ui/core/FormLabel'));
class VsBaseDataComponent extends React.Component {
constructor(props) {
super(props);
this.updateValue = (value, schemaValue) => {
this.setState({ value });
const _schemaValue = !isUndefined(schemaValue) ? schemaValue : value;
this.updateSchemaValue(_schemaValue);
};
this.getSchemaValue = () => {
this.props.schemaManager.getSchemaValue(this.props.comp, this.props.arrayId);
};
this.updateSchemaValue = (value) => {
this.props.schemaManager.updateSchemaValue(this.props.comp, value, this.props.arrayId);
const error = this.errorsToString(this.props.schemaManager.getValueErrorsComp(this.props.comp, this.props.arrayId));
this.setError(error);
};
this.errorsToString = (errors) => {
return errors.map(e => e.error).join('\n');
};
this.setError = (error) => {
this.setState({ error });
};
this.state = {
value: this.props.schemaManager.getSchemaValue(this.props.comp, this.props.arrayId),
error: this.errorsToString(this.props.schemaManager.getValueErrorsComp(this.props.comp, this.props.arrayId)),
};
}
render() {
const { state, updateValue, getSchemaValue, setError } = this;
return this.props.children({ state, updateValue, getSchemaValue, setError, ...this.props });
}
}
class VsBaseFormControl extends React.Component {
constructor(props) {
super(props);
this.FormControlProps = {};
this.FormHelperTextProps = {};
this.FormLabelProps = {};
this.renderComp = (dataProps) => {
const comp = this.props.comp;
const formhelpertext = dataProps.state.error || this.props.comp.hint || '';
return (React.createElement(FormControl, Object.assign({ error: !!dataProps.state.error }, this.FormControlProps),
this.props.showLabel && React.createElement(FormLabel, Object.assign({}, this.FormLabelProps), comp.label),
this.props.children({ ...dataProps }),
formhelpertext && React.createElement(FormHelperText, Object.assign({ error: !!dataProps.state.error }, this.FormHelperTextProps), formhelpertext)));
};
this.initProps();
}
render() {
return (React.createElement(VsBaseDataComponent, Object.assign({}, this.props), this.renderComp));
}
initProps() {
const { FormControlProps, FormHelperTextProps, FormLabelProps } = this.props.comp.props;
if (FormControlProps) {
this.FormControlProps = FormControlProps;
}
if (FormHelperTextProps) {
this.FormHelperTextProps = FormHelperTextProps;
}
if (FormLabelProps) {
this.FormLabelProps = FormLabelProps;
}
if (isUndefined(this.FormControlProps.fullWidth)) {
this.FormControlProps.fullWidth = true;
}
}
}
exports.VsBaseDataComponent = VsBaseDataComponent;
exports.VsBaseFormControl = VsBaseFormControl;