gpt4 book ai didi

reactjs - 如何将折叠/展开图标更改为 Material TreeView 的右侧?

转载 作者:行者123 更新时间:2023-12-03 16:03:06 32 4
gpt4 key购买 nike

我正在尝试使用 Reactjs 中的 Material 来实现一棵树。但是,根据我的设计,折叠和展开的按钮应该在右侧。

另外,在 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>
);
}

模拟设计:

enter image description here

谢谢。

编辑:
工作示例: https://codesandbox.io/s/purple-night-mgzwh

最佳答案

基于 Customized tree view例如,您可以创建一个自定义组件,该组件将文本和图标传递给 TreeItem 的标签属性。并在右侧添加展开/折叠图标 flex-direction: row-reverse;TreeItem 的内容类:

const useTreeItemStyles = makeStyles(theme => ({
content: {
flexDirection: 'row-reverse'
},
labelRoot: {
display: 'flex',
alignItems: 'center',
padding: theme.spacing(0.5, 0),
},
labelIcon: {
marginRight: theme.spacing(1),
},
labelText: {
fontWeight: 'inherit',
flexGrow: 1,
},
}));

function StyledTreeItem(props) {
const classes = useTreeItemStyles();
const { labelText, labelIcon: LabelIcon, ...other } = props;

return (
<TreeItem
label={
<div className={classes.labelRoot}>
<LabelIcon color="inherit" className={classes.labelIcon} />
<Typography variant="body2" className={classes.labelText}>
{labelText}
</Typography>
</div>
}
classes={{
content: classes.content
}}
{...other}
/>
);
}

...

<TreeView
className={classes.root}
defaultCollapseIcon={<ExpandMoreIcon />}
defaultExpandIcon={<ChevronRightIcon />}
expanded={expanded}
onNodeToggle={handleChange}
>
<StyledTreeItem nodeId="1" labelText="RSMSSB" labelIcon={FolderIcon} />

...

工作示例:

Edit green-surf-dcoqr

关于reactjs - 如何将折叠/展开图标更改为 Material TreeView 的右侧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59819211/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com