UNPKG

@cosva-lab/form-builder

Version:
76 lines (73 loc) 4.14 kB
import { __extends, __assign, __decorate } from '../../_virtual/_tslib.js'; import React from 'react'; import clsx from 'clsx'; import { observer } from 'mobx-react'; import { intercept } from 'mobx'; import TextField from '@mui/material/TextField'; import FormControl from '@mui/material/FormControl'; import FormHelperText from '@mui/material/FormHelperText'; import { isEmpty } from '../../utils/isEmpty.js'; import { TransformLabel } from '../../utils/TransformLabel.js'; import { RenderErrorsDefault } from '../../RenderErrorsDefault.js'; import classes from './Input.module.scss.js'; var Input = /** @class */ (function (_super) { __extends(Input, _super); function Input(props) { var _this = _super.call(this, props) || this; _this.errors = []; _this.animation = true; _this.getProps = function (props) { return (__assign({}, props.field)); }; _this.getLastProps = function () { return _this.getProps(_this.props); }; _this.state = { type: props.type }; return _this; } Input.prototype.componentDidMount = function () { var _this = this; var field = this.props.field; intercept(field, 'errors', function (change) { _this.animation = true; return change; }); }; Input.prototype.render = function () { var _this = this; var _a = this.props; _a.onChangeField; var field = _a.field; var ns = field.ns, label = field.label, name = field.name, disabled = field.disabled, _b = field.fullWidth, fullWidth = _b === void 0 ? true : _b, errors = field.errors, autoComplete = field.autoComplete, InputProps = field.InputProps, textFieldProps = field.textFieldProps, value = field.value, RenderErrors = field.renderErrors; var type = this.state.type; var errorsNode = errors && ((RenderErrors && React.createElement(RenderErrors, __assign({}, { errors: errors, field: field }))) || (React.createElement(RenderErrorsDefault, __assign({}, { errors: errors, field: field })))); return (React.createElement(FormControl, __assign({}, { fullWidth: fullWidth }, { className: classes.formControl, variant: "outlined" }), React.createElement(TextField, __assign({ label: React.createElement(TransformLabel, __assign({}, { label: label, ns: ns, name: name })), inputRef: function (element) { return (field.inputRef = element); }, error: !isEmpty(errors), InputProps: typeof InputProps === 'function' ? InputProps({ type: type, field: field, changeType: function (type, callback) { if (type !== _this.state.type) _this.setState({ type: type }, callback); }, }) : InputProps, InputLabelProps: { shrink: type === 'date' ? true : undefined, classes: { root: clsx(classes.InputLabelProps, classes.widthNormal), }, }, onChange: function (e) { var onChange = field.onChange; var value = e.target.value; if (onChange) onChange({ name: field.name, value: value, field: field }, e); else field.setValue(value); }, onBlur: function () { var field = _this.props.field; field && field.markAsTouched(); }, name: name.toString() }, __assign(__assign({}, textFieldProps), { type: type, value: value, disabled: disabled, autoComplete: autoComplete }))), errorsNode && (React.createElement(FormHelperText, { component: "div", error: true, className: classes.formHelperTextPropsRoot }, errorsNode)))); }; Input = __decorate([ observer ], Input); return Input; }(React.Component)); export { Input, Input as default }; //# sourceMappingURL=index.js.map