Editing Feature Guide
If your tables need full CRUD functionality, you can enable editing features in Material React Table.
There are four visually distinct editing modes to choose from, whether you want to let users edit data in a modal, inline one row at a time, one cell at a time, or just always have editing enabled for every cell.
Relevant Props
# | Prop Name 2 | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| MRT Editing Docs | ||
2 |
| MRT Editing Docs | |||
3 |
| Material UI TextField Props | |||
4 |
| ||||
5 |
| MRT Editing Docs | |||
6 |
| ||||
7 |
| MRT Editing Docs | |||
Relevant Column Options
# | Column Option 2 | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
| ||||
2 |
|
| |||
3 |
| ||||
4 |
| Material UI TextField API | |||
Relevant State Options
Enable Editing
To enable editing, you first need to set the enableEditing
prop to true
.
<MaterialReactTable columns={columns} data={data} enableEditing={true} />
However, this is just the first step. You will need to hook up logic and event listeners, but it depends on which editing mode you want to use.
Editing Modes
Material React Table has four supported editing modes: "modal"
(default), "row"
, "cell"
and "table"
. You can specify which editing mode you want to use by passing the editDisplayMode
prop.
Modal Editing Mode
The "modal"
editing mode opens up a dialog where the user can edit data for one row at a time. No data is saved to the table until the user clicks the save button. Clicking the cancel button clears out any changes that were made on that row.
An onEditingRowSave
callback function prop must be provided where you will get access to the updated row data so that changes can be processed and saved. It is up to you how you handle the data. This function has a exitEditingMode
parameter that must be called in order to exit editing mode upon save. The reason for this is so that you can perform validation checks before letting the modal close.
The
onEditingRowSave
callback function prop includes anexitEditingMode
parameter that must be called in order to exit editing mode upon save. The reason for this is so that you can perform validation checks before letting the modal close.
Actions | First Name | Last Name | Address | City | State |
---|---|---|---|---|---|
Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky | |
Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio | |
Ervin | Reinger | 566 Brakus Inlet | South Linda | West Virginia | |
Brittany | McCullough | 722 Emie Stream | Lincoln | Nebraska | |
Branson | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
1import React, { useMemo, useState } from 'react';2import {3 MaterialReactTable,4 type MRT_TableOptions,5 type MRT_ColumnDef,6} from 'material-react-table';7import { data, type Person } from './makeData';89const Example = () => {10 const columns = useMemo<MRT_ColumnDef<Person>[]>(11 () => [12 //column definitions...35 ],36 [],37 );3839 const [tableData, setTableData] = useState<Person[]>(() => data);4041 const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = async ({42 exitEditingMode,43 row,44 values,45 }) => {46 //if using flat data and simple accessorKeys/ids, you can just do a simple assignment here.47 tableData[row.index] = values;48 //send/receive api updates here49 setTableData([...tableData]);50 exitEditingMode(); //required to exit editing mode51 };5253 return (54 <MaterialReactTable55 columns={columns}56 data={tableData}57 editDisplayMode="modal" //default58 enableEditing59 onEditingRowSave={handleSaveRow}60 />61 );62};6364export default Example;65
Row Editing Mode
The row
editing mode is an inline row editing mode. When edit mode is activated, the row shows the edit components in the data cells. No data is saved to the table until the user clicks the save button. Clicking the cancel button clears out any changes that were made on that row.
You must provide an onEditingRowSave
callback function prop where you will get access to the updated row data so that changes can be processed and saved. It is up to you how you handle the data. This function has a exitEditingMode
parameter that must be called in order to exit editing mode upon save. The reason for this is so that you can perform validation checks before letting the modal close.
The
onEditingRowSave
callback function prop includes anexitEditingMode
parameter that must be called in order to exit editing mode upon save. The reason for this is so that you can perform validation checks before letting the modal close.
Actions | First Name | Last Name | Address | City | State |
---|---|---|---|---|---|
Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky | |
Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio | |
Ervin | Reinger | 566 Brakus Inlet | South Linda | West Virginia | |
Brittany | McCullough | 722 Emie Stream | Lincoln | Nebraska | |
Branson | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
1import React, { useMemo, useState } from 'react';2import {3 MaterialReactTable,4 type MRT_TableOptions,5 type MRT_ColumnDef,6} from 'material-react-table';7import { data, type Person } from './makeData';89const Example = () => {10 const columns = useMemo<MRT_ColumnDef<Person>[]>(11 () => [12 //column definitions...35 ],36 [],37 );3839 const [tableData, setTableData] = useState<Person[]>(() => data);4041 const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = async ({42 exitEditingMode,43 row,44 values,45 }) => {46 //if using flat data and simple accessorKeys/ids, you can just do a simple assignment here.47 tableData[row.index] = values;48 //send/receive api updates here49 setTableData([...tableData]);50 exitEditingMode(); //required to exit editing mode51 };5253 return (54 <MaterialReactTable55 columns={columns}56 data={tableData}57 editDisplayMode="row"58 enableEditing59 onEditingRowSave={handleSaveRow}60 />61 );62};6364export default Example;65
Cell Editing Mode
The cell
editing mode is a bit simpler visually. Uses double-click cells to activate editing mode, but only for that cell.
Then there is a bit of work for you to do to wire up either the onBlur
, onChange
, etc., events yourself in order to save the table data. This can be done in the muiEditTextFieldProps
prop or column definition option.
First Name | Last Name | Address | City | State |
---|---|---|---|---|
Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky |
Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio |
Ervin | Reinger | 566 Brakus Inlet | South Linda | West Virginia |
Brittany | McCullough | 722 Emie Stream | Lincoln | Nebraska |
Branson | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
1import React, { useMemo, useState } from 'react';2import {3 MaterialReactTable,4 type MRT_Cell,5 type MRT_ColumnDef,6} from 'material-react-table';7import { Typography } from '@mui/material';8import { data, type Person } from './makeData';910const Example = () => {11 const columns = useMemo<MRT_ColumnDef<Person>[]>(12 () => [13 //column definitions...35 ],36 [],37 );3839 const [tableData, setTableData] = useState<Person[]>(() => data);4041 const handleSaveCell = (cell: MRT_Cell<Person>, value: any) => {42 //if using flat data and simple accessorKeys/ids, you can just do a simple assignment here43 tableData[cell.row.index][cell.column.id as keyof Person] = value;44 //send/receive api updates here45 setTableData([...tableData]); //re-render with new data46 };4748 return (49 <MaterialReactTable50 columns={columns}51 data={tableData}52 editDisplayMode="cell"53 enableEditing54 muiEditTextFieldProps={({ cell }) => ({55 //onBlur is more efficient, but could use onChange instead56 onBlur: (event) => {57 handleSaveCell(cell, event.target.value);58 },59 })}60 renderBottomToolbarCustomActions={() => (61 <Typography sx={{ fontStyle: 'italic', p: '0 1rem' }} variant="body2">62 Double-Click a Cell to Edit63 </Typography>64 )}65 />66 );67};6869export default Example;70
Table Editing Mode
The table
editing mode is similar to the cell
editing mode, but it simply has all of the data cells in the table become editable all at once.
To save data, you must hook up the onBlur
, onChange
, etc., events yourself. This can be done in the muiEditTextFieldProps
prop or column definition option.
First Name | Last Name | Address | City | State |
---|---|---|---|---|
1import React, { useMemo, useState } from 'react';2import {3 MaterialReactTable,4 type MRT_Cell,5 type MRT_ColumnDef,6} from 'material-react-table';7import { data, type Person } from './makeData';89const Example = () => {10 const columns = useMemo<MRT_ColumnDef<Person>[]>(11 () => [12 //column definitions...34 ],35 [],36 );3738 const [tableData, setTableData] = useState<Person[]>(() => data);3940 const handleSaveCell = (cell: MRT_Cell<Person>, value: any) => {41 //if using flat data and simple accessorKeys/ids, you can just do a simple assignment here42 tableData[cell.row.index][cell.column.id as keyof Person] = value;43 //send/receive api updates here44 setTableData([...tableData]); //re-render with new data45 };4647 return (48 <MaterialReactTable49 columns={columns}50 data={tableData}51 editDisplayMode="table"52 enableEditing53 muiEditTextFieldProps={({ cell }) => ({54 //onBlur is more efficient, but could use onChange instead55 onBlur: (event) => {56 handleSaveCell(cell, event.target.value);57 },58 variant: 'outlined',59 })}60 />61 );62};6364export default Example;65
Customizing Editing Components
You can pass any Material UI TextField Props with the muiEditTextFieldProps
prop.
const columns = [{accessor: 'age',header: 'Age',muiEditTextFieldProps: {required: true,type: 'number',variant: 'outlined',},},];
Add Validation to Editing Components
You can add validation to the editing components by using the muiEditTextFieldProps
events. You can write your validation logic and hook it up to the onBlur
, onChange
, etc., events, then set the error
and helperText
props accordingly.
If you are implementing validation, you may also need to use the onEditingRowCancel
prop to clear the validation error state.
const [validationErrors, setValidationErrors] = useState({});const columns = [{accessor: 'age',header: 'Age',muiEditTextFieldProps: {error: !!validationErrors.age, //highlight mui text field red error colorhelperText: validationErrors.age, //show error message in helper text.required: true,type: 'number',onChange: (event) => {const value = event.target.value;//validation logicif (!value) {setValidationErrors((prev) => ({ ...prev, age: 'Age is required' }));} else if (value < 18) {setValidationErrors({...validationErrors,age: 'Age must be 18 or older',});} else {delete validationErrors.age;setValidationErrors({ ...validationErrors });}},},},];
Use Custom Editing Components
If you need to use a much more complicated Editing component than the built-in textfield, you can specify a custom editing component with the Edit
column definition option.
const columns = [{accessorKey: 'email',header: 'Email',Edit: ({ cell, column, table }) => <Autocomplete />,},];
Customize Actions/Edit Column
You can customize the actions column in a few different ways in the displayColumnDefOptions
prop's 'mrt-row-actions'
section.
<MaterialReactTabledata={data}columns={columns}displayColumnDefOptions={{'mrt-row-actions': {header: 'Edit', //change "Actions" to "Edit"//use a text button instead of a icon buttonCell: ({ row, table }) => (<Button onClick={() => table.setEditingRow(row)}>Edit Customer</Button>),},}}/>
React-Hook-Form Example
TODO