-
Notifications
You must be signed in to change notification settings - Fork 20
Automatically set an ID on active combobox options #99
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
Changes from all commits
51f159b
b5fe29d
0ed23d9
101fb88
8cabac9
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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ export default class Combobox { | |
| tabInsertsSuggestions: boolean | ||
| firstOptionSelectionMode: FirstOptionSelectionMode | ||
| scrollIntoViewOptions?: boolean | ScrollIntoViewOptions | ||
| didAutoAssignLastSelectedId: boolean | ||
|
|
||
| constructor( | ||
| input: HTMLTextAreaElement | HTMLInputElement, | ||
|
|
@@ -44,6 +45,7 @@ export default class Combobox { | |
| this.scrollIntoViewOptions = scrollIntoViewOptions ?? {block: 'nearest', inline: 'nearest'} | ||
|
|
||
| this.isComposing = false | ||
| this.didAutoAssignLastSelectedId = false | ||
|
|
||
| if (!list.id) { | ||
| list.id = `combobox-${Math.random().toString().slice(2, 6)}` | ||
|
|
@@ -126,11 +128,19 @@ export default class Combobox { | |
| el.removeAttribute('data-combobox-option-default') | ||
|
|
||
| if (target === el) { | ||
| if (!target.id) { | ||
| target.id = `${this.list.id}-selected` | ||
| this.didAutoAssignLastSelectedId = true | ||
| } | ||
| this.input.setAttribute('aria-activedescendant', target.id) | ||
| target.setAttribute('aria-selected', 'true') | ||
|
Comment on lines
+131
to
136
|
||
| fireSelectEvent(target) | ||
| target.scrollIntoView(this.scrollIntoViewOptions) | ||
| } else { | ||
| if (el.id === `${this.list.id}-selected` && this.didAutoAssignLastSelectedId) { | ||
| el.removeAttribute('id') | ||
| this.didAutoAssignLastSelectedId = false | ||
| } | ||
| el.removeAttribute('aria-selected') | ||
|
francinelucca marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
@@ -141,6 +151,10 @@ export default class Combobox { | |
| for (const el of this.list.querySelectorAll('[aria-selected="true"], [data-combobox-option-default="true"]')) { | ||
| el.removeAttribute('aria-selected') | ||
| el.removeAttribute('data-combobox-option-default') | ||
| if (el.id === `${this.list.id}-selected` && this.didAutoAssignLastSelectedId) { | ||
| el.removeAttribute('id') | ||
| this.didAutoAssignLastSelectedId = false | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
This change introduces new behavior for options without ids (auto-assigning and moving
${list.id}-selected). There are existing tests foraria-activedescendantbehavior intest/test.js, but none cover the missing-id path; please add tests to validate id assignment, id movement across options, and cleanup when selection is cleared/stopped.