Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TreeView] In the RichTreeView, do not use the item id as the HTML id attribute #12319

Merged
merged 1 commit into from
Mar 6, 2024

Conversation

flaviendelangle
Copy link
Member

Problem

When using the following code:

const ITEMS = [{ id: '1', label: 'Item 1' }]

<RichTreeView id="my-tree" items={ITEMS} />

Will produce the following HTML:

<ul id="my-tree">
  <li class="MuiTreeItem-root" id="1" />
</ul>

With this behavior, it is very very easy to unintentionally set an id attribute to your item that is not unique in the whole document.$

Solution

No setting the id attribute in state allows us to use the fallback which is ${treeId}-${nodeId}:

<ul id="my-tree">
  <li class="MuiTreeItem-root" id="my-tree-1" />
</ul>

We could add a new prop getItemIdAttribute to help people customize the id attribute.
Otherwise, they risk to do ugly things such as:

<RichTreeView
  items={ITEMS}
  slotProps={{
    item: ({ someProp }) => ({ id: someProp }),
  }}
/>

And if they do that, we won't be able to use the ID at the TreeView level to set the aria-activedescendant.

@flaviendelangle flaviendelangle self-assigned this Mar 4, 2024
@flaviendelangle flaviendelangle added the component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! label Mar 4, 2024
@flaviendelangle flaviendelangle added this to the 7.0.0 milestone Mar 4, 2024
@mui-bot
Copy link

mui-bot commented Mar 4, 2024

Deploy preview: https://deploy-preview-12319--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against 4ea6f7c

Copy link
Contributor

@noraleonte noraleonte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition! 🎉

Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement! 👏

@LukasTy LukasTy added the enhancement This is not a bug, nor a new feature label Mar 6, 2024
@flaviendelangle flaviendelangle merged commit 6733cf7 into mui:next Mar 6, 2024
18 of 19 checks passed
@flaviendelangle flaviendelangle deleted the rich-tree-view-id-attr branch March 6, 2024 12:35
thomasmoon pushed a commit to thomasmoon/mui-x that referenced this pull request Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants