-
Notifications
You must be signed in to change notification settings - Fork 55
[Accessibility] Adds support for an accessible "clear" button #53
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 4 commits
79a727a
ff94715
06d0220
a59d75d
45b75dd
8fdeb49
8c8e205
dcc6e1a
96408c4
e5f01b1
e450f99
19b9274
fbbdc16
0176081
0184e49
02b7ce9
3d55be1
dd48031
3aeab1b
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ export default class Autocomplete { | |
| feedback: HTMLElement | null | ||
| autoselectEnabled: boolean | ||
| clientOptions: NodeListOf<HTMLElement> | null | ||
| clearButton: HTMLElement | null | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can be more strict and use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, someone can add the ID to another element like an anchor or a span. 🤔 I would like to keep it this way just in case, but I'm open to other ideas. |
||
|
|
||
| interactingWithList: boolean | ||
|
|
||
|
|
@@ -30,6 +31,7 @@ export default class Autocomplete { | |
| this.combobox = new Combobox(input, results) | ||
| this.feedback = document.getElementById(`${this.results.id}-feedback`) | ||
| this.autoselectEnabled = autoselectEnabled | ||
| this.clearButton = document.getElementById(`${this.input.id || this.input.getAttribute('name')}-clear`) | ||
|
inkblotty marked this conversation as resolved.
Outdated
|
||
|
|
||
| // check to see if there are any default options provided | ||
| this.clientOptions = results.querySelectorAll('[role=option]') | ||
|
|
@@ -40,6 +42,20 @@ export default class Autocomplete { | |
| this.feedback.setAttribute('aria-atomic', 'true') | ||
| } | ||
|
|
||
| // if clearButton is not a button, make it one | ||
| if (this.clearButton && this.clearButton.tagName.toLowerCase() !== 'button') { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const [tagName, ...otherAttributes] = this.clearButton.attributes | ||
| const newClearButton = document.createElement('button') | ||
| newClearButton.innerHTML = this.clearButton.innerHTML | ||
| newClearButton.id = this.clearButton.id | ||
| for (const attr of otherAttributes) { | ||
| newClearButton.setAttribute(attr.name, attr.value) | ||
| } | ||
| this.clearButton.parentNode?.replaceChild(newClearButton, this.clearButton) | ||
| this.clearButton = newClearButton | ||
| } | ||
|
|
||
|
inkblotty marked this conversation as resolved.
Outdated
|
||
| this.results.hidden = true | ||
| this.input.setAttribute('autocomplete', 'off') | ||
| this.input.setAttribute('spellcheck', 'false') | ||
|
|
@@ -52,13 +68,15 @@ export default class Autocomplete { | |
| this.onInputFocus = this.onInputFocus.bind(this) | ||
| this.onKeydown = this.onKeydown.bind(this) | ||
| this.onCommit = this.onCommit.bind(this) | ||
| this.handleClear = this.handleClear.bind(this) | ||
|
|
||
| this.input.addEventListener('keydown', this.onKeydown) | ||
| this.input.addEventListener('focus', this.onInputFocus) | ||
| this.input.addEventListener('blur', this.onInputBlur) | ||
| this.input.addEventListener('input', this.onInputChange) | ||
| this.results.addEventListener('mousedown', this.onResultsMouseDown) | ||
| this.results.addEventListener('combobox-commit', this.onCommit) | ||
| this.clearButton?.addEventListener('click', this.handleClear) | ||
|
inkblotty marked this conversation as resolved.
|
||
| } | ||
|
|
||
| destroy(): void { | ||
|
|
@@ -70,6 +88,15 @@ export default class Autocomplete { | |
| this.results.removeEventListener('combobox-commit', this.onCommit) | ||
| } | ||
|
|
||
| handleClear(event: Event): void { | ||
| event.preventDefault() | ||
|
|
||
| this.input.value = '' | ||
| this.container.value = '' | ||
| this.input.focus() | ||
|
inkblotty marked this conversation as resolved.
|
||
| this.updateFeedbackForScreenReaders(`Input cleared. Suggestions hidden.`) | ||
|
inkblotty marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| onKeydown(event: KeyboardEvent): void { | ||
| // if autoselect is enabled, Enter key will select the first option | ||
| if (event.key === 'Enter' && this.container.open && this.autoselectEnabled) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.