MUI Editors
Just like you can use MUI to render filters, you can also use it to render editors.
Basic Example
Let's pick apart the code above:
- We're using the
materialAdapter
to render the grid. - We're using the
createEditBehavior
to enable editing. This is required for theenableEdit
option to work. - We're using the
createFilterBehavior
to enable filtering. This is required for dropdown editors to work, because they use the same distinct value detection logic as the filter behavior. - We're using the
TextInputEditor
to render thefirstName
andlastName
columns. - We're using the
SelectEditor
to render thestateCode
anddepartment
columns. - We're using the
enableFocusCellHighlight
option to highlight the cell that has focus. - We're using the
enableFilters
option to disable filtering (even though we're using thecreateFilterBehavior
behavior - because we just need the distinct value detection logic) - We're using the
editStartMode
option to specify when editing should start. In this case, we're usingEditStartMode.Click
for thefirstName
andlastName
columns andEditStartMode.DoubleClick
for thestateCode
anddepartment
columns. (There is also Excel-like editing, but we'll cover that in a later section.) - We're using the
editorRenderer
option to specify the editor - this ensure you only bundle the editors you need, (although the editors are very small, so it's not a big deal if you bundle them all).
You can find the source code for the All the editors in the ReactDataGrid GitHub repo. Note, the code for the regular editors is in the editors
folder, is the EXACT same code as the MUI editors, because the swap of the MUI components happens in the adapter, so the editors, filters, and renderers all just work with the MUI adapter.
MUI Custom Editor
Lets use the MUI Switch control to render a custom editor.
The code above is very similar to the previous example, but we're using a custom editor for the isActive
column. If you look at the code for the custom editor, you'll see that it's very similar to the editors in the ReactDataGrid GitHub repo
The grid does not automatically save changes. It will not modify your data. You need to save the changes yourself. The api
object has a getChanges
method that returns an array of changes. You can then save the changes to your data however you want.
Adding a custom MUI editor
As you can see from the previous example, it's very easy to add a custom editor. You just need to create a component that renders the editor and pass it to the editorRenderer
option. The editor component will receive the following props:
node
- The node for the cell being edited. The node is an object of type VirtualTreeNode, you can see the documentation for it here. But in a nutshell, this will give you access to all the properties of the grid.
Let's look at how we can use the excellent AutoComplete control from MUI to render a custom editor. We will also use a MUI dialog to render the editor.