I'm using react-material-table. I'm trying to validate the row edit.
Everything works fine for "native" material column. But I had to use a custom editComponent for one column rendering an Autocomplete.
{
title: "test",
field: "foo",
editComponent: (props) => {
return (
<div>
<Autocomplete
onChange={(e, v) => this.handleFooChange(props.rowData.id, v)}
//onChange={(e,v) => props.onChange(v)}
id="people-company"
options={[{ id: 1, name: "bar" }, { id: 2, name: "foo" }]}
getOptionLabel={option => option.name}
value={props.rowData.foo}
filterSelectedOptions
renderInput={params => (
<TextField
{...params}
variant="outlined"
label="company"
placeholder="company"
margin="normal"
fullWidth
/>
)}
/>
</div>
);
},
My MaterialTable define EditRow and EditField
const FormikMTInput = props => {
return (
<Field name={props.columnDef.field}>
{({ field, form, meta }) => {
const { name } = field;
const { errors, setFieldValue } = form;
//console.log("ShowError", errors);
const showError = !!getIn(errors, name);
return (
<div>
<MTableEditField
{...props}
{...field}
error={showError}
onChange={newValue => setFieldValue(name, newValue)}
/>
{errors[field.name] && (
<div style={{ color: "#f44336" }}>{errors[field.name]}</div>
)}
</div>
);
}}
</Field>
);
};
const MuiTableEditRow = ({ onEditingApproved, ...props }) => {
return(
<Formik
validate={values => {
const errors = {};
if (!values.name) {
errors.name = "Required";
}
if (!values.foo) {
errors.foo = "Required";
}
return errors;
}}
initialValues={props.data}
onSubmit={values => {
if (values) {
if (values.name.length > 2 && values.foo)
onEditingApproved(props.mode, values, props.data);
}
}}
render={({ submitForm }) => (
<MTableEditRow {...props} onEditingApproved={submitForm} />
)}
/>
)};
<MaterialTable
title="Editable Preview"
columns={this.state.columns}
data={this.state.data}
components={{
EditRow: MuiTableEditRow,
EditField: FormikMTInput
}}
/>
FormikMTInput is well fired on "default" columns. It is not the case for the overwrited editComponent. How can I pass errors to this column?
Here the sandbox