728x90
1. MUI ์ค์น
MUI ์ค์น ์ ์ผ๊ด ์ค์น๊ฐ ๋๋ ๊ฒ ๊ฐ์๋ฐ.. ์๋์ด ์๋ค๋ฉด @MUI/LAB ์ ์ค์นํด์ฃผ๋๋ก ํ์.
$ npm install @mui/lab
2. MUI ํ ์คํธ์ฉ treeview ์์ฑ
import React from "react";
import TreeView from "@mui/lab/TreeView";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
import TreeItem from "@mui/lab/TreeItem";
const TreeViewExample = () => {
return (
<div>
<TreeView
aria-label="file system navigator"
defaultCollapseIcon={<ExpandMoreIcon />}
defaultExpandIcon={<ChevronRightIcon />}
sx={{ height: 240, flexGrow: 1, maxWidth: 400, overflowY: "auto" }}
>
<TreeItem nodeId="1" label="Applications" >
<TreeItem nodeId="2" label="Calendar" />
<TreeItem nodeId="3" label="Chrome" />
<TreeItem nodeId="4" label="Webstorm" />
</TreeItem>
<TreeItem nodeId="5" label="Documents">
<TreeItem nodeId="10" label="OSS" />
<TreeItem nodeId="6" label="MUI">
<TreeItem nodeId="7" label="src">
<TreeItem nodeId="8" label="index.js" />
<TreeItem nodeId="9" label="tree-view.js" />
</TreeItem>
</TreeItem>
</TreeItem>
</TreeView>
</div>
);
};
export default TreeViewExample;
3. label ๊ฐ์ง๊ณ ์ค๋ ๋ฐฉ๋ฒ
๋ณดํต ์์ฑ์ labeltext ๋ฅผ ๊ฐ์ง๊ณ ์ค๊ฒ ๋์ด ์๋๋ฐ..
์ฌ๊ธฐ์ ๊ฒฝ์ฐ innerText ๋ outerText ๋ฅผ ์ฌ์ฉํด์ผ ์ํ๋ ๊ฐ์ ๊ฐ์ง๊ณ ์ฌ ์ ์๊ฒ ๋์ด ์๋ ๊ฒ ๊ฐ๋ค.
(์์ง ๋ชป ์ฐพ์ ๊ฒ์ผ์๋ ์์)
import React from "react";
import TreeView from "@mui/lab/TreeView";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
import TreeItem from "@mui/lab/TreeItem";
const TreeViewExample = () => {
return (
<div>
<TreeView
aria-label="file system navigator"
defaultCollapseIcon={<ExpandMoreIcon />}
defaultExpandIcon={<ChevronRightIcon />}
sx={{ height: 240, flexGrow: 1, maxWidth: 400, overflowY: "auto" }}
>
<TreeItem nodeId="1" label="Applications" onClick={(e)=>console.log(e.target.outerText)}>
<TreeItem nodeId="2" label="Calendar" onClick={(e)=>console.log(e.target.innerText)}/>
<TreeItem nodeId="3" label="Chrome" />
<TreeItem nodeId="4" label="Webstorm" />
</TreeItem>
<TreeItem nodeId="5" label="Documents">
<TreeItem nodeId="10" label="OSS" />
<TreeItem nodeId="6" label="MUI">
<TreeItem nodeId="7" label="src">
<TreeItem nodeId="8" label="index.js" onClick={(e)=>console.log(e.target.innerText)}/>
<TreeItem nodeId="9" label="tree-view.js" onClick={(e)=>console.log(e.target.innerText)}/>
</TreeItem>
</TreeItem>
</TreeItem>
</TreeView>
</div>
);
};
export default TreeViewExample;
ํ์์ ๊ฒฝ์ฐ innerText ์ outerText ๋ก ๊ฐ์ง๊ณ ์ค๋ ๋ฐฉ๋ฒ ๋๊ฐ์ง๋ฅผ ๋ชจ๋ ์ ์ด๋ณด์๋ค.
Applications ํด๋ฆญ์ Applications ๊ฐ console์ ๋จ๋๋ก ํด๋์๋ค.
728x90