material-table icon indicating copy to clipboard operation
material-table copied to clipboard

How to get all initialEditValue(s) in Custom EditRow on add new row

Open jdavidferreira opened this issue 6 years ago • 1 comments

I implemented formik with material-ui following this #433. I just made a minor modification since the formik was upgraded to v2 and initialValues are required for it to work.

Take this demo project https://codesandbox.io/s/material-table-formik-0mm9y as reference.

I need to get an object with the row data values to pass it to <Formik/> initialValue prop. Currently i get them from EditRow props.data, like this:

export const FormikMTableEditRow = ({ onEditingApproved, ...props }) => {
  console.log("EditRow props =", props);
  const initialValues = props.data || {}; //<<< HERE

  return (
    <Formik
      initialValues={initialValues}
      onSubmit={newData => {
        delete newData.tableData;
        onEditingApproved(props.mode, newData, props.data);
      }}
    >
      {({ values, errors, submitForm }) => {
        console.log("values =", values);
        console.log("errors =", errors);

        return <MTableEditRow {...props} onEditingApproved={submitForm} />;
      }}
    </Formik>
  );
};

But props.data just return values on editing a row, but on add new row it returns undefined, so I can't get the initialEditValues easily. I can see those values are being obtained here: https://github.com/mbrn/material-table/blob/3031eab70105df8d88c17984ca5cea878a9eae3f/src/components/m-table-edit-row.js#L21-L26 but looks like its only for editing.

Is there a way to get those values? Or do I have to get them manually by doing the same as in MTableEditRow.createRowData() traversing props.columns.

jdavidferreira avatar Feb 15 '20 23:02 jdavidferreira

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.

stale[bot] avatar Jul 05 '20 22:07 stale[bot]