-
Notifications
You must be signed in to change notification settings - Fork 21
Add automatic combobox item ID #97
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,6 +126,9 @@ export default class Combobox { | |
| el.removeAttribute('data-combobox-option-default') | ||
|
|
||
| if (target === el) { | ||
| if (!target.id) { | ||
| target.id = `combobox-item-${Math.random().toString().slice(2, 6)}` | ||
| } | ||
|
Comment on lines
+129
to
+131
|
||
| this.input.setAttribute('aria-activedescendant', target.id) | ||
| target.setAttribute('aria-selected', 'true') | ||
| fireSelectEvent(target) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The auto-generated option id uses only a short
Math.random().toString().slice(2, 6)fragment and doesn’t check for existing IDs in the document. This makes duplicate IDs very likely as users navigate through many options (andNumber#toString()can sometimes yield fewer than 4 digits, shrinking the space further), which can breakaria-activedescendantresolution and violates unique-id requirements.Suggested fix: generate IDs in a much larger space and guarantee uniqueness (e.g., prefix with
this.list.idand use a per-instance counter, or usecrypto.randomUUID()when available with a fallback), and/or loop untildocument.getElementById(candidate)is null before assigning.