Open In App

React MUI TreeView API

Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google. 

In this article, we are going to discuss the React MUI TreeView API. The TreeView displays a hierarchical list. It is generally used to display a file system or can be used as a file system navigator. The TreeView contains TreeItems. The API provides a lot of functionality and we are going to learn to implement them.



Import TreeItem API

import TreeView from '@mui/lab/TreeView';
// or
import { TreeView } from '@mui/lab';

Props List: Here is the list of different props used with this component. We can access them and modify them according to our needs.



CSS Rules:

Syntax: Create the TreeView as follows:

<TreeView defaultCollapseIcon={<ExpandMoreIcon />} 
    defaultExpandIcon={<ChevronRightIcon />}>
    <TreeItem nodeId="1" label="Applications">
        <TreeItem nodeId="2" label="Calendar" />
      </TreeItem>
</TreeView>

Installing and Creating React app and adding the MUI dependencies.

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react @emotion/styled @mui/lab @mui/icons-material

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have a  basic TreeView.




import './App.css'
import * as React from 'react'
import TreeView from '@mui/lab/TreeView'
import TreeItem from '@mui/lab/TreeItem'
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'
import ChevronRightIcon from '@mui/icons-material/ChevronRight'
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: 'fit-content',
                    margin: 'auto',
                }}
            >
                <h1
                    style={{
                        color: 'green',
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI TreeView API</strong>
            </div>
  
            <TreeView
                aria-label="Tutorials navigator"
                defaultCollapseIcon={<ExpandMoreIcon />}
                defaultExpandIcon={<ChevronRightIcon />}
                sx={{
                    margin: 'auto',
                    flexGrow: 1,
                    width: 'fit-content',
                }}
            >
                <TreeItem nodeId="1" label="Data Structures">
                    <TreeItem nodeId="2" label="Array" />
                    <TreeItem nodeId="3" label="Max Heap" />
                    <TreeItem nodeId="4" label="Stack" />
                </TreeItem>
                <TreeItem nodeId="5" label="Algorithms">
                    <TreeItem nodeId="10" label="Gready" />
                    <TreeItem nodeId="6" label="Graph">
                        <TreeItem nodeId="8" label="DFS" />
                        <TreeItem nodeId="8" label="BFS" />
                    </TreeItem>
                </TreeItem>
            </TreeView>
        </div>
    )
}
  
export default App

Output:

 

Example 2: In the following example, we will implement expanded and selected fields.




import './App.css'
import * as React from 'react'
import TreeView from '@mui/lab/TreeView'
import TreeItem from '@mui/lab/TreeItem'
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'
import ChevronRightIcon from '@mui/icons-material/ChevronRight'
  
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
function App() {
    const [expanded, setExpanded] = React.useState([])
    const [selected, setSelected] = React.useState([])
  
    const handleToggle = (event, nodeIds) => {
        setExpanded(nodeIds)
    }
  
    const handleSelect = (event, nodeIds) => {
        setSelected(nodeIds)
    }
  
    const handleExpandClick = () => {
        setExpanded((oldExpanded) =>
            oldExpanded.length === 0 ? ['1', '5', '6'] : [],
        )
    }
  
    const handleSelectClick = () => {
        setSelected((oldSelected) =>
            oldSelected.length === 0
                ? ['1', '2', '3', '4', '5', '6', '7', '8', '9']
                : [],
        )
    }
  
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: 'fit-content',
                    margin: 'auto',
                }}
            >
                <h1
                    style={{
                        color: 'green',
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI TreeView API</strong>
            </div>
            <div
                style={{
                    margin: 'auto',
                    width: 'fit-content',
                }}
            >
                <Box sx={{ 
                    height: 270, 
                    flexGrow: 1, 
                    maxWidth: 400, 
                    overflowY: 'auto' 
                }}>
                    <Box sx={{ mb: 1 }}>
                        <Button onClick={handleExpandClick}>
                            {expanded.length === 0 ? 
                                'Expand all' : 'Collapse all'}
                        </Button>
                        <Button onClick={handleSelectClick}>
                            {selected.length === 0 ? 
                                'Select all' : 'Unselect all'}
                        </Button>
                    </Box>
                    <TreeView
                        aria-label="Tutorials navigator"
                        defaultCollapseIcon={<ExpandMoreIcon />}
                        defaultExpandIcon={<ChevronRightIcon />}
                        sx={{
                            margin: 'auto',
                            flexGrow: 1,
                            width: 'fit-content',
                        }}
                        expanded={expanded}
                        selected={selected}
                        onNodeToggle={handleToggle}
                        onNodeSelect={handleSelect}
                        multiSelect
                    >
                        <TreeItem nodeId="1" label="Data Structures">
                            <TreeItem nodeId="2" label="Array" />
                            <TreeItem nodeId="3" label="Max Heap" />
                            <TreeItem nodeId="4" label="Stack" />
                        </TreeItem>
                        <TreeItem nodeId="5" label="Algorithms">
                            <TreeItem nodeId="10" label="Gready" />
                            <TreeItem nodeId="6" label="Graph">
                                <TreeItem nodeId="8" label="DFS" />
                                <TreeItem nodeId="9" label="BFS" />
                            </TreeItem>
                        </TreeItem>
                    </TreeView>
                </Box>
            </div>
        </div>
    )
}
  
export default App

Output:

 

Reference: https://mui.com/material-ui/api/tree-view/


Article Tags :