Rich Tree View - Label editing
Learn how to edit Tree View item labels.
Enable label editing
Use the isItemEditable prop to enable editing.
If set to true, this prop enables label editing on all items as shown in the demo below:
- @mui/x-data-grid
- @mui/x-data-grid-pro
- @mui/x-date-pickers
- @mui/x-date-pickers-pro
- Charts
- Tree View
Limit editing to some items
If you pass a method to isItemEditable, then only the items for which the method returns true will be editable:
- @mui/x-data-grid editable
- @mui/x-data-grid-pro editable
- @mui/x-date-pickers
- @mui/x-date-pickers-pro
- Charts
- Tree View
Limit editing to leaves
You can limit editing to just the leaves of the tree, as shown below:
- @mui/x-data-grid
- @mui/x-data-grid-pro
- @mui/x-date-pickers
- @mui/x-date-pickers-pro
- Charts
- Tree View
Track item label change
Use the onItemLabelChange prop to trigger an action when an item label changes:
No item has been edited yet
- @mui/x-data-grid
- @mui/x-data-grid-pro
- @mui/x-date-pickers
- @mui/x-date-pickers-pro
- Charts
- Tree View
Change the default behavior
By default, blurring a TreeItem saves the new value if there is one.
To modify this behavior, use the slotProps on the TreeItem as shown below:
- @mui/x-data-grid
- @mui/x-data-grid-pro
- @mui/x-date-pickers
- @mui/x-date-pickers-pro
- Charts
- Tree View
Validation
You can override the event handlers of the labelInput and implement a custom validation logic using the interaction methods from useTreeItemUtils():
- @mui/x-data-grid
- @mui/x-data-grid-pro
- @mui/x-date-pickers
- @mui/x-date-pickers-pro
- Charts
- Tree View
Enable editing using only icons
The demo below shows how to completely override the editing behavior and implement it using icons:
- @mui/x-data-grid
- @mui/x-data-grid-pro
- @mui/x-date-pickers
- @mui/x-date-pickers-pro
- Charts
- Tree View
Create a custom labelInput
The demo below shows how to use a different component in the labelInput slot:
- Elena Kim
- Noah Rodriguez
- Maya Patel
- Ethan Lee
- Ava Jones
Imperative API
To use the apiRef object, you need to initialize it using the useRichTreeViewApiRef() or useRichTreeViewProApiRef() hook as follows:
// Community package
const apiRef = useRichTreeViewApiRef();
return <RichTreeView apiRef={apiRef} items={ITEMS} />;
// Pro package
const apiRef = useRichTreeViewProApiRef();
return <RichTreeViewPro apiRef={apiRef} items={ITEMS} />;
When your component first renders, apiRef.current is undefined.
After the initial render, apiRef holds methods to interact imperatively with RichTreeView.
Change the label of an item
Use the updateItemLabel() API method to imperatively update the label of an item.
apiRef.current.updateItemLabel(
// The id of the item to update
itemId,
// The new label of the item
newLabel,
);
- Data Grid
- Date and Time pickers
- Charts
- Tree View
Change edition mode of an item
Use the setEditedItem() API method to set which item is being edited.
apiRef.current.setEditedItem(
// The id of the item to edit, or `null` to exit editing mode
itemId,
);
- Jane Doe
Editing lazy loaded children
To store the updated item labels on your server, use the onItemLabelChange() callback function.
Changes to the label are not automatically updated in the dataSourceCache and must be updated manually.
const handleItemLabelChange = (itemId: TreeViewItemId, newLabel: string) => {
// update your cache here
};
<RichTreeViewPro
items={[]}
onItemLabelChange={handleItemLabelChange}
isItemEditable
dataSource={{
getChildrenCount: (item) => item?.childrenCount as number,
getTreeItems: fetchData,
}}
{...otherProps}
/>;
See lazy loading for more details.
API
See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.