Skip to content

Commit

Permalink
Merge pull request #100 from loisolire/allow-empty-query-search
Browse files Browse the repository at this point in the history
Add `fetch-on-empty` optional attribute
  • Loading branch information
keithamus authored Mar 30, 2023
2 parents b2d87f2 + 5a0e443 commit a058977
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
14 changes: 13 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,19 @@
document.querySelector("auto-complete#custom-fetching-method").fetchResult = async (el, url) => (await fetch(url)).text();
</script>

<form>
<label id="fetch-on-empty-robots-label" for="fetch-on-empty-robot">Fetch on empty Robots</label>
<!-- To enable fetch on empty use fetch-on-empty attribute -->
<auto-complete src="/demo" for="fetch-on-empty-items-popup" aria-labelledby="fetch-on-empty-robots-label" fetch-on-empty>
<input name="fetch-on-empty-robot" type="text" aria-labelledby="fetch-on-empty-robots-label" autofocus />
<button id="fetch-on-empty-robot-clear">x</button>
<ul id="fetch-on-empty-items-popup"></ul>
<div id="fetch-on-empty-items-popup-feedback" class="sr-only"></div>
</auto-complete>
<button type="submit">Save</button>
</form>

<!-- <script type="module" src="./dist/bundle.js"></script>-->
<script type="module" src="https://unpkg.com/@github/auto-complete-element@latest/dist/bundle.js"></script>
<script type="module" src="https://unpkg.com/@github/auto-complete-element@latest/dist/bundle.js"></script>
</body>
</html>
8 changes: 8 additions & 0 deletions src/auto-complete-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export default class AutocompleteElement extends HTMLElement {
}
}

get fetchOnEmpty(): boolean {
return this.hasAttribute('fetch-on-empty')
}

set fetchOnEmpty(fetchOnEmpty: boolean) {
this.toggleAttribute('fetch-on-empty', fetchOnEmpty)
}

fetchResult = fragment

static get observedAttributes(): string[] {
Expand Down
2 changes: 1 addition & 1 deletion src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export default class Autocomplete {

fetchResults(): void {
const query = this.input.value.trim()
if (!query) {
if (!query && !this.container.fetchOnEmpty) {
this.container.open = false
return
}
Expand Down
45 changes: 45 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,51 @@ describe('auto-complete element', function () {
assert.equal(`5 results. first is the top result: Press Enter to activate.`, feedback.innerHTML)
})
})

describe('fetch on empty enabled', () => {
let container
beforeEach(function () {
document.body.innerHTML = `
<div id="mocha-fixture">
<auto-complete fetch-on-empty src="/moke" for="popup" data-autoselect="true">
<input type="text" value="1">
<ul id="popup"></ul>
<div id="popup-feedback"></div>
</auto-complete>
</div>
`
container = document.querySelector('auto-complete')
container.fetchResult = async () => `
<li>Mock Custom Fetch Result 1</li>
<li>Mock Custom Fetch Result 2</li>`
})

it('should fetch result when value is empty', async function () {
const input = container.querySelector('input')
const popup = container.querySelector(`#popup`)
const feedback = container.querySelector(`#popup-feedback`)

triggerInput(input, '')
await once(container, 'loadend')

assert.equal(2, popup.children.length)
assert.equal(popup.querySelector('li').textContent, 'Mock Custom Fetch Result 1')
assert.equal(feedback.textContent, '')
})

it('does not fetch result when value is empty, if fetch-on-empty removed', async function () {
const input = container.querySelector('input')
const popup = container.querySelector(`#popup`)
const feedback = container.querySelector(`#popup-feedback`)
container.fetchOnEmpty = false

triggerInput(input, '')
await new Promise(resolve => setTimeout(resolve, 100))

assert.equal(0, popup.children.length)
assert.equal(feedback.textContent, '')
})
})
})

function waitForElementToChange(el) {
Expand Down

0 comments on commit a058977

Please sign in to comment.