Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96408c4

Browse files
authored
removing "input cleared" in favor of properly labelled button as per feedback from @jscholes
1 parent dcc6e1a commit 96408c4

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

examples/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</head>
4040
<body>
4141
<form>
42-
<label id="robots-label">Robots</label>
42+
<label id="robots-label" for="robot">Robots</label>
4343
<!-- To enable auto-select (select first on Enter), use data-autoselect="true" -->
4444
<auto-complete src="/demo" for="items-popup" aria-labelledby="robots-label" data-autoselect="true">
4545
<input name="robot" type="text" aria-labelledby="robots-label" autofocus>
@@ -57,10 +57,10 @@
5757

5858
<!-- example where clear button uses input id -->
5959
<form>
60-
<label id="robots-a-label">Robots-a</label>
60+
<label id="robots-a-label" for="robot-a">Robots (using Input ID)</label>
6161
<!-- To enable auto-select (select first on Enter), use data-autoselect="true" -->
62-
<auto-complete src="/demo" for="items-popup" aria-labelledby="robots-a-label" data-autoselect="true">
63-
<input id="robot-a" name="robot-a" type="search" aria-labelledby="robots-a-label" autofocus>
62+
<auto-complete src="/demo" for="items-a-popup" aria-labelledby="robots-a-label" data-autoselect="true">
63+
<input id="robot-a" name="robot-a" type="text" aria-labelledby="robots-a-label" autofocus>
6464
<!-- if a clear button is passed in, recommended to be *before* UL elements to avoid conflicting with their blur logic -->
6565
<button id="robot-a-clear">x</button>
6666
<ul id="items-a-popup" aria-labelledby="robots-a-label"></ul>
@@ -75,7 +75,7 @@
7575

7676
<!-- example without autoselect -->
7777
<form>
78-
<label id="robots-2-label">Robots 2</label>
78+
<label id="robots-2-label" for="robot-2">Robots (without autoselect on enter)</label>
7979
<auto-complete src="/demo" for="items-2-popup" aria-labelledby="robots-2-label" >
8080
<input name="robot-2" type="text" aria-labelledby="robots-2-label" autofocus>
8181
<ul id="items-2-popup" aria-labelledby="robots-2-label"></ul>

src/autocomplete.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export default class Autocomplete {
5656
this.clearButton = newClearButton
5757
}
5858

59+
// if clearButton doesn't have an accessible label, give it one
60+
if (this.clearButton && !this.clearButton.getAttribute('aria-label')) {
61+
const labelElem = document.querySelector(`label[for="${this.input.name}"`);
62+
const label = labelElem?.innerHTML || this.input.getAttribute('aria-label') || '';
63+
this.clearButton.setAttribute('aria-label', `clear ${label}`)
64+
}
65+
5966
this.results.hidden = true
6067
this.input.setAttribute('autocomplete', 'off')
6168
this.input.setAttribute('spellcheck', 'false')
@@ -94,7 +101,7 @@ export default class Autocomplete {
94101
this.input.value = ''
95102
this.container.value = ''
96103
this.input.focus()
97-
this.updateFeedbackForScreenReaders('Input cleared. Suggestions hidden.')
104+
this.updateFeedbackForScreenReaders('Suggestions hidden.')
98105
}
99106

100107
onKeydown(event: KeyboardEvent): void {

0 commit comments

Comments
 (0)