Skip to content
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const states = new WeakMap()
class RemoteInputElement extends HTMLElement {
constructor() {
super()
const fetch = fetchResults.bind(null, this, true)
const state = {currentQuery: null, oninput: debounce(fetch), fetch, controller: null}
const fetch = (e: Event) => fetchResults.bind(null, this, true, e)
const state = {currentQuery: null, oninput: (e: Event) => debounce(fetch(e)), fetch, controller: null}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better:

Suggested change
const state = {currentQuery: null, oninput: (e: Event) => debounce(fetch(e)), fetch, controller: null}
const state = {currentQuery: null, oninput: debounce((e: Event) => fetch(e)), fetch, controller: null}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

states.set(this, state)
}

Expand Down Expand Up @@ -68,7 +68,7 @@ function makeAbortController() {
return {signal: null, abort() {}}
}

async function fetchResults(remoteInput: RemoteInputElement, checkCurrentQuery: boolean) {
async function fetchResults(remoteInput: RemoteInputElement, checkCurrentQuery: boolean, event?: Event) {
const input = remoteInput.input
if (!input) return

Expand Down Expand Up @@ -121,7 +121,7 @@ async function fetchResults(remoteInput: RemoteInputElement, checkCurrentQuery:

if (response && response.ok) {
resultsContainer.innerHTML = html
remoteInput.dispatchEvent(new CustomEvent('remote-input-success', {bubbles: true}))
remoteInput.dispatchEvent(new CustomEvent('remote-input-success', {bubbles: true, detail: { "eventType": event? event.type : undefined}}))
} else {
remoteInput.dispatchEvent(new CustomEvent('remote-input-error', {bubbles: true}))
}
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ describe('remote-input', function() {
it('loads content again after src is changed', async function() {
const result1 = once(remoteInput, 'remote-input-success')
changeValue(input, 'test')


await result1
assert.equal(results.querySelector('ol').getAttribute('data-src'), '/results?q=test')
Expand All @@ -122,6 +123,16 @@ describe('remote-input', function() {
await result2
assert.equal(results.querySelector('ol').getAttribute('data-src'), '/srcChanged?q=test')
})

it('check eventType', async function() {
const result = Promise(() => {
element.addEventListener(eventName, (event) => {
assert.equal(event.detail.eventType, 'change')
}, {once: true})
})
changeValue(input, 'test')
await result
})
})
})

Expand Down