MUI树取消第一个项目折叠事件。

huangapple go评论88阅读模式
英文:

MUI tree cancel first item collapse event

问题

I am using v5 MUI, and How to cancel the collapse event of the first item?

MUI树取消第一个项目折叠事件。

Like ALL in the picture

and remove dropdown icon

like list item in the picture

英文:

i am using with v5 MUI, and How to cancel the collapse event of the first item?

MUI树取消第一个项目折叠事件。

Like ALL in the picture

and remove dropdown icon

like list item in the picture

答案1

得分: 0

据我所知,无法在非受控模式下禁用单个TreeItem的展开/折叠功能(如果您正在使用非受控模式的话)。您可以通过将展开的项目列表保持在状态中,将该列表传递给TreeViewexpanded属性,并通过不为您不希望折叠的TreeItem(s)提供点击处理程序来轻松将您的TreeView切换到受控模式。例如:

  1. ...
  2. // 默认情况下将顶级节点的nodeId设置为"expanded"
  3. const [expanded, setExpanded] = React.useState(["1"]);
  4. // 根据您的选择处理展开逻辑(这只是一个示例)
  5. const createHandler = (id) => () => {
  6. if (!expanded.includes(id)) {
  7. setExpanded([...expanded, id]);
  8. } else {
  9. setExpanded(
  10. expanded.filter((i) => {
  11. return i !== id && !i.startsWith(`${id}.`);
  12. })
  13. );
  14. }
  15. };
  16. ...
  17. return (
  18. <TreeView
  19. className={classes.root}
  20. defaultCollapseIcon={<ExpandMoreIcon />}
  21. defaultExpandIcon={<ChevronRightIcon />}
  22. expanded={expanded}
  23. >
  24. {/* 不要为此节点提供点击处理程序,以防止其折叠 */}
  25. {/* 注意:我将一个空片段传递给`collapseIcon`属性,以便不显示折叠图标 */}
  26. <TreeItem nodeId="1" label="All Items" collapseIcon={<></>}>
  27. {/* 为所有可折叠子节点提供点击处理程序 */}
  28. <TreeItem
  29. nodeId="2"
  30. label="Second Level Item"
  31. onClick={createHandler("2")}
  32. />
  33. ...
  34. </TreeView>
  35. );
  36. ...

上面的示例还包括一种通过将空的React片段传递给collapseIcon属性来删除折叠图标的方法——但如果您更喜欢,也可以使用一些巧妙的CSS来实现这一点。

工作示例代码Sandbox: https://codesandbox.io/s/material-demo-forked-4nmsp6?file=/demo.tsx:1514-1699

英文:

To my knowledge, there's no way to disable the expand/collapse functionality of an individual TreeItem in uncontrolled mode (if that's what you're running). Presumably, you should be able to easily change your TreeView to run in controlled mode, by keeping the expanded item list in state, passing that list to the TreeView's expanded prop, and then force it to keep top-level items expanded by not providing a click handler for the TreeItem(s) that you don't want to collapse. For example:

  1. ...
  2. // Setting the top-level node&#39;s nodeId to &quot;expanded&quot;, by default
  3. const [expanded, setExpanded] = React.useState([&quot;1&quot;]);
  4. // Handle the expand logic however you choose (this is just an example)
  5. const createHandler = (id) =&gt; () =&gt; {
  6. if (!expanded.includes(id)) {
  7. setExpanded([...expanded, id]);
  8. } else {
  9. setExpanded(
  10. expanded.filter((i) =&gt; {
  11. return i !== id &amp;&amp; !i.startsWith(`${id}.`);
  12. })
  13. );
  14. }
  15. };
  16. ...
  17. return (
  18. &lt;TreeView
  19. className={classes.root}
  20. defaultCollapseIcon={&lt;ExpandMoreIcon /&gt;}
  21. defaultExpandIcon={&lt;ChevronRightIcon /&gt;}
  22. expanded={expanded}
  23. &gt;
  24. {/* Don&#39;t provide a click handler to this node so it won&#39;t collapse */}
  25. {/* Note: I&#39;m passing an empty fragment to the `collapseIcon` prop so no collapse icon is displayed */}
  26. &lt;TreeItem nodeId=&quot;1&quot; label=&quot;All Items&quot; collapseIcon={&lt;&gt;&lt;/&gt;}&gt;
  27. {/* Provide a click handler for all collapsible children */}
  28. &lt;TreeItem
  29. nodeId=&quot;2&quot;
  30. label=&quot;Second Level Item&quot;
  31. onClick={createHandler(&quot;2&quot;)}
  32. /&gt;
  33. ...
  34. &lt;/TreeView&gt;
  35. );
  36. ...

The example above also includes a method for removing the collapse icon by passing an empty React Fragment to the collapseIcon prop -- But you could also achieve this with some clever CSS, if you prefer.

Working CodeSandbox: https://codesandbox.io/s/material-demo-forked-4nmsp6?file=/demo.tsx:1514-1699

huangapple
  • 本文由 发表于 2023年6月13日 09:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定