UNPKG

@vs-form/vs-form

Version:

A schema-based form generator component for React using material-ui

101 lines (97 loc) 3.97 kB
import { Component, createElement } from 'react'; import 'lodash/capitalize'; import 'lodash/cloneDeep'; import 'lodash/get'; import 'lodash/has'; import 'lodash/isArray'; import 'lodash/isDate'; import 'lodash/isBoolean'; import 'lodash/isEmpty'; import 'lodash/isFunction'; import 'lodash/isInteger'; import 'lodash/isNull'; import 'lodash/isNumber'; import 'lodash/isObject'; import 'lodash/isPlainObject'; import 'lodash/isRegExp'; import 'lodash/isString'; import isUndefined from 'lodash/isUndefined'; import 'lodash/merge'; import 'lodash/set'; import 'lodash/toInteger'; import 'lodash/toNumber'; import 'lodash/trimEnd'; import 'lodash/uniq'; import 'lodash/debounce'; import 'lodash/throttle'; import FormControl from '@material-ui/core/FormControl'; import FormHelperText from '@material-ui/core/FormHelperText'; import FormLabel from '@material-ui/core/FormLabel'; class VsBaseDataComponent extends 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 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 (createElement(FormControl, Object.assign({ error: !!dataProps.state.error }, this.FormControlProps), this.props.showLabel && createElement(FormLabel, Object.assign({}, this.FormLabelProps), comp.label), this.props.children({ ...dataProps }), formhelpertext && createElement(FormHelperText, Object.assign({ error: !!dataProps.state.error }, this.FormHelperTextProps), formhelpertext))); }; this.initProps(); } render() { return (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; } } } export { VsBaseDataComponent as a, VsBaseFormControl as b };