[autocomplete] Grouped options throws accessibility scanner issues
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 99.1k
- Forks
- 32.5k
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 106
Description
Steps to reproduce
Steps:
- Create an autocomplete component with grouping enabled, same as the official example.
<Autocomplete
id="grouped-demo"
options={options.sort((a, b) => -b.firstLetter.localeCompare(a.firstLetter))}
groupBy={(option) => option.firstLetter}
getOptionLabel={(option) => option.title}
sx={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="With categories" />}
/>
- Open the dropdown and scan with an accessibility tool like axe DevTools
Note: you may need to force the autocomplete to stay open while you scan withopen={true}
Current behavior
I get a bunch of errors around incorrect aria roles:
This is because <li /> are not allowed as children of listbox.
The current role structure is:
listbox
generic <--- the group label
list
option
option
...
generic
list
option
...
Expected behavior
No accessibility errors.
To achieve this, you should follow the ARIA APG Listbox pattern which forms this role structure:
listbox
group
presentation <--- the group label
option
option
...
group
presentation
option
...
This can be achieved in html like this
<div role="listbox">
<ul role="group">
<li role="presentation">
Group 1
</li>
<li role="option">
Option 1
</li>
...
</ul>
<ul role="group">
<li role="presentation">
Group 2
</li>
<li role="option">
Option 1
</li>
...
</ul>
</div>
In my case, I achieved this in the Autocomplete component with fully custom rendering (renderOption, renderGroup), but I wouldn't expect most users to do this.
Context
I was trying to create a grouped autocomplete list.
My company is a paying customer, our support key / order number is 65425
Your environment
npx @mui/envinfo
System:
OS: Windows 11 10.0.22631
Binaries:
Node: 21.5.0 - C:\Program Files\nodejs\node.EXE
npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
pnpm: Not Found
Browsers:
Chrome: Not Found
Edge: Chromium (126.0.2592.68)
Search keywords: autocomplete accessibility roles
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.