diff --git a/README.md b/README.md index 9108c71..609f080 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,14 @@ item whose display text needs to be different:
  • BB-8 (astromech)
  • ``` +Use `data-no-result-found="true"` to show a no results message inside the autocomplete popover. Be sure to add `role="presentation"` +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 +handles aria-live announcing number of search results so this should be purely decorative. + +```html + +``` + ### A Note on Clear button 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. diff --git a/examples/index.html b/examples/index.html index 2b90b7d..bd89f1f 100644 --- a/examples/index.html +++ b/examples/index.html @@ -100,9 +100,15 @@
    - + @@ -123,3 +129,4 @@ + \ No newline at end of file diff --git a/src/autocomplete.ts b/src/autocomplete.ts index 13126dc..9072274 100644 --- a/src/autocomplete.ts +++ b/src/autocomplete.ts @@ -209,7 +209,9 @@ export default class Autocomplete { this.identifyOptions() this.combobox.indicateDefaultOption() const allNewOptions = this.results.querySelectorAll('[role="option"]') - const hasResults = !!allNewOptions.length + + const hasResults = + !!allNewOptions.length || !!this.results.querySelectorAll('[data-no-result-found="true"]').length const numOptions = allNewOptions.length const [firstOption] = allNewOptions diff --git a/test/auto-complete-element.js b/test/auto-complete-element.js index 7d1827f..87cdcea 100644 --- a/test/auto-complete-element.js +++ b/test/auto-complete-element.js @@ -4,6 +4,31 @@ import {AutoCompleteElement} from '../src/index.ts' const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)) describe('auto-complete element', function () { + describe('no results', () => { + beforeEach(function () { + document.body.innerHTML = ` +
    + + + + + +
    + ` + }) + + it('checks that no results is displayed', async () => { + const container = document.querySelector('auto-complete') + const input = container.querySelector('input') + const popup = container.querySelector('#popup') + + triggerInput(input, 'none') + await once(container, 'loadend') + assert.isTrue(container.open) + assert.equal(1, popup.children.length) + }) + }) + describe('element creation', function () { it('creates from document.createElement', function () { const el = document.createElement('auto-complete') diff --git a/web-test-runner.config.js b/web-test-runner.config.js index 8647d26..24ebe22 100644 --- a/web-test-runner.config.js +++ b/web-test-runner.config.js @@ -28,6 +28,11 @@ export default {
  • fourth
  • link
  • ` + } else if (method === 'GET' && url.startsWith('/noresults?q=none')) { + response.status = 200 + response.body = ` + + ` } await next() },