我正在尝试使用 Reactjs 中的材料来实现一棵树。但是,根据我的设计,折叠和展开的按钮应该在右侧。
另外,在添加这样的图标时出现错误TreeItem
:
<TreeItem nodeId="1" label="RSMSSB" icon={FolderIcon}>
全码:
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";
import FolderIcon from '@material-ui/icons/Folder';
const useStyles = makeStyles({
root: {
height: 216,
flexGrow: 1,
maxWidth: 400
}
});
export default function ControlledTreeView() {
const classes = useStyles();
const [expanded, setExpanded] = React.useState([]);
const handleChange = (event, nodes) => {
setExpanded(nodes);
};
return (
<TreeView
className={classes.root}
defaultCollapseIcon={<ExpandMoreIcon />}
defaultExpandIcon={<ChevronRightIcon />}
expanded={expanded}
onNodeToggle={handleChange}
>
<TreeItem nodeId="1" label="RSMSSB" icon={FolderIcon}>
<TreeItem nodeId="2" label="Science"></TreeItem>
<TreeItem nodeId="3" label="Mathematics">
<TreeItem nodeId="4" label="Polynomials"></TreeItem>
<TreeItem nodeId="7" label="Inequalities"></TreeItem>
</TreeItem>
<TreeItem nodeId="8" label="English"></TreeItem>
</TreeItem>
</TreeView>
);
}
模拟设计:
谢谢。
编辑:
工作示例:https : //codesandbox.io/s/purple-night-mgzwh