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
materialAdapterto render the grid. - We're using the
createEditBehaviorto enable editing. This is required for theenableEditoption to work. - We're using the
createFilterBehaviorto 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
TextInputEditorto render thefirstNameandlastNamecolumns. - We're using the
SelectEditorto render thestateCodeanddepartmentcolumns. - We're using the
enableFocusCellHighlightoption to highlight the cell that has focus. - We're using the
enableFiltersoption to disable filtering (even though we're using thecreateFilterBehaviorbehavior - because we just need the distinct value detection logic) - We're using the
editStartModeoption to specify when editing should start. In this case, we're usingEditStartMode.Clickfor thefirstNameandlastNamecolumns andEditStartMode.DoubleClickfor thestateCodeanddepartmentcolumns. (There is also Excel-like editing, but we'll cover that in a later section.) - We're using the
editorRendereroption 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.