header-img
Info :
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
๋”๋ณด๊ธฐ
FRONTEND/React