Skip to content

Commit 49d5f01

Browse files
Merge pull request #126 from github/kendallg/allow-no-results
Allow a no-results option with role=presentation
2 parents fa1e349 + 749c264 commit 49d5f01

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ item whose display text needs to be different:
6969
<li role="option" data-autocomplete-value="bb8">BB-8 (astromech)</li>
7070
```
7171
72+
Use `data-no-result-found="true"` to show a no results message inside the autocomplete popover. Be sure to add `role="presentation"`
73+
to this element so that screen readers do not mistake this as an auto-complete option. The auto-complete-element has built in functionality that
74+
handles aria-live announcing number of search results so this should be purely decorative.
75+
76+
```html
77+
<li role="presentation" aria-hidden="true" disabled data-no-result-found="true">No results found!</li>
78+
```
79+
7280
### A Note on Clear button
7381
While `input type="search"` comes with an `x` that clears the content of the field and refocuses it on many browsers, the implementation for this control is not keyboard accessible, and so we've opted to enable a customizable clear button so that your keyboard users will be able to interact with it.
7482

examples/index.html

+9-2
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,15 @@
100100
<div id="custom-fetching-items-popup-feedback" class="sr-only"></div>
101101
</auto-complete>
102102
<button type="submit">Save</button>
103-
</form>
103+
</form>
104104
<script>
105-
window.fetch = () => Promise.resolve(new Response(robotsList));
105+
window.fetch = (url) => {
106+
const query = url.split('?q=')[1]
107+
if (query === 'none') {
108+
return Promise.resolve(new Response('<li role="presentation" aria-hidden="true" data-no-result-found="true">No results found!</li>'))
109+
}
110+
return Promise.resolve(new Response(robotsList));
111+
}
106112
// fetchResult must be a function that return a Promise of string and that accepts as parameters an element and an URL
107113
document.querySelector("auto-complete#custom-fetching-method").fetchResult = async (el, url) => (await fetch(url)).text();
108114
</script>
@@ -123,3 +129,4 @@
123129
<script type="module" src="https://unpkg.com/@github/auto-complete-element@latest/dist/bundle.js"></script>
124130
</body>
125131
</html>
132+

src/autocomplete.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ export default class Autocomplete {
209209
this.identifyOptions()
210210
this.combobox.indicateDefaultOption()
211211
const allNewOptions = this.results.querySelectorAll('[role="option"]')
212-
const hasResults = !!allNewOptions.length
212+
213+
const hasResults =
214+
!!allNewOptions.length || !!this.results.querySelectorAll('[data-no-result-found="true"]').length
213215
const numOptions = allNewOptions.length
214216

215217
const [firstOption] = allNewOptions

test/auto-complete-element.js

+25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ import {AutoCompleteElement} from '../src/index.ts'
44
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
55

66
describe('auto-complete element', function () {
7+
describe('no results', () => {
8+
beforeEach(function () {
9+
document.body.innerHTML = `
10+
<div id="mocha-fixture">
11+
<auto-complete src="/noresults" for="popup">
12+
<input type="text">
13+
<ul id="popup"></ul>
14+
<div id="popup-feedback"></div>
15+
</auto-complete>
16+
</div>
17+
`
18+
})
19+
20+
it('checks that no results is displayed', async () => {
21+
const container = document.querySelector('auto-complete')
22+
const input = container.querySelector('input')
23+
const popup = container.querySelector('#popup')
24+
25+
triggerInput(input, 'none')
26+
await once(container, 'loadend')
27+
assert.isTrue(container.open)
28+
assert.equal(1, popup.children.length)
29+
})
30+
})
31+
732
describe('element creation', function () {
833
it('creates from document.createElement', function () {
934
const el = document.createElement('auto-complete')

web-test-runner.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export default {
2828
<li role="option" aria-disabled="true"><span>fourth</span></li>
2929
<li><a role="option" href="#hash">link</a></li>
3030
`
31+
} else if (method === 'GET' && url.startsWith('/noresults?q=none')) {
32+
response.status = 200
33+
response.body = `
34+
<li role="presentation" aria-hidden="true" disabled data-no-result-found="true">No results found!</li>
35+
`
3136
}
3237
await next()
3338
},

0 commit comments

Comments
 (0)