@novin-dev/formalite
Version:
Generate MUI form with few line of code
40 lines (37 loc) • 1.93 kB
JavaScript
import { __rest, __assign } from 'tslib';
import React from 'react';
import TextField from '@mui/material/TextField';
import { checkIsRequired, getData, checkRegex, showErrorMessage } from '../../config/utils.js';
import { TextViewSkeleton } from './SkeletonBase.js';
var TextFieldBase = function (props) {
var name = props.name, formik = props.formik, loading = props.loading, validationSchema = props.validationSchema, translator = props.translator, mustRegex = props.mustRegex, onChange = props.onChange, otherProps = __rest(props, ["name", "formik", "loading", "validationSchema", "translator", "mustRegex", "onChange"]);
if (loading) {
return React.createElement(TextViewSkeleton, { hasHelper: !!otherProps.helperText });
}
var handleChange = function (e) {
var value = e.target.value;
if (!checkRegex({ regexs: mustRegex, text: value })) {
return;
}
formik.setFieldValue(name, value);
if (onChange) {
try {
onChange(value, e);
}
catch (error) {
showErrorMessage(error);
}
}
};
return (React.createElement(TextField, __assign({ fullWidth: true, name: name, required: checkIsRequired({
schema: validationSchema,
formikValues: formik.values,
key: name,
}), error: getData({ source: formik.touched, key: name }) &&
Boolean(getData({ source: formik.errors, key: name })), value: getData({ source: formik.values, key: name }), onChange: handleChange }, otherProps, { helperText: getData({ source: formik.touched, key: name }) &&
getData({ source: formik.errors, key: name })
? translator(getData({ source: formik.errors, key: name }))
: otherProps.helperText })));
};
export { TextFieldBase };
//# sourceMappingURL=TextFieldBase.js.map