forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TreeView] Add recursive demo (mui#19636)
- Loading branch information
1 parent
1bd9e6c
commit bbca056
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import TreeView from '@material-ui/lab/TreeView'; | ||
import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; | ||
import ChevronRightIcon from '@material-ui/icons/ChevronRight'; | ||
import TreeItem from '@material-ui/lab/TreeItem'; | ||
|
||
const data = { | ||
id: 'root', | ||
name: 'Parent', | ||
children: [ | ||
{ | ||
id: '1', | ||
name: 'Child - 1', | ||
}, | ||
{ | ||
id: '3', | ||
name: 'Child - 3', | ||
children: [ | ||
{ | ||
id: '4', | ||
name: 'Child - 4', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
height: 110, | ||
flexGrow: 1, | ||
maxWidth: 400, | ||
}, | ||
}); | ||
|
||
export default function RecursiveTreeView() { | ||
const classes = useStyles(); | ||
|
||
const renderTree = nodes => ( | ||
<TreeItem key={nodes.id} nodeId={nodes.id} label={nodes.name}> | ||
{Array.isArray(nodes.children) ? nodes.children.map(node => renderTree(node)) : null} | ||
</TreeItem> | ||
); | ||
|
||
return ( | ||
<TreeView | ||
className={classes.root} | ||
defaultCollapseIcon={<ExpandMoreIcon />} | ||
defaultExpanded={['root']} | ||
defaultExpandIcon={<ChevronRightIcon />} | ||
> | ||
{renderTree(data)} | ||
</TreeView> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import TreeView from '@material-ui/lab/TreeView'; | ||
import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; | ||
import ChevronRightIcon from '@material-ui/icons/ChevronRight'; | ||
import TreeItem from '@material-ui/lab/TreeItem'; | ||
|
||
interface RenderTree { | ||
id: string; | ||
name: string; | ||
children?: RenderTree[]; | ||
} | ||
|
||
const data: RenderTree = { | ||
id: 'root', | ||
name: 'Parent', | ||
children: [ | ||
{ | ||
id: '1', | ||
name: 'Child - 1', | ||
}, | ||
{ | ||
id: '3', | ||
name: 'Child - 3', | ||
children: [ | ||
{ | ||
id: '4', | ||
name: 'Child - 4', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
height: 110, | ||
flexGrow: 1, | ||
maxWidth: 400, | ||
}, | ||
}); | ||
|
||
export default function RecursiveTreeView() { | ||
const classes = useStyles(); | ||
|
||
const renderTree = (nodes: RenderTree) => ( | ||
<TreeItem key={nodes.id} nodeId={nodes.id} label={nodes.name}> | ||
{Array.isArray(nodes.children) ? nodes.children.map(node => renderTree(node)) : null} | ||
</TreeItem> | ||
); | ||
|
||
return ( | ||
<TreeView | ||
className={classes.root} | ||
defaultCollapseIcon={<ExpandMoreIcon />} | ||
defaultExpanded={['root']} | ||
defaultExpandIcon={<ChevronRightIcon />} | ||
> | ||
{renderTree(data)} | ||
</TreeView> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters